What's the definition of socket,why need it and how it works? -
i know socket used in network program, , can write code it. can't understand things:
1. why need sockets?
if used distinguish application layer program network layer, why not use appliation name or other uniqueue attribute?
is socket standard?
is socket structure in linux or other os?
2. how socket works?
there lot of functions around socket,such bind, accept, listen, send.for example, send() send msg dest tcp, , how send msg network-layer? send msg tcp buffer? how network layer process read tcp buffer data , how send data lower layer process?
if used send(int sockfd, const void *buf, size_t len, int flags)
function, first arg 'sockfd' undefined socket, such random int, result cause , why? os process?
on unix, socket kind of file descriptor. on windows, socket kind of handle. file descriptors , handles way these oss allow various operations (such read , write) performed on resources. network connection considered os resource provided network service of os.
the socket api more or less standard, although each os may provide os specific apis os specific options standard apis.
the underlying implementation of socket involves structure.
how os implements socket apis not different implement other system calls. send()
api converted system call delivers data network stack. network stack manage own buffers, send()
typically cause data in userspace copied network buffer (which not unlike happens when write()
system call called). network stack processes data through code, passing different layers (typically: socket -> tcp -> ip -> ethernet driver). @ lowest layer, similar device driver os uses communicate physical device.
when pass non-socket file descriptor send()
, expect error, either ebadf
, einval
, or enotsock
, likely.
Comments
Post a Comment