Transcript for:
Socket Programming

[Music] in this section we're going to learn about socket programming sockets are the api the only api that's available between application layer code the transport layer services beneath it in the internet architecture we're going to learn about the socket abstraction we're going to learn about programming udp sockets and tcp sockets and we're going to walk through an example of a client server simple client server application programmed in python using sockets so let's get started in this section we're going to learn how to build to program client server applications that communicate over the internet and that means they're going to need to communicate using sockets sockets are the one and only api that sits between the application layer and the transport layer and if you want to directly access the internet's transport layer services to send application layer messages from one part of the distributed application to another you're going to need to use sockets that's true for all operating systems from an operating system point of view your applications written in user space that's to say outside of the operating system while the transport layer the layers beneath it in the internet protocol stack are inside the operating system the socket is the interface the door between your application layer program the transport layer within the operating system below it the internet socket api only provides for two types of transport layer services the first offered by tcp provides for reliable congestion-controlled flow controlled and byte-stream-oriented data transfer from one process to another the second which is udp service offers a datagram-oriented unreliable service from one process to another in just a second we're going to program in python a very simple client server application to see socket programming up close and personal as the saying goes and we'll do it using udp sockets first and then tcp sockets and in this application here's what's going to happen client's going to read a line of characters data from its keyboard and send that data to the server the server then receives the data and converts the characters to uppercase and then sends that uppercase translation back to the client the client then receives this data from the server and will display it on its screen well it's a silly little application and you're not going to become a billionaire building applications like this but it will show you how to use sockets in practice then you can come up with a great idea for an app program it using sockets and become a billionaire and if you do remember where you learn socket programming with udp service there's no connection between the client and the server that means there's no handshaking involved before the client server can communicate it also means that when the client is going to send data to the server it's got to include explicitly include the ip address and the port number of the server on the server side when the server receives a datagram it's going to have to extract out the client ip address and the client port number to know who it's talking to with udp data can be lost and can also be misordered as it travels between client and the server so from an application point of view udp service is about an unreliable actually unordered transfer of datagrams from a client to a server so now let's look at the sequence of steps taken by the client and the server to create use and then close sockets in that toy application that we discussed earlier and we're going to start with udp sockets since they're the easiest in a programming sense so we have the client here on the right and the server here on the left the client and server are going to need to first create the sockets to use here's a python code snippet in red on the client side where the client creates a udp socket object named client socket let's take a look at these parameters the first parameter af underscore inet says that this is going to be an internet type socket so there used to be actually a lot of underlying network layers around not just the internet and ip when sockets were invented so we need to specify this is an internet type socket and in particular this socket is going to be using the internet protocol version 4. don't worry about this now we're going to get to this in chapter 4. the second parameter sock dram says that this is a udp datagram socket rather than a tcp socket and note that we're not specifying the port number here of the client socket when we create it we'll let the operating system do this for us if we wanted to though we could request a specific port number for the socket using the bind method on the server side we do the same thing creating a socket with the name of server socket now on the client side we're going to create an application layer message a string of characters and when we send this message into the socket we're also going to explicitly need to attach the server's ip address and the service port number to the message and then pass that information into the client socket socket now you might wonder how do we know the server's ip address and port number well you sort of just have to the internet doesn't have a directory service where services are say registered by servers and clients could look up that service there are network architectures that have included such a service novel networks for example had a really nice directory service but in the internet you just need to know the host name or the ip address and the port number and if you only know the host name that will need to be translated into an ip address via a call to your local dns server as we saw earlier and one quick thing to remember about port numbers some port numbers are standardized as we've seen before they are quote unquote well-known port numbers remember that we've seen some well-known port numbers already port 80 for http port 25 for smtp port 53 for a dns server now returning to this example the datagram sent by the clients eventually received by the server and the application layer message read out of server socket the server will also be able to learn the ip address and the port number of the sending client the server then formulates a reply message and sends the message into its udp socket called server socket and note that this is the same socket that the server's reading from as well as sending into as in the client case the server is going to need to include an ip address and port number for the destination of this datagram the message eventually reaches the client the client reads the message from the server and closes the socket now let's dive down even deeper to the next level of detail for this example and look at the client and server code line by line in python we want to learn how to program sockets and examples are great and this should be conceptually pretty easy since we've covered a lot already our client begins by including python socket module by including this line we'll be able to use all of python's socket related methods we specify the server name and the server's port number here and then we create the udp socket on the client side we've walked through this already the client then gets some user input from the screen and uses the send to method to send the application layer message into the client socket you can see here how the server name actually not its ip address and the server's port number are included in the call to the send to method the translation between the server name and the server's ip address is actually done for you inside the send to method and of course is going to involve a call to the dns the client then reads a reply back from the server using the receive from method note that the socket being received from client sockets specified here and the receive from method returns both the message and the ip address of the sender of this message the client then prints the message and closes the socket with a call to the close method again specifying the socket client socket to be closed now let's take a look at the python code for our udp server the server like the client first includes python socket module then creates a udp socket named server socket using the socket method just as we've seen on the client side in the server's case however the server doesn't want its operating system to assign any old port number to this socket since the client is going to be contacting the server specifically on server port 12000 so here the server uses the bind method to assign port number 12000 to this socket now if port number 12 12000 had been in use by another process the bind method would have returned an error condition here and i'm embarrassed to say that this code doesn't check for that or any other error condition anywhere but definitely should the server then enters a loop reading from the socket using the receive from method that we've seen before on the client side performs its translation to uppercase and then returns a reply via a call to the send to method which we've also seen before on the client side hey that was pretty easy tcp sockets are connection oriented and that means that the client and server gonna have to communicate with each other a little bit before data actually begins to flow so the client is going to first contact the server that means the server has to be up and running and has to have already opened a welcoming socket welcoming the client's contact on the client side the is going to create a socket specifying the server's ip address as well as the server's port number and when that socket is created that's when the magic happens that's when the client process reaches out to the server process at the tcp level to establish that connection now the most important thing to remember about tcp sockets is that when a client first contacts the server as part of the tcp handshake the server is going to create a new socket specifically dedicated for communicating with that specific client now i've found that students who are seeing tcp sockets for the first time sometimes and actually may be often confuse the two sockets involved here so think about this carefully first there's the welcoming socket that's the initial point of contact for all clients wanting to communicate with the server on a particular port number say port number 80 for a web server and secondly there's the new socket that's created by the server for future communication with that specific client now you might ask yourself what's the port number associated with this new socket and and the answer rather confusingly right now is that the new socket has the same port number as that initial welcoming socket we'll clear this up shortly when we cover multiplexing and demultiplexing at the beginning of chapter 3. for now just remember that a new socket is created every time a tcp client first contacts a tcp server and that this new socket is dedicated for communication with that specific client for the duration of that tcp connection this tcp connection serves as a sort of a pipe between the client and the server with the server side of this pipe being this newly created socket and as we've seen that pipe provides reliable in-order byte stream transfer between client and server processes let's now return to our simple toy client server application and take a look at the interaction between the tcp client and the tcp server as we did with udp remember that unlike udp tcp is a connection oriented service this means that before the client server can send application level messages to each other they'll first need to handshake and establish a tcp connection one end of the tcp connection will be attached to the client tcp socket and the other end will be attached to a server-side tcp socket and let's begin on the server side this time because the server needs to be up and running and waiting for a client's connection request the server first creates a socket on which it will wait for an incoming connection request from the tcp client this is the welcoming socket sometimes called the listening socket it's the socket on which it will wait for initial client contacts but it's not the socket through which the application level messages will flow between client and server so after creating a socket the server then invokes the accept method on this welcoming socket this is a blocking call it'll cause the server to wait until a client reaches out now let's head over to the client side the client also creates a tcp socket specifying the server's name and the server's port number to which this socket's going to be connected creating this socket on the client side will cause a tcp connection request message to be sent from the client to the server this connection request message is sent from within the transport layer when the client invokes the socket method it's not sent by the application itself this connection request from the client is just what the server has been waiting for and when it's received at the server a couple of important things happen first a new sockets created at the server and returned to the server side application which returns from the weight it had been doing on the accept call this returned newly created socket called connection socket here in this example is the socket that the server server-side application will use to communicate with the client-side application and secondly a tcp level message is sent from within the server's operating system again not by the server-side application itself to the client to let the tcp client know that the connection's been established we'll look at these tcp messages in detail when we get to chapter 3. in our application the client and server then exchange messages pretty much as in the case of the udp example but let me stress here one last time that the server side application uses this newly created socket created here on return from the accept not the listening socket to communicate with the client the client will communicate with the server using the client side socket it created earlier it's the only socket it's created now let's take a look at the python code for the client side of our application using tcp sockets our tcp based client again begins by including python socket module the server name and server port number specified here then the tcp socket on the client side is created via a call to the socket method note that the arguments to the socket method here specify that this is an ipv4 socket and sockstream indicates that a tcp socket is to be created before sending a message to the server the tcb based client must first connect to the server by invoking the connect method on the socket specifying the server that's being connected to on return from the connect method call the connection has been established all of that handshaking beneath the covers has happened and the client can then read in the input from the keyboard and send the input to the client using the send method here note that in this tcp case there's no need to include information about who the message is going to via the call to the send method connection's already been established and so we know who's on the other side of the connection the client then receives the server's reply via the recv method displays the message and exits closing the socket now let's take a look at the python code for the server side of our application that's using tcp sockets with this socket method call here we're creating an internet ipv4 sock stream that's to say tcp socket we're binding that socket to port 12000 here we then set this socket into listening mode here now we're ready to receive client connection requests and then we enter a loop each time through the loop we'll connect to a client do the work close the connection and loop back again and wait for another client connection request the most important line for you to focus on is this one here where we invoke the accept method this is where the server will wait within the accept method until a client connects and after return from the accept a new socket has been created for us by the operating system called connection socket here this new socket's the one that we'll be using to communicate with the client at this point the server now has two sockets a welcoming socket and a socket that's been created to communicate with this specific client after getting the new connection socket the server reads a line from the client does its work sends the uppercase sentence back to the client and then closes the connection socket pay attention to the fact that when the server invokes the send method here it's using the connection socket not the welcoming socket to communicate with the client and that wraps up our discussion of socket programming in this section we learned about the socket abstraction we learned about programming udp sockets as well as tcp sockets we even walk through an example of python code showing a very simple client server application programmed using sockets and our discussion of sockets here also wraps up our overall discussion of the application layer well our study of the application layer is now complete and we've learned tremendous amount remember we started by talking about the two basic ways for structuring applications client server paradigm the peer-to-peer paradigm then we talked about the application requirements are there delay requirements the reliable data transfer requirements does an application need a certain amount of bandwidth in order to be successful and then we talked about the two types of service that are offered up by the transport layer to applications there's tcp which provides for reliable flow controlled congestion controlled and a bite-stream oriented approach towards transferring data and then there's the udp service model which provides an unreliable and datagram oriented service and we learned about application layer protocols and we studied specific internet application layer protocols including http for the web smtp for email the dns the domain name service which provides a critical internet infrastructure service but does so as an application and we took a quick look at peer-to-peer and the bittorrent system in particular we then moved on to video streaming looking at an application that has timing considerations associated with it and we looked at cdns content distribution networks that are massively distributed systems for providing content close to the edge of the network where the users live and finally we looked at socket programming sockets are the api between application code and the transport layer beneath it in the internet we learned about the socket abstraction and the specifics of programming distributed applications using udp sockets and tcp sockets and perhaps most importantly we learned about protocols we saw a number of examples of client server protocols where client reaches out with a request and the server responds back with an answer to the request with data and often with a status code we also looked at the format of protocol messages and saw that every message that we looked at had a header portion which has information about the message itself and the data or the payload part of the message in our study of network applications and application layer protocols we saw a number of important themes arising that cut across all the applications and the application protocols we looked at do applications take centralized or decentralized approaches are applications and protocols stateless or are they stateful and what about the issue of scale how can one operate at an internet scale that has hundreds of millions of interacting endpoints does an application always require reliable data transfer or will unreliable message transfer actually suffice and finally we saw in a number of cases the notion of pushing complexity to the network edge to the host at the edge of the network and not in the routers and the switches deep within the network and that really does wrap up our discussion of the application layer next we're headed down to the transport layer i think you're going to enjoy that you