OPERATING SYSTEMS COURSE THE HEBREW UNIVERSITY SPRING 2015 1 Sockets.

Slides:



Advertisements
Similar presentations
Transport Layer3-1 Transport Overview and UDP. Transport Layer3-2 Goals r Understand transport services m Multiplexing and Demultiplexing m Reliable data.
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Computer Communication Digital Communication in the Modern World Transport Layer Multiplexing, UDP
Chapter 3: Transport Layer
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
2: Application Layer1 Data Communication and Networks Lecture 12 Java Sockets November 30, 2006.
Transport Layer3-1 Transport Layer Our goals: r understand principles behind transport layer services: m multiplexing/demultipl exing m reliable data transfer.
Tutorial 8 Socket Programming
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
Lecture 8 Chapter 3 Transport Layer
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
Some slides are in courtesy of J. Kurose and K. Ross Review of Previous Lecture Electronic Mail: SMTP, POP3, IMAP DNS Socket programming with TCP.
3-1 Transport services and protocols r provide logical communication between app processes running on different hosts r transport protocols run in end.
8-1 Transport Layer Our goals: r understand principles behind transport layer services: m multiplexing/demultipl exing m reliable data transfer m flow.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
2: Application Layer 1 Socket Programming TCP and UDP.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
Review: –What is AS? –What is the routing algorithm in BGP? –How does it work? –Where is “policy” reflected in BGP (policy based routing)? –Give examples.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Data Communications and Computer Networks Chapter 3 CS 3830 Lecture 12 Omar Meqdadi Department of Computer Science and Software Engineering University.
CS 1652 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights Reserved Jack Lange.
Network LayerII-1 RSC Part III: Transport Layer 1. Basic Concepts Redes y Servicios de Comunicaciones Universidad Carlos III de Madrid These slides are,
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
Transport Layer 3-1 Chapter 3 Transport Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012 Part.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Transport Layer3-1 Chapter 3: Transport Layer Our goals: r understand principles behind transport layer services: m multiplexing/demultipl exing m reliable.
Transport Layer1 Ram Dantu (compiled from various text books)
Lecture91 Administrative Things r Return homework # 1 r Review some problems in homework # 1 r Questions about grading? Yona r WebCT for CSE245 is working!
CS 3830 Day 13 Introduction 1-1. Announcements r Quiz 3: Wednesday, Oct 10 r Prog3 due Wednesday, Oct 10 Transport Layer 3-2.
Transport Layer3-1 Chapter 3 Transport Layer Computer Networking: A Top Down Approach 4 th edition. Jim Kurose, Keith Ross Addison-Wesley, July A.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Transport Layer 3-1 Chapter 3 Outline r 3.1 Transport-layer services r 3.2 Multiplexing and demultiplexing r 3.3 Connectionless transport: UDP.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
Transport Layer3-1 Chapter 3 Transport Layer Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley,
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of.
UNIX Internet Socket API
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
MULTIPLEXING/DEMULTIPLEXING, CONNECTIONLESS TRANSPORT.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Transport Layer3-1 Chapter 3: Transport Layer Our goals: r understand principles behind transport layer services: m multiplexing/demultipl exing m reliable.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
Introduction 1-1 source application transport network link physical HtHt HnHn M segment HtHt datagram destination application transport network link physical.
Spring 2006 CPE : Transport Layer Overview2-1 Special Topics in Computer Engineering The Transport Layer in the Internet: Overview Some of these.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Transport Layer3-1 Transport Layer Never take life seriously. Nobody gets out alive anyway.
Chapter 3 outline 3.1 Transport-layer services
CS 1652 Jack Lange University of Pittsburgh
06- Transport Layer Transport Layer.
Socket Programming in C
Transport layer API: Socket Programming
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Transport Layer Our goals:
Presentation transcript:

OPERATING SYSTEMS COURSE THE HEBREW UNIVERSITY SPRING Sockets

Overview Files in POSIX-based OS TCP and UDP Transport Protocols Sockets – concept Technical Material Socket Addresses Address Related System Calls Sockets programing 2

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 STDIN 1 STDOUT 2STDERR FDs Table

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.

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.

Overview Files in POSIX-based OS TCP and UDP Transport Protocols Sockets – concept Technical Material Socket Addresses Address Related System Calls Sockets programing 6

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

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 ChecksumLength 8

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

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:

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

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

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

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

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

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

Overview Files in POSIX-based OS TCP and UDP Transport Protocols Sockets – concept Technical Material Socket Addresses Address Related System Calls Sockets programing 17

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

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

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

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.

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

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

Socket programming with UDP Output: sends packet (TCP sent “byte stream”) Input: receives packet (TCP received “byte stream”) Client process client UDP socket 24

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

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

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

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

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

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

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

Big\Little Endian Little-endian is popular (though not universal) among microprocessors Big-endian is the most common convention in data networking 32

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

IP Addresses #include struct sockaddr_in my_addr; my_addr.sin_family = AF_INET; my_addr.sin_port = htons(3490); inet_aton(" ", &(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

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

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

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

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

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

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

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

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

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 ). 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

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

Socket’s Protocol Parameter We use “0” value that choose the default protocol. Sometimes this is the only supported protocol. 45

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

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

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

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

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

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

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

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

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

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

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

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()

Socket programming with UDP There are many references on the web. One that I like: 58

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

Listener side Talker side 1. socket(); 2. bind(); 3. recvfrom(); 1. socket(); 2. sendto(); 60 Summary Datagram Socket

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.