TCPIP Sockets ECS 152 B Xin Liu What
TCP/IP Sockets ECS 152 B Xin Liu
What is a socket? • API: network programming interface • Socket: abstraction through which an application may send/receive data. • Allows an application to “plug-in” a network and communicates with other apps. in the same network. • Sockets: different underlying protocol families. Application Sockets Protocol A Protocol B Protocol C ECS 152 B Xin Liu
TCP/IP • TCP/IP does not include an API definition. • There a variety of APIs for use with TCP/IP: – Sockets (Berkeley sockets) – TLI (Transport layer interface by AT&T) – Winsock – Mac. TCP ECS 152 B Xin Liu
Functions needed • Specify local and remote communication endpoints • Initiate a connection • Wait for incoming connection • Send and receive data • Terminate a connection gracefully • Error handling ECS 152 B Xin Liu
Sockets, protocols, ports Applications sockets binding ports TCP/UDP IP ECS 152 B Xin Liu
Creating a Socket int socket(int family, int type, int proto); • family specifies the protocol family (PF_INET for TCP/IP). • type specifies the type of service (SOCK_STREAM, SOCK_DGRAM). – SOCK_STREAM: reliable byte-stream – SOCK_DGRAM: best-effort datragram • protocol specifies the specific protocol – IPPROTO_TCP, IPPROTO_UDP – 0 means the default ECS 152 B Xin Liu
socket() • The socket() system call returns a socket descriptor (nonnegative) or -1 on error. • socket() allocates resources needed for a communication endpoint - but it does not deal with endpoint addressing. ECS 152 B Xin Liu
Unix Descriptor Table Data structure for file 0 0 1 2 3 4 ECS 152 B Data structure for file 1 Data structure for file 2 Xin Liu
Socket Descriptor Data Structure Descriptor Table 0 1 2 3 4 ECS 152 B Family: PF_INET Service: SOCK_STREAM Local IP: 111. 22. 3. 4 Remote IP: 123. 45. 6. 78 Local Port: 2249 Remote Port: 3726 Xin Liu
Necessary Background Information: POSIX data types int 8_t uint 8_t int 16_t uint 16_t int 32_t uint 32_t ECS 152 B signed 8 bit int unsigned 8 bit int signed 16 bit int unsigned 16 bit int signed 32 bit int unsigned 32 bit int Xin Liu
More POSIX data types sa_family_t socklen_t in_addr_t in_port_t ECS 152 B address family length of struct IPv 4 address IP port number Xin Liu
Generic socket addresses struct sockaddr { unsigned short sa_family; char sa_data[14]; }; • sa_family specifies the address type – AF_INET (Internet protocol address family) • sa_data specifies the address value. ECS 152 B Xin Liu
struct sockaddr_in (IPv 4) struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; struct in_addr { unsigned long }; ECS 152 B s_addr; Xin Liu
Address • sockaddr_in is another view of the data in sockaddr structure. • Fill, cast, and pass to the socket functions • Socket functions look at the sa_family • PF_INET and AF_INET: – allow maximum flexibility, e. g. , the same protocol family can use different address families. ECS 152 B Xin Liu
sockaddr_in sa_family AF_INET sin_port sin_addr sa_data sin_zero ECS 152 B Xin Liu
Network Byte Order • All values stored in a sockaddr_in must be in network byte order. – sin_port – sin_addr ECS 152 B a TCP/IP port number. an IP address. Xin Liu
Byte Ordering • Big-Endian machine: the most significant byte has the lowest address – Motorola 68000 and Sparc • Little-Endian machine: the least significant byte has the lowest address – Intel x 86 and DEC Alpha architectures • Network byte order: Big-Endian ECS 152 B Xin Liu
Network Byte Order Functions ‘h’ : host byte order ‘n’ : network byte order ‘s’ : short (16 bit) ‘l’ : long (32 bit) uint 16_t htons(uint 16_t); uint 16_t ntohs(uint_16_t); uint 32_t htonl(uint 32_t); uint 32_t ntohl(uint 32_t); ECS 152 B Xin Liu
Byte ordering • Whenever sending a multibyte, binary value from one machine to another machine • Passing values to an API function in sockaddr structure myaddr. sin_port = htons( portnum ); myaddr. sin_addr = htonl( ipaddress); • Receiver needs to convert before using the values ECS 152 B Xin Liu
Allocation of Port Numbers 1 BSD reserved ports 1 5000 5001 1023 1024 65535 BSD servers (nonprivilidged) BSD ephemeral (short lived) ports 1023 1024 IANA well-known ports 49151 49152 IANA dynamic or private IANA registered ports IANA: Internet Assigned Numbers Authority ECS 152 B Xin Liu 65535
Assigning an address to a socket • The bind() system call is used to assign an address to an existing socket. int bind( int sockfd, const struct sockaddr *myaddr, int addrlen); • bind returns 0 if successful or -1 on error. ECS 152 B Xin Liu
bind() • calling bind() assigns the address specified by the sockaddr structure to the socket descriptor. • You can give bind() a sockaddr_in structure: bind( mysock, (struct sockaddr*) &myaddr, sizeof(myaddr) ); ECS 152 B Xin Liu
bind() Example int mysock, err; struct sockaddr_in myaddr; memset(&myaddr, 0, sizeof(myaddr)); mysock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); myaddr. sin_family = AF_INET; myaddr. sin_port = htons( portnum ); myaddr. sin_addr = htonl( ipaddress); err=bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr)); if (err<0) Handle. Error; ECS 152 B Xin Liu
Uses for bind() • There a number of uses for bind(): – Server would like to bind to a well known address (port number). – Client can bind to a specific port. – Client can ask the O. S. to assign any available port number. ECS 152 B Xin Liu
Framing • TCP: stream service, does not conserve boundary • Framing: formatting the information s. t. the receiver can parse message – The beginning and end of the message, boundaries between fields within the message – Fixed message size – Using delimiter ECS 152 B Xin Liu
TCP Clients • • ECS 152 B Create a socket, socket() Establish connection to the server, connect() Communicate, send() and recv() Close connection, close() Xin Liu
connect() int connect( int sockfd, const struct sockaddr *foreignserver, socklen_t addrlen); sockfd is an already created TCP socket. foreignserver contains the address of the server (IP Address and TCP port number) connect() returns 0 if OK, -1 on error ECS 152 B Xin Liu
Client Code • TCP clients can call connect() which: – takes care of establishing an endpoint address for the client socket. • don’t need to call bind first, the O. S. will take care of assigning the local endpoint address (TCP port number, IP address). – Attempts to establish a connection to the specified server. • 3 -way handshake ECS 152 B Xin Liu
Send/receive in a TCP socket int send(int sd, const void *buf, unsigned int messagelen, int flags); int recv(int sd, void *buf, unsigned int bufferlen, int flags); sd: socket descriptor flags: change default behavior, 0 default Return: -1 for error ECS 152 B Xin Liu
TCP Server • Creating a passive mode (server) socket. • Establishing an application-level connection. • send/receive data. • Terminating a connection. ECS 152 B Xin Liu
listen() int listen( int sockfd, int backlog); sockfd is the TCP socket (already bound to an address) backlog is the number of incoming connections the kernel should be able to keep track of (queue for us). listen() returns -1 on error (otherwise 0). ECS 152 B Xin Liu
accept() int accept( int sockfd, struct sockaddr* cliaddr, socklen_t *addrlen); sockfd is the passive mode TCP socket. cliaddr is a pointer to allocated space. addrlen is a value-result argument – must be set to the size of cliaddr – on return, will be set to be the number of used bytes in cliaddr. ECS 152 B Xin Liu
accept() return value accept() returns a new socket descriptor (small positive integer) or -1 on error. After accept returns a new socket descriptor, I/O can be done using the send()& recv() system calls. ECS 152 B Xin Liu
Terminating a TCP connection • Either end of the connection call the close() system call. • If the other end has closed the connection, and there is no buffered data, reading from a TCP socket returns 0 to indicate EOF. ECS 152 B Xin Liu
Metaphor for Good Relationships To succeed in relationships: ) ( d in b – you need to establish your own identity. t() p e c c a – you need to be open & accepting. – you need to establish contacts. connect() – you need to take things as they come, not as you expect them. read might return 1 byte ECS 152 B Xin Liu
A simple client example /* Create a reliable, stream socket using TCP */ if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) Die. With. Error("socket() failed"); /* Construct the server address structure */ memset(&echo. Serv. Addr, 0, sizeof(echo. Serv. Addr)); echo. Serv. Addr. sin_family = AF_INET; echo. Serv. Addr. sin_addr. s_addr = inet_addr(serv. IP); echo. Serv. Addr. sin_port = htons(echo. Serv. Port); ECS 152 B Xin Liu
/* Establish the connection to the echo server */ if (connect(sock, (struct sockaddr *) &echo. Serv. Addr, sizeof(echo. Serv. Addr)) < 0) Die. With. Error("connect() failed"); echo. String. Len = strlen(echo. String); /* Determine input length */ /* Send the string to the server */ if (send(sock, echo. String. Len, 0) != echo. String. Len) Die. With. Error("send() sent a different number of bytes than expected"); ECS 152 B Xin Liu
A simple TCP Server /* Create socket for incoming connections */ if ((serv. Sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) Die. With. Error("socket() failed"); /* Construct local address structure */ memset(&echo. Serv. Addr, 0, sizeof(echo. Serv. Addr)); /* Zero out structure */ echo. Serv. Addr. sin_family = AF_INET; /* Internet address family */ echo. Serv. Addr. sin_addr. s_addr = htonl(INADDR_ANY); /* Any incoming interface */ echo. Serv. Addr. sin_port = htons(echo. Serv. Port); /* Local port */ /* Bind to the local address */ if (bind(serv. Sock, (struct sockaddr *) &echo. Serv. Addr, sizeof(echo. Serv. Addr)) < 0) Die. With. Error("bind() failed"); /* Mark the socket so it will listen for incoming connections */ if (listen(serv. Sock, MAXPENDING) < 0) Die. With. Error("listen() failed"); ECS 152 B Xin Liu
for (; ; ) /* Run forever */ { /* Set the size of the in-out parameter */ clnt. Len = sizeof(echo. Clnt. Addr); /* Wait for a client to connect */ if ((clnt. Sock = accept(serv. Sock, (struct sockaddr *) &echo. Clnt. Addr, &clnt. Len)) < 0) Die. With. Error("accept() failed"); /* clnt. Sock is connected to a client! */ printf("Handling client %sn", inet_ntoa(echo. Clnt. Addr. sin_addr)); Handle. TCPClient(clnt. Sock); } /* NOT REACHED */ } ECS 152 B Xin Liu
Under the hook • • • ECS 152 B Data structure Buffering and TCP Deadlock Performance TCP socket life cycle Xin Liu
Data structure of a TCP socket Socket descriptor recved queue sent queue protocol state (closed) Local IP: 111. 22. 3. 4 Remote IP: 123. 45. 6. 78 Local Port: 2249 Remote Port: 3726 ECS 152 B Xin Liu
UDP Local IP: 111. 22. 3. 4 Remote IP: 123. 45. 6. 78 Local Port: 2249 Remote Port: 3726 ECS 152 B Xin Liu
Buffering and TCP Cannot assume correspondence between send()s and recv()s Ex: connect(s, …); send(s, buffer 0, 1000, 0); … send(s, buffer 1, 2000, 0); … send(s, buffer 2, 5000, 0); … close(s) ECS 152 B Xin Liu
Queues • Send. Q • Recv. Q • Delivered ECS 152 B Xin Liu
Deadlock • N>Send. Q+Recv. Q • Notes: design the protocol carefully to avoid simultaneous send() in both directions. ECS 152 B Xin Liu
Performance • Effective size of send() is limited by Send. Q size. • Can change using SO_RCVBUF & SO_SNDBUF socket options. ECS 152 B Xin Liu
TCP Socket life cycle • Client: – Connecting • Server: – Binding – Listen – Accept ECS 152 B Xin Liu
- Slides: 47