160 likes | 278 Vues
Programming with TCP – I. Timeline of a Typical Scenario TCP Server Functions socket () bind () listen() accept () TCP 3– Way Handshaking TCP Client Functions socket () connect () TCP 3- way HS – errors. Timeline of a Typical Scenario. s ocket ();. TCP Client TCP Server.
E N D
Programmingwith TCP – I Timeline of a TypicalScenario TCP Server Functions socket() bind() listen() accept() TCP 3–WayHandshaking TCP ClientFunctions socket() connect() TCP 3-way HS – errors
Timeline of a TypicalScenario socket(); TCP ClientTCP Server socket(); bind(); Connectionestablishment connect(); TCP 3–wayhandshake listen(); write(); accept(); blocksuntilconnectionfromclient read(); processrequest data (reply) read(); write(); end-of-file notification close(); read(); close();
Timeline of a TypicalScenario • The server is started, thensometimelater a client is startedthatconnectstothe server. • Assumingthattheclientsends a requesttothe server, the server processestherequest & sends a replyback. • Thiscontinuesuntiltheclientclosesitsend of theconnection, whichsends an end-of-filenotificationtothe server. • The server thenclosesitsend of theconnection, goesbacktoaccept. • See: TCPClient.c& TCPServer.c
TCP Server Functions – socket() & bind() • socket(): creates a TCP socket • bind() : bindsthesocketto a well-knownport
TCP Server Functions – listen() Performs 2 actions: • When a socket is created, it is assumedto be an activesocket, i.e. a clientsocketwillissue a connect. Thelistenfunctionconverts an unconnectedsocketinto a passivesocket, indicatingthatthekernelshouldacceptincomingconnectionrequestsdirectedtothissocket. Interms of TCP statetransitiondiagram, thecalltolistenmovesthesocketfromCLOSEDstatetotheLISTENstate.
TCP Server Functions – listen() • Thesecondargumenttothisfunctionspecifiesthemaximumnumber of connectionsthatthekernelshouldqueueforthissocket. • Tounderstandthebacklogargument, wemustrealizethatfor a givenlisteningsocket. • #include<sys/socket.h> • int listen(intsockfd, intbacklog); • returns: 0 if OK, -1 on failure.
TCP Server Functions – listen() Thekernelmaintains 2 queues • Incompleteconnectionqueuewhichcontains an entryforeachSYNthat has arrivedfrom a clientforwhichthe server is waitingcompletion of the TCP 3-wayhandshake. Thesesocketsare in theSYN_RCVDstate. • A completedconnectionqueuewhichcontains an entryforeachclientwithwhomthe TCP 3-wayhandshake has completed. Thesesocketsare in ESTABLISHEDstate.
TCP Server Functions – listen() SERVER accept completedconnectionqueue. (ESTABLISHED state.) incompleteconnectionqueue. (SYN_RCVD state.) SYN Arriving Sum of bothqueuescannotexceedbacklog!
TCP 3–WayHandshaking TCP ClientTCP Server connectcalled SYN j Create an entry on incompletequeue. RTT SYN k, ACK j+1 RTT connectreturns ACK k+1 Entrymovedfromincompletequeuetocompletedqueue. accept can return
TCP 3–WayHandshaking • Whatifthis 3rd ACK neverarrives? • Berkley-derivedimplementationshave a timeout of 75 secondsfortheseincompleteentries. • Whenthe 3–wayhandshakecompletesnormally, theentrymovesfromincompletequeuetotheend of thecompletequeue. • Whentheprocesscallsaccept, thefirstentry on thecompletedqueue is returned. Ifthequeue is empty, theprocesssleepsuntil an entryarrives.
TCP Server Functions – accept() • #include<sys/socket.h> • intaccept(intsockfd, structsocaddr *clientaddress, • socklen_t *addrlen); • returns: non-negativedescriptorif OK, -1 on failure. • acceptreturnsthenextcompletedconnectionfromthefront of thecompletedconnectionqueue. • Ifthecompletedconnectionqueue is empty, theprocess is put tosleep. • Ifacceptis successful, itsreturnvalue is a brandnewdescriptorautomaticallycreatedbythekernel. • A given server typicallycreatesonelisteningsocketwhichthenexistsforthelifetime of the server. BUT thekernelcreates a connectedsocketforeachclientconnectionthat is accepted. Whenthe server is finishedserving a givenclient, theconnectedsocket is closed.
TCP ClientFunctions – socket() • Same as TCP server’ssocket().
TCP ClientFunctions – connect() • #include<sys/socket.h> • intconnect(intsockfd, conststructsockaddr • *serveraddress, socklen_t *addrlen); • returns: 0 if OK, -1 on failure. • connectis usedby a TCP clienttoestablish a connectionwith a TCP server. • Ifclientsocket is not boundto a specificport/IP beforeconnectis called (as in ourexample), kernelwillfirstchoose an ephemeralport# & source IP.
TCP ClientFunctions – connect() TCP ClientTCP Server • Thereareseveraldifferenterrorspossible connectcalled SYN j Create an entry on incompletequeue. RTT SYN k, ACK j+1 RTT connectreturns ACK k+1 Entrymovedfromincompletequeuetocompletedqueue. accept can return
TCP 3-way HS – errors • TCP Clientreceives no responsetoitsSYN. ETIMEOUT is returned (after a total of 75 seconds). connectcalled SYN j 6 seconds SYN j 24 seconds SYN j . . . returnETIMEOUT
TCP 3-way HS – errors • Iftheserver’sresponsetotheclient’sSYNis a rest (RST) indicatingthat no process is waitingforconnections on the server host at thespecifiedport (i.e. The server process is probably not running). ECONNREFUSEDis returnedtotheclient. • Ifconnectfails, thesocket is no longerusableandmust be closed. Wecannotcallconnectagain on thesocket. • Interms of TCP statediagram, connectmovesthestatefromCLOSEDtoSYN_SENTtoESTABLISHED.