Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.

Similar presentations


Presentation on theme: "Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns."— Presentation transcript:

1 Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns a small integer (socket descriptor) that the program uses to reference the socket –Application program can then use read and write system calls on the socket –Program closes the socket when finished using it

2 Creating a Socket The socket system call: int sd; // socket descriptor int pf; // protocol family (one of PF_INET, // PF_PUP, PF_APPLETALK, // PF_UNIX, etc.) int type;// type of service (one of SOCK_RAW, // SOCK_DGRAM, SOCK_STREAM) int protocol;// specific protocol in pf sd = socket(pf, type, protocol)// create a new socket

3 Binding a Socket to an Internet Source Address (cont) The bind system call: int sd; // socket descriptor struct sockaddr_in addr;// structure specifying source // address int len;// length (in bytes) of // addr struct bind(sd,addr,len)// bind socket to source IP // address

4 Connecting a Socket to a Destination Address The connect system call: int sd; // socket descriptor struct sockaddr addr;// structure specifying dest addr int len;// length (in bytes) of // addr struct connect(sd,addr,len)// connect socket to // dest address Can also use a sockaddr_in struct for dest address

5 Sending Data Through a Socket The write system call: int sd; // socket descriptor void *buffer;// address of the data to be sent int len;// number of bytes to send write(sd,buffer,len)// send data through socket

6 Receiving Data Through a Socket The read system call: int sd; // socket descriptor void *buffer;// address in memory at which // to store the data int len;// number of bytes to receive read(sd,buffer,len)// receive data through socket

7 Closing a Socket The close system call: int sd; // socket descriptor close(sd)// close socket

8 Datagram Socket Example: Receiver #include main() { int sock, len; struct sockaddr_in name; char buf [1024]; sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) exit(-1); name.sin_family = AF_INET; name.sin_addr.s_addr = INADDR_ANY; name.sin_port = 0; if (bind(sock, (struct sockaddr *) &name, sizeof(name))) exit(-1); len = sizeof(name); if (getsockname(sock, (struct sockaddr *) &name, &len)) exit(-1); printf("Receiver listening on port %d\n",ntohs(name.sin_port)); if (read(sock, buf, 1024) < 0) exit(-1); printf("%s\n",buf); close(sock); }

9 Datagram Socket Example: Sender #include main(int argc, char **argv) { int sock; struct sockaddr_in name; struct hostent *hp, *gethostbyname(); if (argc != 3) exit(-1); sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) exit(-1); hp = gethostbyname("prime.cs.ohiou.edu"); if (hp == 0) exit(-1); bcopy(hp->h_addr, &name.sin_addr, hp->h_length); name.sin_family = AF_INET; name.sin_port = htons(atoi(argv[1])); if (connect(sock, (struct sockaddr *) &name, sizeof(name))) exit(-1); if (write(sock, argv[2], (strlen(argv[2])+1)) < 0) exit(-1); close(sock); }

10 Obtaining Local Socket Addresses The getsockname system call: int sd; // socket descriptor struct sockaddr *addr; // address structure to be filled int *len;// pointer to integer that will // contain the length of the // address getsockname(sd, addr, len); // obtain local socket address

11 Obtaining and Setting Socket Options The getsockopt system call: int sd; // socket descriptor int level; // option for socket or protocol? int optionid;// which specific option? void *optionval;// where to place the requested // value int *len; // length of the optionval getsockopt(sd, level, optionid, optionval, len); // obtain // socket opt

12 Obtaining and Setting Socket Options (cont) Values for level (from ): –SOL_SOCKEToption for the socket –IPPROTO_IPoption for IP –IPPROTO_ICMPoption for ICMP –IPPROTO_TCPoption for TCP –IPPROTO_UDPoption for UDP

13 Obtaining and Setting Socket Options (cont) Values for optionid (from ): –SO_TYPEsocket type –SO_SNDBUFsend buffer size –SO_RCVBUFreceive buffer size –SO_DEBUGdebugging info available? –SO_ACCEPTCONNsocket listening enabled? –SO_BROADCASTbroadcast supported? –SO_REUSEADDRaddress reuse allowed? –SO_KEEPALIVEkeep alive after close?

14 Obtaining and Setting Socket Options (cont) The setsockopt system call: int sd; // socket descriptor int level; // option for socket or protocol? int optionid;// which specific option? void *optionval;// option value int *len; // length of the option value setsockopt(sd, level, optionid, optionval, len); // set option // value

15 Socket Options for Servers The listen system call: int sd; // socket descriptor int length; // length of request queue listen(sd, length)// set socket request queue // length Can only be used for SOCK_STREAM sockets

16 Servers: Accepting Connections The accept system call: int sd; // socket descriptor struct sockaddr *name;// address of client int *len; // length of address struct newsock = accept(sd, addr, len) // accept connection A new socket is created that connects to the client Server handles request, sends reply, closes newsock

17 Servers That Provide Multiple Services The select system call: int ndesc; // check descriptors 0...ndesc-1 void *indesc;// descriptors to check for input void *outdesc; // descriptors to check for output void *excdesc;// descriptors to check for exceptions int *timeout;// how long to wait for a connection nready = select(ndesc, indesc, outdesc, excdesc, timeout) // determine which descriptors are // ready for I/O

18 Servers That Provide Multiple Services (cont) The select system call: nready = select(ndesc, indesc, outdesc, excdesc, timeout) Returns the number of descriptors from the specified set that are ready for I/O A process can use select to communicate over more than one socket at a time Typically each socket provides a distinct service

19 Miscellaneous (Useful) System Calls The gethostname system call: char *name;// buffer to store name int length;// size of buffer in bytes gethostname(name, length)// get name of host Defined in include file Process can learn host it’s running on

20 Miscellaneous (Useful) System Calls (cont) The network byte order conversion routines: –Network-to-host (short), ntohs, convert a short int from network byte order to host byte order –Network-to-host (long), ntohl, convert a long int from network byte order to host byte order –Host-to-network (short), htons, convert a short int from network byte order to host byte order –Host-to-network (long), htonl, convert a long int from network byte order to host byte order

21 Miscellaneous (Useful) System Calls (cont) Testing htons: #include main(int argc, char **argv) { if (argc != 2) exit(-1); printf("%d\n",atoi(argv[1])); printf("%d\n",htons(atoi(argv[1]))); }

22 Miscellaneous (Useful) System Calls (cont) The gethostbyname system call: struct hostent *h;// hostent structure char *name;// host name h = gethostbyname(name);// fill in hostent with // info about name

23 Miscellaneous (Useful) System Calls (cont) The hostent structure (defined in ): struct hostent { char *h_name;/* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses from name server */

24 OHCE Server Example Check that program is running on the proper host Create a socket on which the server will receive requests Bind the socket to the well-known port for the ohce service Loop forever –Receive request and process it –Send reply to port and machine specified in the client’s request

25 OHCE Client Example Create a socket on which to receive the server’s reply Bind it to some port on the local machine Create a socket on which to send a request to the server Bind it to the port the server’s listening on Create a request message and send it to the server Wait for a reply and print out the result

26 Summary The client-server model is widely-used for application interaction over a TCP/IP internet –Server: a program that offers a service that can be reached over a network Examples: web server, file server –Client: a program that requests a service from a server Examples: ping, browser Application programs typically interact with the networking software by using sockets and system calls: –Socket – create a socket –Bind – bind a socket to a port –Read/write – get/put data in a socket –Etc.


Download ppt "Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns."

Similar presentations


Ads by Google