Download presentation
Presentation is loading. Please wait.
1
Socket Programming: a Primer Socket to me!
2
Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network
3
Feb. 23, 2001EE122, UCB3 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
4
Feb. 23, 2001EE122, UCB4 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, …
5
Feb. 23, 2001EE122, UCB5 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
6
Feb. 23, 2001EE122, UCB6 How to create a socket? # include 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…
7
Feb. 23, 2001EE122, UCB7 How to define address? struct sockaddr { u_short sa_family; char sa_data[14]; } struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; } struct in_addr { u_long s_addr; } WARNING: Don’t forget to convert byte orders! htons, htonl, ntohs, ntohl
8
Feb. 23, 2001EE122, UCB8 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));
9
Feb. 23, 2001EE122, UCB9 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] } struct hostent *h; struct in_addr *inad; h=gethostbyname(“cory.eecs”); inad=(struct in_addr *) h->h_addr; addr.sin-addr.s_addr=inad;
10
Feb. 23, 2001EE122, UCB10 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);
11
Feb. 23, 2001EE122, UCB11 Using a Stream Socket establishing a connection int listen(int sockfd, int backlog); int connect(int sockfd, struct sockaddr *addr, int addr_len); int accept(int sockfd, void *addr, int *addrlen ); listen () connect( ) accept () client server
12
Feb. 23, 2001EE122, UCB12 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!
13
Feb. 23, 2001EE122, UCB13 A Quick Summary: Datagram socket() to create socket bind() to a receiving port recvfrom() sendto () socket() to create socket bind() to any port recvfrom() sendto () server client
14
Feb. 23, 2001EE122, UCB14 A Quick Summary: Stream socket() server bind() to a receiving port listen () to socket bind() to any port connect () To server client accept () connection socket() send () recv () send () recv ()
15
Feb. 23, 2001EE122, UCB15 Sample Codes: Datagram Client #include #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));
16
Feb. 23, 2001EE122, UCB16 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!
17
Feb. 23, 2001EE122, UCB17 Sample Code: Datagram Server #include #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));
18
Feb. 23, 2001EE122, UCB18 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.
19
Feb. 23, 2001EE122, UCB19 Sample Code: Stream Client #include #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));
20
Feb. 23, 2001EE122, UCB20 Sample Code: Stream Client send(sock,msg,sizeof(msg),0); close(sock); }
21
Feb. 23, 2001EE122, UCB21 Sample Code: Stream Server #include #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);
22
Feb. 23, 2001EE122, UCB22 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); }
23
Feb. 23, 2001EE122, UCB23 Further Reading W. Richard Stevens. Unix Network Programming, Prentice Hall. The Unix Socket FAQ. http://www.ibrado.com/sock- faq
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.