Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring 20061 CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.

Slides:



Advertisements
Similar presentations
Programming with TCP – I
Advertisements

© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
1 Elementary TCP Sockets socket function connect function bind function listen function accept function fork and exec functions Concurrent servers close.
Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
CSCE 515: Computer Network Programming TCP Details Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Tutorial 8 Socket Programming
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Client Server Design Alternatives© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Computer Network Architecture and Programming
Advanced UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
ECE 4110 – Internetwork Programming Client-Server Model.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
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.
Elementary TCP Sockets
Chapter 2 Applications and Layered Architectures Sockets.
TELE 402 Lecture 3: Elementary … 1 Overview Last Lecture –TCP/UDP and Sockets introduction This Lecture –Elementary TCP sockets –Source: Chapter 4 of Stevens’
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
Distributed Computing A Programmer’s Perspective.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
1 Dimension of Server Designs r Iterative vs Concurrent r Connection-oriented vs. connectionless r Stateful and stateless m Constrained by application.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 K. Salah Application Layer Module K. Salah Network layer duties.
1 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Assignment 3 A Client/Server Application: Chatroom
Sockets and Beginning Network Programming
Read: Chapters 1,2, 3, 4 Communications Client Server Ex: TCP/IP
Elementary UDP Sockets
CH5 TCP Client - Server Example:
Chapter4 Elementary TCP Socket
Socket Programming in C
CHAPTER 8 ELEMENTARY UDP SOCKETS
Network Programming CSC- 341
Network Programming CSC- 341
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
UDP Sockets Programming
Socket Programming in C
TCP Sockets Programming
Advanced Network Programming spring 2007
Chapter 11 Name and Address Conversions (Part II)
Server-side Programming CSE 333 Autumn 2018
Server-side Programming CSE 333 Summer 2018
Socket Programming Neil Tang 09/08/2008
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Today’s topic: Basic TCP API
Presentation transcript:

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department Virginia Tech Elementary TCP Sockets

© Dr. Ayman Abdel-Hamid, CS4254 Spring Outline Elementary TCP Sockets  Information to write a complete TCP client and server

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring Typical Scenario between TCP client/server

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring socket Function #include int socket (int family, int type, int protocol) // returns non-negative descriptor if OK, -1 on error family protocol family (AF_INET  IPv4 protocols, AF_INET6  IPv6 Protocols) (see Fig. 4.2) type (SOCK_STREAM  stream socket, SOCK_DGRAM  Datagram socket) (see Fig. 4.3) protocol Use 0 to get system’s default given combination of family and type (see Fig. 4.4)

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring connect Function 1/3 #include int connect (int sockfd, const struct sockaddr * servaddr, socklen_t addrlen) // returns 0 if OK, -1 on error No need to specify client’s source IP address or port  Kernel will choose an ephemeral port and source IP if necessary Connect function initiates TCP’s three-way handshake Function returns only when connection is established or an error occurs

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring connect Function 2/3 Several possible errors (The following numbers for 4.4 BSD) Send SYN....& after 6 seconds..& after 24 seconds if after a total of 75 seconds no SYN-ACK received  ETIMEOUT is returned if server responds with RST  no process waiting at port  hard error  ECONNREFUSED is returned if a router returns ICMP destination unreachable (soft error)  send after 6 and 24 seconds and if no connection after 75 seconds  EHOSTUNREACH is returned You can't reconnect the socket to another address unless you close and call socket again.

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring connect Function 3/3 Try it out with the daytime TCP client/server  Successful connection  IP address on local subnet, but host nonexistent Connection timed out  Correct local IP address, not running a daytime server Connection refused  Unreachable Internet IP address Intermediate router will return ICMP error No route to host Reasons for RST segment  SYN arrives for a port with no listening server  TCP wants to abort an existing condition  TCP receives a segment for a connection that does not exist

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring bind Function 1/2 #include int bind (int sockfd, const struct sockaddr * myaddr, socklen_t addrlen) // assigns a local protocol address  returns 0 if OK, -1 on error Server (see daytimetcpsrv3.c in intro folder)  Normally bind to a well know port & INADDR_ANY  Using port 0: kernel choose a free port and we use getsockname to find the selected port  When a connection is accepted, the address of the connection is fixed and we use getsockname to find the interface IP address  You can bind to specific IP address instead of INADDR_ANY, only connections to this address are accepted  Can generate EADDRINUSE error

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring bind Function 2/2 Client (see daytimetcpcli3.c in intro folder)  Normally do not bind to any specific port or address  As part of connect  bind is implicitly called  Any ephemeral port and interface IP address is filled based on the routing table  Use getsockname to find out the port and address struct sockaddr_inservaddr, cliaddr; len = sizeof(cliaddr); Getsockname(sockfd, (SA *) &cliaddr, &len); printf("local addr: %s\n", sock_ntop((SA *) &cliaddr, sizeof(cliaddr)));

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring listen Function 1/4 #include int listen (int sockfd, int backlog) //returns 0 if OK, -1 on error When a socket created  assumed active socket  A client socket that will issue a connect listen converts an unconnected socket into a passive socket backlog specifies maximum number of connections the kernel should queue for this socket Kernel maintains 2 queues  Incomplete connection queue (only SYN received from client)  Completed connection queue (three-way handshake done)

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring listen Function 2/4

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring listen Function 3/4

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring listen Function 4/4 Berkeley-derived implementations add a fudge-factor to the backlog (multiplied by 1.5  backlog of 5 allows up to 8 queued entries). See figure 4.10 A backlog of 0 is not recommended (different implementations) Specifying a backlog inside source code is a problem! (growing number of connections to handle)  Specify a value larger than supported by kernel  kernel truncates value to maximum value that it supports  Textbook uses an environment variable for backlog (see lib/wrapsock.c ) If queues are full when client SYN arrives  Ignore arriving SYN but do not send a RST (Why?) Data that arrives after 3WHS, but before a call to accept should be queued by TCP server

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring accept Function #include int accept (int sockfd, struct sockaddr * cliaddr, socklen_t * addrlen) //returns non-negative descriptor if OK, -1 on error cliaddr and addrlen used to return protocol address of connected peer process Set to null if not interested in identifying client addrlen is a value-result argument Difference between listening socket and connected socket See daytimetcpsrv1.c getsockname return the same port number for listening and connected socket

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring Server Concurrency Servers use concurrency to achieve functionality and performance Concurrency is inherent in the server  must be explicitly considered in server design Exact design and mechanisms depend on support provided by the underlying operating system Achieved through  Concurrent processes  Concurrent threads (will cover later)  Can you differentiate between the two design methodologies?

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring fork Function #include pid_t fork (void) //returns 0 in child, process ID of child in parent, -1 on error A child has only 1 parent, can obtain parent ID by calling getppid Parent can not obtain IDs of its children unless keep track from return of fork All descriptors open in parent before call to fork are shared with child after fork returns (connected socket shared between parent and child) Use fork to  Process makes a copy of itself (typical for network servers)  Process wants to execute another program (call fork then exec )

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring Concurrent servers 1/3 ?

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring Concurrent Servers 2/3 Why close of connfd by parent does not terminate connection with the client? Every file or socket has a reference count Reference count: A count of the number of descriptors that are currently open that refer to this file or socket

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring Concurrent Servers 3/3

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring Port Numbers and Concurrent Servers 1/2 Main server loop spawns a child to handle each new connection What happens if child continues to use the well- known port number while serving a long request?

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring Port Numbers and Concurrent Servers 2/2 Another client process on client host requests a connection with the same server

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring close Function #include int close (int sockfd) //returns 0 if OK, -1 on error Will try to send any data that is already queued to be sent to the other side, then normal TCP connection termination sequence takes place (send FIN) Can use an option to discard unsent data (later)

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring getsockname and getpeername Functions #include int getsockname (int sockfd, struct sockaddr* localaddr, socklen_t * addrlen) Int getpeername (int sockfd, struct sockaddr* peeraddr, socklen_t * addrlen) getsockname returns local protocol address associated with a socket getpeername returns the foreign protocol address associated with a socket getsockname will return local IP/Port if unknown (TCP client calling connect without a bind, calling a bind with port 0, after accept to know the connection local IP address, but use connected socket)