1 / 16

Programming with TCP – I

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.

Télécharger la présentation

Programming with TCP – I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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();

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

  4. TCP Server Functions – socket() & bind() • socket(): creates a TCP socket • bind() : bindsthesocketto a well-knownport

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

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

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

  8. TCP Server Functions – listen() SERVER accept completedconnectionqueue. (ESTABLISHED state.) incompleteconnectionqueue. (SYN_RCVD state.) SYN Arriving Sum of bothqueuescannotexceedbacklog!

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

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

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

  12. TCP ClientFunctions – socket() • Same as TCP server’ssocket().

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

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

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

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

More Related