230 likes | 351 Views
Socket to me!. Socket Programming: a Primer. sockets. Why does one need sockets?. application. network. network protocol. 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
E N D
Socket to me! Socket Programming: a Primer
sockets Why does one need sockets? application network network protocol EE122, UCB
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 EE122, UCB
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, … EE122, UCB
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 EE122, UCB
How to create a socket? # include <sys/types.h> # include <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… EE122, UCB
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! struct in_addr { u_long s_addr; } htons, htonl, ntohs, ntohl How to define address? struct sockaddr { u_short sa_family; char sa_data[14]; } EE122, UCB
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)); EE122, UCB
struct hostent *h; struct in_addr *inad; h=gethostbyname(“cory.eecs”); inad=(struct in_addr *) h->h_addr; addr.sin-addr.s_addr=inad; 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] } EE122, UCB
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); EE122, UCB
connect( ) accept () Using a Stream Socket • establishing a connection server client listen () int listen(int sockfd, int backlog); int connect(int sockfd, struct sockaddr *addr, int addr_len); int accept(int sockfd, void *addr, int *addrlen ); EE122, UCB
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! EE122, UCB
A Quick Summary: Datagram server socket() to create socket bind() to a receiving port recvfrom() sendto () client recvfrom() sendto () socket() to create socket bind() to any port EE122, UCB
A Quick Summary: Stream server socket() bind() to a receiving port listen () to socket accept () connection send () recv () client connect () To server bind() to any port send () recv () socket() EE122, UCB
Sample Codes: Datagram Client #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define 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)); EE122, UCB
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! EE122, UCB
Sample Code: Datagram Server #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define 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)); EE122, UCB
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. Sample Code: Datagram Server count=recvfrom(sock, buffer, Bfsize, 0, (struct sockaddr *)&client,&length); printf("---> %s\n", buffer); close(sock); } EE122, UCB
Sample Code: Stream Client #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define 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)); EE122, UCB
Sample Code: Stream Client send(sock,msg,sizeof(msg),0); close(sock); } EE122, UCB
Sample Code: Stream Server #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define 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_addr.s_addr=htonl(INADDR_ANY); server.sin_port=htons(atoi(argv[1])); bind(sock, (struct sockaddr *)&server, sizeof(server)); listen(sock,1); EE122, UCB
Sample Code: Stream Server new_sock = accept(sock,(struct sockaddr *)&client,&length); count=recv(new_sock,buffer,Bfsize,0); printf("---> %s\n", buffer); close(new_sock); close(sock); } EE122, UCB
Further Reading • W. Richard Stevens. Unix Network Programming, Prentice Hall. • The Unix Socket FAQ. http://www.ibrado.com/sock-faq EE122, UCB