#include <sys/types.h> #include <sys/socket.h> int socket(int family, int type, int protocol);The family is one of
AF_UNIX -- Unix internal protocols AF_INET -- Internet protocols AF_NS -- Xerox NS Protocols AF_IMPLINK -- IMP link layerThe AF_ prefix stands for "address family." In the first project, we are going to use AF_INET.
SOCK_STREAM stream socket SOCK_DGRAM datagram socket SOCK_RAW raw socket SOCK_SEQPACKET sequenced packet socket SOCK_RDM reliably delivered message socket (not implemented yet)
family | type | protocol | Actual protocol |
AF_INET | SOCK_DGRAM | IPPROTO_UDP | UDP |
AF_INET | SOCK_STREAM | IPPROTO_TCP | TCP |
AF_INET | SOCK_RAW | IPPROTO_ICMP | ICMP |
AF_INET | SOCK_RAW | IPPROTO_RAW | (raw) |
#include <sys/types.h> #include <sys/socket.h> int bind(int sockfd, struct sockaddr *myaddr, int addrlen);The first argument is the socket descriptor returned from socket system call. The second argument is a pointer to a protocol-specific address and the third argument is the size of this address. There are three uses of bind.
#include <sys/types.h> #include <sys/socket.h> int connect(int sockfd, struct sockaddr *servaddr, int addrlen);The sockfd is a socket descriptor that was returned by the socket system call. The second and third arguments are a pointer to a socket address, and its size, as described earlier.
#include <sys/types.h> #include <sys/socket.h> int listen(int sockfd, int backlog);It is usually executed after both the socket and bind system calls, and immediately before the accept system call. The backlog argument specifies how many connection requests can be queued by the system while it waits for the server to execute the accept system call. This argument is usually specified as 5, the maximum value currently allowed.
#include <sys/types.h> #include <sys/socket.h> int accept(int sockfd, struct sockaddr *peer, int *addrlen);accept takes the first connection request on the queue and creates another socket with the same properties as sockfd. If there are no connection requests pending, this call blocks the caller until one arrives.
#include <sys/types.h> #include <sys/socket.h> int send(int sockfd, char *buff, int nbytes, int flags); int sendto(int sockfd, char *buff, int nbytes, int flags, struct sockaddr *to, int addrlen); int recv(int sockfd, char *buff, int nbytes, int flags); int recvfrom(int sockfd, char *buff, int nbytes, int flags, struct sockaddr *from, int *addrlen);The first three arguments, sockfd, buff, and nbytes, to the four system calls are similar to the first three arguments for read and write. The flags argument can be safely set to zero ignoring the details for it. The to argument for sendto specifies the protocol-specific address of where the data is to be sent. Since this address is protocol-specific, its length must be specified by addrlen. The recvfrom system call fills in the protocol-specific address of who sent the data into from. The length of this address is also returned to the caller in addrlen. Note that the final argument to sendto is an integer value, while the final argument to recvfrom is a pointer to an integer value.
int close(int fd);If the socket being closed is associated with a protocol that promises reliable delivery (e.g., TCP or SPP), the system must assure that any data within the kernel that still has to be transmitted or acknowledged, is sent. Normally, the system returns from the close immediately, but the kernel still tries to send any data already queued.
Last modified January 7, 1998 by mike@cs.hmc.edu