150 likes | 292 Views
Unix Network Programming Ch # 4. Elementary TCP Sockets. Elementary Socket functions. Socket Function. To perform network I/O, first thing a process must do is call the socket function. #include <sys/socket.h> int socket(int family, int type, int protocol);
E N D
Unix Network Programming Ch # 4 Elementary TCP Sockets
Socket Function • To perform network I/O, first thing a process must do is call the socket function #include <sys/socket.h> int socket(int family, int type, int protocol); - returns: non-negative descriptor if ok, -1 on error
Connect funciton • The connect function is used by a TCP client to establish a connection with a TCP server: #include <sys/socket.h> int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); • Returns: 0 if ok, -1 on error • Sockfd is a socket descriptor returned by the socket function • 2nd & 3rd args are the socket address structures, must contain the address of the server to communicate with • The client does not have to call bind • The kernel chooses both an ephemeral port and the source IP address if necessary.
Bind function • The bind funtion assigns a local protocol address to a socket. • With IP, combination of 32-bit (IPv4 or 128-bit for IPv6) address, along with a 16-bit TCP or UDP port number. #include <sys/socket.h> int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); • Servers bind to their well-known port when they start • A process can bind a specific IP address to its socket • Normally, however, a client does not bind an IP address, so that client can then respond on any interface available on the host
Listen function • The listen function is called only by a TCP server and it performs 2 actions • Converts an unconnected (active) socket into a passive socket (indicates kernel should accept incoming connect requests directed to this socket • 2nd argument specifies the maximum number of connections kernel should queue for this socket #include <sys/socket.h> int listen(int sockfd, int backlog);
Listen function • Normally called after both the socket and bind function, only by the server of course • Backlog - for a given listening socket, the kernel maintains 2 queues: • An incomplete connection queue, which contains an entry for each SYN that has arrived from a client for which server is awaiting completion of the TCP 3-way handshake • A completed connection queue, entry for each client with whom 3-way handshake has completed. Figure 4.7, pg. 105
Accept function • Accept is called by a TCP server to return the next completed connection from the front of the completed connection queue. • If completed queue is empty, the process is put to sleep. #include <sys/socket.h> int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); • Returns: non-negative descriptor if OK, -1 on error • The cliaddr and addrlen args are used to return the protocol address of the connect peer process (the client).
Fork and exec functions • We will look at building a concurrent server • Need to create a new child process to handle each incomming client request/transaction • fork function is the only way in Unix to create a new process: #include <unistd.h> pid_t fork(void); • Returns: 0 in child, process ID of child in parent, -1 on error • Called once but returns TWICE • Once in the parent process (returns child process id), • and once in the child process (return of 0)
More Forking • All descriptors open in the parent before the call to fork() are shared with the child after fork returns. • Including the connected socket file description returned by accept
Exec function • Only way in which an executable program file on disk can be executed in Unix is for an existing process to call one of the 6 exec functions
Concurrent Servers • When a client request can take some time to service, don't want to take away time for handling connections to service a single client • Handle the communication with multiple clients at the same time • Simplest way to write a concurrent server under Unix is to fork a child process to handle each client.
Close function • Close() function used to close a socket and terminate a TCP connection #include <unistd.h> int close(int sockfd); • Returns: 0 if ok, -1 on error • Default action of close with a TCP socket description is to mark the socket as closed and return tot he process immediately. • Socket descriptor is no longer usable to the app process at this point • But TCP will try to send any data that is already queued, and once flushed begin the normal TCP termination sequence.