TCP IP Sockets
TCP IP Sockets
TCP IP Sockets
Overview
Introduction
TCP/IP Basic
UNIX/C Sockets
UDP Sockets TCP Sockets Utility Functions
Summary
IP Address
32-bit identifier Identifies a host interface (not a host)
Transport Protocols
Best-effort is not sufficient !!!
Add services on top of IP User Datagram Protocol (UDP)
Data checksum Best-effort
Ports
Identifying the ultimate destination
IP addresses identify hosts Host has many applications Ports (16-bit identifier)
Application Port WWW 80 E-mail 25 Telnet 23
194.42.16.25
Application
byte stream Ill pass these to the app. It knows what to do.
TCP/IP
TCP/IP
Socket
How does one speak TCP/IP?
Sockets provides interface to TCP/IP Generic interface for many protocols
Socket Types
Socket
Identified by protocol and local/remote address/port Applications may refer to many sockets Socket abstraction
Berkeley Sockets (traditional)
Generic
multiple families address representation independence)
Sockets work with Unix I/O services just like files, pipes Sockets have special needs:
establishing a connection specifying communication endpoint addresses
TCP/IP Sockets
mySock = socket(family, type, protocol); TCP/IP-specific sockets
Family TCP PF_INET UDP SOCK_DGRAM IPPROTO_UDP Type SOCK_STREAM Protocol IPPROTO_TCP
Socket reference
File (socket) descriptor in UNIX Socket handle in WinSock
Generic
struct sockaddr { unsigned short sa_family; char sa_data[14]; }; 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 s_addr; };
IP Specific
/* Internet protocol (AF_INET) */ /* Port (16-bits) */ /* Internet address (32-bits) */ /* Not used */
sockaddr
Blob 8 bytes
sockaddr_in
Family
Not used
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
1. 2. 3. 4.
Client Create a TCP socket Establish connection Communicate Close the connection
1. 2. 3. 4.
Server Create a TCP socket Assign a port to socket Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection
Creating a Socket
int socket(int family, int type, int proto)
system call returns a socket descriptor(small integer); -1 on error allocates resources needed for a communication endpoint; does not deal with endpoint addressing
Example:
struct sockaddr_in sin; int s; s = socket(AF_INET, SOCK_DGRAM, 0); sin.sin_family = AF_INET; sin.sin_port = htons(9999); sin.sin_addr.s_addr = INADDR_ANY; bind(s, (struct sockaddr *)&sin, sizeof(sin));
use the socket library functions instead of the file I/O equivalents.
1. 2. 3. 4.
Client Create a TCP socket Establish connection Communicate Close the connection
1. 2. 3. 4.
Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection
1. 2. 3. 4.
Client Create a TCP socket Establish connection Communicate Close the connection
1. 2. 3. 4.
Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection
1. 2. 3. 4.
Client Create a TCP socket Establish connection Communicate Close the connection
1. 2. 3. 4.
Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection
1. 2. 3. 4.
Client Create a TCP socket Establish connection Communicate Close the connection
1. 2. 3. 4.
Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection
1. 2. 3. 4.
1. 2. 3. 4.
1. 2. 3. 4.
Client Create a TCP socket Establish connection Communicate Close the connection
1. 2. 3. 4.
Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection
1. 2. 3. 4.
Client Create a TCP socket Establish connection Communicate Close the connection
1. 2. 3. 4.
Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection
1. 2. 3. 4.
Client Create a TCP socket Establish connection Communicate Close the connection
1. 2. 3. 4.
Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection
1. 2. 3. 4.
Client Create a TCP socket Establish connection Communicate Close the connection
1. 2. 3. 4.
Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection
1. 2. 3. 4.
Client Create a TCP socket Establish connection Communicate Close the connection
1. 2. 3. 4.
Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection
1. 2. 3. 4.
Client Create a TCP socket Establish connection Communicate Close the connection
1. 2. 3. 4.
Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection
TCP Tidbits
Client must know the servers address and port Server only needs to know its own port No correlation between send() and recv() Client
send(Hello Bob)
Server
recv() -> Hello recv() -> Bob send(Hi ) send(Jane)
Closing a Connection
Echo Client
send(string)
Echo Server
close(client socket)
Byte-order Transformations
Byte ordering is a function of machine architecture
Intel: little-endian Sparc, PowerPC: big-endian Network order: big-endian
The byte order for the TCP/IP protocol suite is big endian.
u_long m =htonl(u_long m)
host-to-network byte order, 32 bit
ntohs(),htons()
short (16 bit)
Example:
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(9999); sin.sin_addr.s_addr = inet_addr;
Address transformation
Address transformation
unsigned int inet_addr(char *str)
str represents an IP address(dotted-quad notation); inet_addr will return it's equivalent 32-bit value in network byte order. This value can be passed into the sin_addr.s_addr field of a socketaddr_in structure -1 is returned if the string can not be interpreted
Struct hostent *hptr; hptr=gethostbyaddr(char *addr,int addrlen, int addrtype); Ex: gethostbyaddr(&addr, 4, AF_INET);
Others
Include files
#include <sys/types.h>; #include <sys/socket.h>; #include <netinet/in.h>; #include <arpa/inet.h>; #include <netdb.h>; #include <unistd.h>; #include <signal.h>; #include <stdio.h>; #include <fcntl.h>; #include <errno.h; #include <sys/time.h>; #include <stdlib.h>; #include <memory.h>;
Solaris:
cc my_socket_program.c -o my_socket_program -lsocket -lnsl
Programming tips
always check the return value for each function call consult the UNIX on-line manual pages ("man") for a complete description
Summary
TCP/IP basic
UNIX/C Sockets
socket() ; bind() ; connect() ; listen() ; accept() ; sendto() ; recvfrom(); send() ; recv() ; read() ; write(); some utility functions