Chapter4 Elementary TCP Socket

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Programming with TCP – I
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
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.
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.
Computer Networks Sockets.
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.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Tutorial 8 Socket Programming
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Introduction to Project 1 Web Client and Server Jan 2006.
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
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.
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.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
Elementary TCP Sockets
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.
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.
1 CMPT 471 Networking II Transport Layer Network Programming © Janice Regan, 2013.
Elementary TCP Sockets
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
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,
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
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.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
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.
EECS340 Recitation 1: Very helpful to your project Hongyu Gao 1.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Assignment 3 A Client/Server Application: Chatroom
Read: Chapters 1,2, 3, 4 Communications Client Server Ex: TCP/IP
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Network Programming CSC- 341
Network Programming CSC- 341
Socket Programming in C
TCP Sockets Programming
Advanced Network Programming spring 2007
Network Programming Chapter 12
TCP/IP Socket Programming in C
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Today’s topic: Basic TCP API
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

Chapter4 Elementary TCP Socket

Why do we need sockets? Provides an abstraction for interprocess communication

Definition The services provided (often by the operating system) that provide the interface between application and protocol software. Application Network API Protocol A Protocol B Protocol C

Functions Define an “end-point” for communication Initiate and accept a connection Send and receive data Terminate a connection gracefully Examples File transfer apps (FTP), Web browsers (HTTP), Email (SMTP/ POP3), etc…

Types of Sockets Two different types of sockets : stream vs datagram Stream socket :(connection- oriented socket) It provides reliable, connected networking service Error free; no out-of-order packets (uses TCP) applications: telnet/ ssh, http etc Datagram socket :(connectionless socket) It provides unreliable, best-effort networking service Packets may be lost; may arrive out of order (uses UDP) applications: streaming audio/ video (realplayer) etc

Addressing Client Server

abstract Socket function connect function bind function listen function accept function fork and exec function concurrent server close function getsockname and getpeername function

TCP Server TCP Client socket() bind() Well-known port listen() accept() blocks until connection from client connect() Connection establishment Data(request) write() read() process request Data(reply) write() read() close() End-of-file notification read() close()

#include <sys/socket.h> int socket(int family, int type, int protocol); returns : nonnegative descriptor if OK, -1 on error ==>Normally the protocol argument to the socket function is set to 0 except for raw socket.

Client – high level view Create a socket Setup the server address Connect to the server Read/write data Shutdown connection

Connect function #include <sys/socket.h> int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); Returns:0 if OK, -1 on error ==>this function used by a TCP client

Make the socket Resolve the host Setup up the struct Connect int connect_ socket( char *hostname, int port) { int sock; struct sockaddr_in sin; struct hostent *host; sock = socket( AF_ INET, SOCK_ STREAM, 0); if (sock == -1) return sock; host = gethostbyname( hostname); if (host == NULL) { close( sock); return -1; } memset (& sin, 0, sizeof( sin)); sin. sin_ family = AF_ INET; sin. sin_ port = htons( port); sin. sin_ addr. s_ addr = *( unsigned long *) host-> h_ addr_ list[ 0]; if (connect( sock, (struct sockaddr *) &sin, sizeof( sin)) != 0) { close (sock); Ipv4 socket address structure struct socketaddr_in{ uint8_t sin_len; /*length of the structure (16)*/ sa_family_t sin_family /* AF_INT*/ in_port_t sin_port /* 16 bit TCP or UDP port number*/ struct in_addr sin_addr /* 32 bit Ipv4 address */ char sin_zero(8)/* unused*/ }   Hostent structure struct hostent{ char * h_name /*official name of host*/ char ** h_aliases; /* pointer ot array of\ pointers to alias name*/ int h_addrtype /* host address type*/ int h_length /* length of address */ char ** h_addr_list /*prt to array of ptrs with \ IPv4 or IPv6 address*/ } Make the socket Socket(int family , int type, in t protocol); return nonnegative value for OK, -1 for error Resolve the host struct hostent *gethostbyname( const char *hostname); /*Return nonnull pointer if OK, NULL on error */ Setup up the struct unit16_t htons(unit16_t host16bitvaule) /*Change the port number from host byte order to network byte order */ Connect connect(int socketfd, const struct sockaddr * servaddr, socket_t addrlen) /*Perform the TCP three way handshaking*/

Connect function Return error ETIMEOUT : no response from server RST : server process is not running EHOSTUNREACH : client’s SYN unreachable from some intermediate router.

Server – high level view Create a socket Bind the socket Listen for connections Accept new client connections Read/write to client connections Shutdown connection

Listening on a port (TCP) int make_ listen_ socket( int port) { struct sockaddr_ in sin; int sock; sock = socket( AF_ INET, SOCK_ STREAM, 0); if (sock < 0) return -1; memset(& sin, 0, sizeof( sin)); sin. sin_ family = AF_ INET; sin. sin_ addr. s_ addr = htonl( INADDR_ ANY); sin. sin_ port = htons( port); if (bind( sock, (struct sockaddr *) &sin, sizeof( sin)) < 0) return sock; } Make the socket Setup up the struct Bind bind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen); /* return 0 if OK, -1 on error assigns a local protocol adress to a socket*/

bind function #include <sys/socket.h> int bind (int sockfd, const struct sockaddr *myaddr,socklen_t addrlen); Returns: 0 if OK, -1 on error ==>this function assigns a local protocol address to a socket. wildcard = INADDR_ANY = 0 “ipv4” in6addr_any “ipv6”

listen Function #include <sys/socket.h> 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)

Backlog argument to the listen function has historically specified the maximum value for the sum of both queues

listen Function 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

accept Function #include <sys/socket.h> 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

accepting a client connection (TCP) int get_ client_ socket( int listen_ socket) { struct sockaddr_ in sin; int sock; int sin_ len; memset(& sin, 0, sizeof( sin)); sin_ len = sizeof( sin); sock = accept( listen_ socket, (struct sockaddr *) &sin, &sin_ len); return sock; } Setup up the struct Accept the client connection accept(int sockefd, struct sockaddr * claddr, socklen_t * addrlen) /* return nonnegative descriptor if OK, -1 on error return the next completed connection from the front of the completed connection queue. if the queue is empty, the process is put to sleep(assuming blocking socket)*/

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 Can you differentiate between the two design methodologies?

fork Function #include <unistd.h> 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)

Fork function #include <unistd.h> pid_t fork(void); Returns: 0 in child, process ID of child in parent, -1 on error A process makes a copy of itself network servers A process wants to execute another program for programs such as shells

Concurrent server Server: Iterative Concurrent pid_t pid; int listenfd, connfd; listenfd = Socket (...); /* fill in socket_in{} with server’s well-known port */ Bind (listenfd, ...); Listen (listenfd, LISTENQ); for ( ; ; ){ connfd = Accept (listenfd, ...); /* probably blocks */ if ( (pid = Fork ( ) ) == 0) { Close (listenfd); /* child closes listening socket */ doit (connfd); /* process the request */ Close (connfd); /* done with this client */ exit (0); /* child terminates */ } Close (connfd); /* parent closes connected socket */

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

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?

Port Numbers and Concurrent Servers 2/2 Another client process on client host requests a connection with the same server

close Function #include <unistd.h> 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)

getsockname and getpeername Functions #include <sys/socket.h> 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)