Download presentation
Presentation is loading. Please wait.
Published byOswald Stephens Modified over 8 years ago
1
OPERATING SYSTEMS COURSE THE HEBREW UNIVERSITY SPRING 2015 1 Sockets
2
Overview Files in POSIX-based OS TCP and UDP Transport Protocols Sockets – concept Technical Material Socket Addresses Address Related System Calls Sockets programing 2
3
File Access Calls 3 POSIX-based operating systems treat a file in a very general sense File is an object that can be written to, or read from, or both. A file has certain attributes, including access permissions and type. Regular file Symbolic link Directory Character devices Block devices Pipes FIFO special file Sockets NULL 5 4 3 0 STDIN 1 STDOUT 2STDERR FDs Table
4
File Access Calls – open and close 4 “open” depends on the type of the file, different command for regular files, pipes, sockets, etc. However, open always returns file descriptor (fd). close – close a file descriptor err = close(int fd); The close() function shall deallocate the file descriptor indicated by fd. To deallocate means to make the file descriptor available for return by subsequent calls to open() or other functions that allocate file descriptors. When all file descriptors associated with an open file description have been closed, the open file description shall be freed.
5
File Access Calls – read and write 5 read – read from a file b_read = read(int fd, void *buf, int nbyte); The read() function shall attempt to read nbyte bytes from the file associated with the open file descriptor, fd, into the buffer pointed to by buf. The return value shall be a non-negative integer indicating the number of bytes actually read. write – write to a file b_written = write(int fd, void *buf, int nbyte); The write() function shall attempt to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor fd. The return value shall be a non-negative integer indicating the number of bytes actually written.
6
Overview Files in POSIX-based OS TCP and UDP Transport Protocols Sockets – concept Technical Material Socket Addresses Address Related System Calls Sockets programing 6
7
Transport Layer Internet Layer (IP Protocol) Responsible for end to end transmission Unreliable - Packets might be lost, corrupted, duplicated, delivered out of order Supplies End-to-end communication services for applications. Main protocols TCP UDP 7
8
User Datagram Protocol (UDP) Thin layer on top of IP Adds packet length + checksum Guard against corrupted packets Also source and destination ports Ports are used to associate a packet with a specific application at each end Still unreliable: Duplication, loss, out-of-orderness possible Destination PortSource Port Application data 0 16 31 ChecksumLength 8
9
Transmission Control Protocol (TCP) Reliable stream transport Two ends communicate to agree on details Connection oriented + Buffering Takes care of Lost packets Out of order Duplicates 9 SourceDestination Packet 1 Ack 1 Packet 2 Ack 2 Packet 3
10
Multiplexing/demultiplexing 10 application transport network link physical P1 application transport network link physical application transport network link physical P2 P3 P4 P1 host 1 host 2 host 3 = process= socket Delivering received segments to correct socket Demultiplexing at rcv host: Gathering data from multiple sockets, enveloping data with header (later used for demultiplexing) Multiplexing at send host:
11
How demultiplexing works Host receives IP datagrams Each datagram has source IP address, destination IP address Each datagram carries 1 transport-layer segment Each segment has source, destination port number Host uses IP addresses & port numbers to direct segment to appropriate socket source port #dest port # 32 bits application data (message) other header fields TCP/UDP segment format 11
12
Connectionless demultiplexing Create sockets with port numbers: DatagramSocket mySocket1 = new DatagramSocket(12534); DatagramSocket mySocket2 = new DatagramSocket(12535); UDP socket identified by two-tuple: (dest IP address, dest port number) When host receives UDP segment, OS do the following: Checks destination port number in segment Directs UDP segment to socket with that port number IP datagrams with different source IP addresses and/or source port numbers directed to same socket 12
13
Connectionless demux (cont) DatagramSocket serverSocket = new DatagramSocket(6428); Client IP:B P2 client IP: A P1 P3 server IP: C SP: 6428 DP: 9157 SP: 9157 DP: 6428 SP: 6428 DP: 5775 SP: 5775 DP: 6428 SP provides “return address” 13
14
Connection-oriented demux TCP socket identified by 4- tuple: Source IP address Source port number Dest IP address Dest port number Receiving host uses all four values to direct segment to appropriate socket Server host may support many simultaneous TCP sockets: Each socket identified by its own 4-tuple Web servers have different sockets for each connecting client Non-persistent HTTP will have different socket for each request 14
15
Connection-oriented demux (cont) Client IP:B P1 client IP: A P1P2P4 server IP: C SP: 9157 DP: 80 SP: 9157 DP: 80 P5P6P3 D-IP:C S-IP: A D-IP:C S-IP: B SP: 5775 DP: 80 D-IP:C S-IP: B 15
16
Connection-oriented demux: Threaded Web Server Client IP:B P1 client IP: A P1P2 server IP: C SP: 9157 DP: 80 SP: 9157 DP: 80 P4 P3 D-IP:C S-IP: A D-IP:C S-IP: B SP: 5775 DP: 80 D-IP:C S-IP: B 16
17
Overview Files in POSIX-based OS TCP and UDP Transport Protocols Sockets – concept Technical Material Socket Addresses Address Related System Calls Sockets programing 17
18
Socket programming Socket API Explicitly created, used, released by applications Client/server paradigm Two types of transport service via socket API: – unreliable datagram – reliable, byte stream- oriented a host-local, application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application process socket Goal: learn how to build client/server application that communicate using sockets 18
19
Socket-Programming using TCP Socket: a door between application process and end-transport protocol (UCP or TCP) TCP service: reliable transfer of bytes from one process to another process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server internet 19
20
Socket programming with TCP Client must contact server server process must first be running server must have created socket (door) that welcomes client’s contact Client contacts server by: creating client-local TCP socket specifying IP address and port number of server process When client creates socket: client TCP establishes connection to server TCP When contacted by client, server TCP creates new socket to communicate with client – allows server to talk with multiple clients – source port numbers used to distinguish clients TCP provides reliable, in-order transfer of bytes between client and server application viewpoint 20
21
Streams 21 A stream is a sequence of characters that flow into or out of a process. An input stream is attached to some input source for the process, e.g. keyboard or socket. An output stream is attached to an output source, e.g. monitor or socket.
22
Socket programming with TCP Example client-server app: 1) client reads line from standard input ( inFromUser stream), sends to server via socket ( outToServer stream) 2) server reads line from socket 3) server converts line to uppercase, sends back to client 4) client reads, prints modified line from socket ( inFromServer stream) Client process client TCP socket 22
23
Client/server socket interaction: TCP wait for incoming connection request connectionSocket = welcomeSocket.accept() create socket, port= x, for incoming request: welcomeSocket = ServerSocket() create socket, connect to hostid, port= x clientSocket = Socket() close connectionSocket read reply from clientSocket close clientSocket Server (running on hostid ) Client send request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup 23
24
Socket programming with UDP Output: sends packet (TCP sent “byte stream”) Input: receives packet (TCP received “byte stream”) Client process client UDP socket 24
25
Socket programming with UDP UDP: no “connection state” between client and server No handshaking Sender explicitly attaches IP address and port of destination to each packet Server must extract IP address, port of sender from received packet UDP: transmitted data may be received out of order, or lost application viewpoint UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server 25
26
Client/server socket interaction: UDP close clientSocket Server (running on hostid ) read reply from clientSocket create socket, clientSocket = DatagramSocket() Client Create, address ( hostid, port=x, send datagram request using clientSocket create socket, port= x, for incoming request: serverSocket = DatagramSocket() read request from serverSocket write reply to serverSocket specifying client host address, port number 26
27
Overview Files in POSIX-based OS Transport Layer including TCP and UDP Protocols Sockets – concept Technical Material Socket Addresses Address Related System Calls Sockets programing 27
28
struct sockaddr struct sockaddr { unsigned short sa_family; char sa_data[14]; }; Address family in this presentation: AF_INET Contains a destination address and port number for the socket. 28
29
struct sockaddr_in struct sockaddr_in { short int sin_family; unsigned short int sin_port; struct in_addr sin_addr; unsigned char sin_zero[8]; }; struct in_addr { uint32_t s_addr; }; This structure makes it easy to reference elements of the socket address. 29
30
struct sockaddr_in A pointer to a struct sockaddr_in can be cast to a pointer to a struct sockaddr and vice-versa. Note that sin_zero should be set to all zeros with the function memset(). sin_family corresponds to sa_family in a sockaddr and should be set to " AF_INET ". sin_port and sin_addr must be in Network Byte Order! 30
31
Structs and Data Handling There are two byte orderings: Most significant byte first. Least significant byte first. In order to convert "Host Byte Order“ to “Network Byte Order”, you have to call a function. 31
32
Big\Little Endian Little-endian is popular (though not universal) among microprocessors Big-endian is the most common convention in data networking 32
33
Conversion Functions There are two types that you can convert: short and long. These functions work for the unsigned variations as well. htons() - "Host to Network Short“ htonl() - "Host to Network Long“ ntohs() - "Network to Host Short“ ntohl() - "Network to Host Long“ Be portable! Remember: put your bytes in Network Byte Order before you put them on the network. 33
34
IP Addresses #include struct sockaddr_in my_addr; my_addr.sin_family = AF_INET; my_addr.sin_port = htons(3490); inet_aton("10.12.110.57", &(my_addr.sin_addr)); memset(&(my_addr.sin_zero), '\0', 8); inet_aton(), unlike practically every other socket related function, returns non-zero on success, and zero on failure. 34
35
Overview Files in POSIX-based OS Transport Layer including TCP and UDP Protocols Sockets – concept Technical Material Socket Addresses Address Related System Calls Sockets programing 35
36
getpeername int getpeername(int sockfd, struct sockaddr *addr, int *addrlen); Get the address of the other end of a connected stream socket Return value - 0 on success, -1 in case of an error sockfd - the FD of the connected stream socket. addr is a pointer to a struct sockaddr that will hold the information about the other side of the connection, addrlen indicates on the addr’s length. Should be initialized to sizeof(struct sockaddr). 36
37
Domain Name Service - gethostname DNS is a service which maps human-readable address (a.k.a. host names) to IP addresses. The function gethostname() returns the name of the computer that your program is running on. 37
38
Domain Name Service - gethostbyname The name can then be used to determine the IP address of your local machine #include struct hostent * gethostbyname(const char *name); returns a pointer to the filled struct hostent, or NULL on error. 38
39
struct hostent struct hostent { char *h_name; //Official name of the host char **h_aliases;//Alternate names. int h_addrtype; //usually AF_INET. int h_length; //length of each address. char **h_addr_list; //network addresses for the host in N.B.O. }; #define h_addr h_addr_list[0] 39
40
Example – getip program Demonstration of how to use gethostbyname struct hostent int main(int argc, char *argv[]) { struct hostent *h; if (argc != 2) { fprintf(stderr, "usage: getip address\n"); exit(1); } if ((h=gethostbyname(argv[1])) == NULL) { fprintf(stderr, "gethostbyname "); exit(1); } printf("Host name : %s\n", h- >h_name); printf("IP Address : %s\n", inet_ntoa(*((struct in_addr *)h->h_addr))); return 0; } 40
41
Overview Files in POSIX-based OS Transport Layer including TCP and UDP Protocols Sockets – concept Technical Material Socket Addresses Address Related System Calls Sockets programing 41
42
First Step - Creating a Socket A socket is used to allow one process to speak to another, very much like the telephone is used to allow one person to speak to another. First, you must create a socket to listen for connections. This is done by using socket() function. int socket(int domain, int type, int protocol); 42
43
Socket’s Domain Parameter The addressing format of a socket: – AF_UNIX addressing uses UNIX pathnames to identify sockets - these sockets are very useful for IPC between processes on the same machine. – AF_INET addressing uses Internet addresses which are four-byte numbers usually written as four decimal numbers separated by periods (such as 192.9.200.10). In addition to the machine address, there is also a port number which allows more than one AF_INET socket on each machine. AF_INET addresses are what we will deal with here, as they are the most useful and widely used. 43
44
Socket’s Type Parameter The type of the data in the socket. – SOCK_STREAM indicates that data will come across the socket as a stream of characters. – SOCK_DGRAM indicates that data will come in bunches (called datagrams). 44
45
Socket’s Protocol Parameter We use “0” value that choose the default protocol. Sometimes this is the only supported protocol. 45
46
Second Step – Binding an Address Second Step: give the socket an address to listen to This is just as you get a telephone number so that you can receive calls) using the bind() function. int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); 46
47
Third Step – Listening Sockets have the ability to queue incoming connection requests, which is a lot like having "call waiting" for your telephone. If you are busy handling a connection, the connection request will wait until you can deal with it. The listen() function is used to set the maximum number of requests (up to a maximum of five) that will be queued before requests start being denied. int listen(int sockfd, int backlog); 47
48
Connection establishment (1) This example demonstrates how we practically use the first three steps. int establish(unsigned short portnum) { char myname[MAXHOSTNAME+1]; int s; struct sockaddr_in sa; struct hostent *hp; memset(&sa, 0, sizeof(struct sockaddr_in)); gethostname(myname, MAXHOSTNAME); hp = gethostbyname(myname); if (hp == NULL) return(-1); sa.sin_family = hp->h_addrtype; memcpy(&sa.sin_addr, hp->h_addr, hp- >h_length); /* this is our host address */ sa.sin_port= htons(portnum); /* this is our port number */ … 48
49
Connection establishment (2) This example demonstrates how we practically use the first three steps. … if ((s= socket(AF_INET, SOCK_STREAM, 0)) < 0) /* create socket */ return(-1); if (bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) { close(s); return(-1); } listen(s, 3); /* max # of queued connects */ return(s); } 49
50
Fourth Step – Waiting for Calls After creating a socket to get calls, you must wait for calls to that socket using the accept() function. Calling accept() is analogous to picking up the telephone if it's ringing. Accept() returns a new socket which is connected to the caller. int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) 50
51
Accept connections wait for a connection to occur on a socket created with establish() int get_connection(int s) { int ns; /*socket of connection*/ if ((ns = accept(s,NULL,NULL)) < 0) return -1; return ns; } 51
52
The Client You now know how to create a socket that will accept incoming calls. How do you call it? As with the telephone, you must first have the phone before using it to call. You use the socket() function to do this. After getting a socket you use the connect() function to try to connect to a listening socket. int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); 52
53
Connect to a socket (1) First part, init the address int call_socket(char *hostname, unsigned short portnum) { struct sockaddr_in sa; struct hostent *hp; int a, s; if ((hp= gethostbyname (hostname)) == NULL) { return(-1); } memset(&sa,0,sizeof(sa)); sa.sin_family = hp->h_addrtype; memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length); sa.sin_port = htons((u_short)portnum); …. 53
54
Connect to a socket (2) Second part, connect to the server …. if ((s = socket(AF_INET, SOCK_STREAM,0)) < 0) return(-1); if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) { close(s); return(-1); } return(s); } 54
55
Sending and Reading Data Now that you have a connection between sockets you want to send data between them. The read() and write() functions are used to do this, just as they are for normal files. You don't get back the same number of characters that you asked for, so you must loop until you have read the number of characters that you want. 55
56
Read Data code int read_data(int s, char *buf, int n) { int bcount = 0; /* counts bytes read */ int br; /* bytes read this pass */ while (bcount < n) { /* loop until full buffer */ if ((br= read(s, buf, n-bcount)) > 0) { bcount += br; buf += br; } else if (br < 0) return(-1); } return(bcount); } 56
57
Server has multiple FD 57 The server may have multiple FD (sockets) One (or more) that listens to new connections FDs that created by accept() and are getting their service currently. To handle several FD, the server may 1. Create a single thread for each socket 2. Use Select()
58
Socket programming with UDP There are many references on the web. One that I like: www.abc.se/~m6695/udp.html 58
59
Server Side Client Side 1. socket(); 2. bind(); 3. listen(); 4. accept(); 5. send()/recv(); 1. socket(); 2. connect(); 3. send()/recv(); 59 Summary Stream Socket
60
Listener side Talker side 1. socket(); 2. bind(); 3. recvfrom(); 1. socket(); 2. sendto(); 60 Summary Datagram Socket
61
Listener side Talker side 1. socket(); 2. bind(); 3. recvfrom(); 1. socket(); 2. connect(); 3. send (); 61 Summary Datagram Socket By using connect(), talker can send \ receive to \ from a specific address. For this purpose, simple send() and recv() can be used.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.