DOC PREVIEW
Berkeley ELENG 122 - Socket Programming - a Primer

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Socket Programming: a PrimerWhy does one need sockets?So what exactly does a socket do?Types of SocketsHow should one define a socket?How to create a socket?How to define address?Bind a SocketHow to convert addresses?Using a Datagram SocketUsing a Stream SocketUsing a Stream Socket (contd)A Quick Summary: DatagramA Quick Summary: StreamSample Codes: Datagram ClientSample Code: Datagram ClientSample Code: Datagram ServerSlide 18Sample Code: Stream ClientSlide 20Sample Code: Stream ServerSlide 22Further ReadingSocket Programming: a PrimerSocket to me!Feb. 23, 2001 EE122, UCB 2Why does one need sockets? applicationnetwork protocolsocketsnetworkFeb. 23, 2001 EE122, UCB 3So 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 lectureFeb. 23, 2001 EE122, UCB 4Types 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 5How 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 socketFeb. 23, 2001 EE122, UCB 6How 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…Feb. 23, 2001 EE122, UCB 7How 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, ntohlFeb. 23, 2001 EE122, UCB 8Bind a Socket•bind( ): associate a socket descriptor with an address•putting everything togetherint 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 9How to convert addresses?•You also need to define address for the other end•If you know its IP addressaddr.sin_addr.s_addr=inet_addr(“128.32.138.240”);•If you know its name only:–need to perform a DNS lookupstruct 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;Feb. 23, 2001 EE122, UCB 10Using a Datagram Socket•Sending dataint sendto(int sockfd, void *msg, int msg_len, u_short flags, struct sockaddr *dest, int dest_len);•Receiving dataint recvfrom(int sockfd, void *msg, int msg_len, u_short flags, struct sockaddr *src, int src_len);Feb. 23, 2001 EE122, UCB 11Using a Stream Socket•establishing a connectionint 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 ()clientserverFeb. 23, 2001 EE122, UCB 12Using a Stream Socket (contd)•Sending dataint send(int sockfd, void *msg, int msg_len, u_short flags);•Receiving dataint recv(int sockfd, void *msg, int msg_len, u_short flags);•Notice that no address is required!Feb. 23, 2001 EE122, UCB 13A Quick Summary: Datagramsocket() tocreate socketbind() to areceiving portrecvfrom()sendto ()socket() tocreate socketbind() toany portrecvfrom()sendto ()serverclientFeb. 23, 2001 EE122, UCB 14A Quick Summary: Streamsocket()serverbind() to areceiving portlisten ()to socketbind() toany portconnect ()To serverclientaccept ()connectionsocket()send ()recv ()send ()recv ()Feb. 23, 2001 EE122, UCB 15Sample Codes: Datagram Client#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#define Bfsize 1024main(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 16Sample 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 17Sample Code: Datagram Server#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#define Bfsize 1024main(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 18Sample Code: Datagram Server count=recvfrom(sock, buffer, Bfsize, 0,


View Full Document

Berkeley ELENG 122 - Socket Programming - a Primer

Documents in this Course
Lecture 6

Lecture 6

22 pages

Wireless

Wireless

16 pages

Links

Links

21 pages

Ethernet

Ethernet

10 pages

routing

routing

11 pages

Links

Links

7 pages

Switches

Switches

30 pages

Multicast

Multicast

36 pages

Switches

Switches

18 pages

Security

Security

16 pages

Switches

Switches

18 pages

Lecture 1

Lecture 1

56 pages

OPNET

OPNET

5 pages

Lecture 4

Lecture 4

16 pages

Ethernet

Ethernet

65 pages

Models

Models

30 pages

TCP

TCP

16 pages

Wireless

Wireless

48 pages

Load more
Download Socket Programming - a Primer
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Socket Programming - a Primer and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Socket Programming - a Primer 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?