Socket Programming a Primer Socket to me Why does one need sockets application sockets network network protocol Feb 23 2001 EE122 UCB 2 So what exactly does a socket do It is an API between applications and network protocol software Functions it provides Define an end point for communication Initiate and accept a connection Send and receive data Terminate a connection gracefully Supports multiple protocol families Examples Unix inter process communication TCP IP Only Internet sockets will be covered in this lecture Feb 23 2001 EE122 UCB 3 Types of Sockets Two different types of sockets stream vs datagram Stream socket a k a connection oriented socket It provides reliable connected networking service Error free no out of order packets uses TCP applications telnet http Datagram socket a k a connectionless socket It provides unreliable best effort networking service Packets may be lost may arrive out of order uses UDP applications streaming audio video Feb 23 2001 EE122 UCB 4 How should one define a socket To define an end point of communication one needs to specify the family of protocol it uses Internet vs others addressing information IP address port number the type of service it provides stream vs datagram Done in three steps create a socket define address and port number associate address with the socket Feb 23 2001 EE122 UCB 5 How to create a socket include include sys types h sys socket h int sock sock socket AF INET SOCK STREAM 0 for stream sock socket AF INET SOCK DGRAM 0 for datagram Notice that the socket descriptor is just a regular int So it has the same usage as a file descriptor in Unix Feb 23 2001 EE122 UCB 6 How to define address struct sockaddr u short sa family char sa data 14 struct in addr u long s addr struct sockaddr in short sin family u short sin port struct in addr sin addr char sin zero 8 WARNING Don t forget to convert byte orders htons htonl ntohs ntohl Feb 23 2001 EE122 UCB 7 Bind a Socket bind associate a socket descriptor with an address putting everything together int bind int sockfd struct sockaddr addr int len int sockfd struct sockaddr in addr sockfd socket AF INET SOCKE STREAM 0 addr sin family AF INET addr sin port htons 5000 0 randomly assigned by OS addr sin addr s addr htonl INADDR ANY local address bzero addr sin zero 8 pad zeros bind sockfd struct sockaddr addr sizeof struct sockaddr Feb 23 2001 EE122 UCB 8 How to convert addresses You also need to define address for the other end If you know its IP address addr sin addr s addr inet addr 128 32 138 240 If you know its name only need to perform a DNS lookup struct hostent gethostbyname char name struct hostent char h name char h aliases int h addrtype int h length char h addr list define h addr h addr list 0 Feb 23 2001 struct hostent h struct in addr inad h gethostbyname cory eecs inad struct in addr h h addr addr sin addr s addr inad EE122 UCB 9 Using a Datagram Socket Sending data int sendto int sockfd void msg int msg len u short flags struct sockaddr dest int dest len Receiving data int recvfrom int sockfd void msg int msg len u short flags struct sockaddr src int src len Feb 23 2001 EE122 UCB 10 Using a Stream Socket establishing a connection server client connect listen accept int listen int sockfd int backlog int connect int sockfd struct sockaddr addr int addr len int accept int sockfd void addr int addrlen Feb 23 2001 EE122 UCB 11 Using a Stream Socket contd Sending data int send int sockfd void msg int msg len u short flags Receiving data int recv int sockfd void msg int msg len u short flags Notice that no address is required Feb 23 2001 EE122 UCB 12 A Quick Summary Datagram server socket to create socket bind to a receiving port recvfrom sendto bind to any port recvfrom sendto client socket to create socket Feb 23 2001 EE122 UCB 13 A Quick Summary Stream server socket bind to a receiving port listen to socket accept connection send recv client socket Feb 23 2001 bind to any port connect To server EE122 UCB send recv 14 Sample Codes Datagram Client include include include include define sys types h sys socket h netinet in h netdb h Bfsize 1024 main int argc char argv int sock struct sockaddr in client server struct hostent host gethostname sock socket AF INET SOCK DGRAM 0 open socket client sin family AF INET client sin addr s addr htonl INADDR ANY local addr client sin port htons 0 any port bind sock struct sockaddr client sizeof client Feb 23 2001 EE122 UCB 15 Sample Code Datagram Client host gethostbyname argv 1 get host name memcpy char server sin addr char host h addr host h length server sin family AF INET server sin port htons atoi argv 2 sendto sock msg sizeof msg 0 struct sockaddr server sizeof server close sock Don t forget error handling when calling these functions in your programs Feb 23 2001 EE122 UCB 16 Sample Code Datagram Server include include include include define sys types h sys socket h netinet in h netdb h Bfsize 1024 main int argc char argv int sock length count struct sockaddr in server client char buffer Bfsize sock socket AF INET SOCK DGRAM 0 server sin family AF INET server sin addr s addr htonl INADDR ANY server sin port htons atoi argv 1 listening port bind sock struct sockaddr server sizeof server Feb 23 2001 EE122 UCB 17 Sample Code Datagram Server count recvfrom sock buffer Bfsize 0 struct sockaddr client length printf s n buffer close sock Note that we don t have to define client s address here because the server can receive from any one on sock After return from recvfrom client contains sender s address information Feb 23 2001 EE122 UCB 18 Sample Code Stream Client include include include include define sys types h sys socket h netinet in h netdb h msg hello ee122 main int argc char argv int sock struct sockaddr in client server struct hostent host gethostname host gethostbyname argv 1 memcpy char server sin addr char host h addr host h length server sin family AF INET server sin port htons atoi argv 2 no bind is needed sock socket AF INET SOCK STREAM 0 connect sock struct sockaddr server sizeof server Feb 23 2001 EE122 UCB 19 Sample Code Stream Client send sock msg sizeof msg 0 close sock Feb 23 2001 EE122 UCB 20 Sample Code Stream Server include include include include define sys types h sys socket h netinet in h netdb h Bfsize 1024 main int argc char argv int new sock sock length count struct sockaddr in server client struct hostent host gethostbyname char buffer Bfsize sock socket AF INET SOCK STREAM 0 server sin family AF INET server sin
View Full Document