This preview shows page 1. Sign up to view the full content.
Unformatted text preview: olution was to deﬁne sockets functions to expect a pointer to a generic sockaddr structure, and then require applications to cast pointers to protocol-speciﬁc structures to this generic structure. To simplify our code examples, we will follow Stevens’s lead and deﬁne the following type
1 typedef struct sockaddr SA; that we use whenever we need to cast a protocol-speciﬁc structure to a generic one. 12.5.2 The socket Function
Clients and servers use the socket function to create a socket descriptor.
#include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol);
returns: nonnegative descriptor if OK, -1 on error In our codes, we will always call the socket function with the following arguments:
1 sockfd = Socket(AF_INET, SOCK_STREAM, 0); where AF INET indicates that we are using the Internet, and SOCK STREAM indicates that the socket will be an endpoint for an Internet connection. The sockfd descriptor returned by socket is only partially opened and cannot yet be used for reading and writing. How we ﬁnish opening the socket depends on...
View Full Document
- Spring '10
- The American