Transmission and Programming | Socket Programming | Alison
Loading

Module 1: Transmission and Programming

Nota de Estudos
Study Reminders
Support
Text Version

Socket Programming

Set your study reminders

We will email you at these times to remind you to study.
  • Monday

    -

    7am

    +

    Tuesday

    -

    7am

    +

    Wednesday

    -

    7am

    +

    Thursday

    -

    7am

    +

    Friday

    -

    7am

    +

    Saturday

    -

    7am

    +

    Sunday

    -

    7am

    +

Socket Programming
Welcome back to the course on computer network and internet protocol. So, today wewill see some demo of network programming. So, we will look into the socketprogramming in details. And see that with the help of socket programming, how you canaccess the transport layer of the protocol stack, and you can run your own application ontop of the network protocol stack to transfer some data to or from a computer.(Refer Slide Time: 00:47)So, let us start our journey in the socket programming. So, to start with as we have seenearlier or we have also discussed earlier that this entire network protocol stack isimplemented inside the kernel of the operating system. So, in general we have five layersin the TCP/IP protocol stack that we have talked about. And in that five layers of theTCP/IP protocol stack this physical layer, and the part of the data link layer are storedinside the hardware.Whereas the upper part of the data link layer mainly a part of the MAC and the logicallink control, the network layer and the transport layer they are implemented inside thekernel space of operating system. And then from the user space you can write multipleapplication to access this protocol stack. So, the application have to interact with thetransport layer to send or receive data. And as we have learned already that in thetransport layer, we have two different protocols, the transmission control protocol or theTCP and the user datagram protocol or UDP.So, today we will look into that this entire kernel space which is there inside that wehave this different layers of the protocol stack, the transport layer, network layer and thepart of the data link layer which is already implemented inside the operating systemkernel. And we will see that how you can make a interaction between this user space andthe protocol stack which is implemented inside the kernel space with the help of thesocket programming. Now, interestingly what we will see that whenever you will write aapplication, network application at the user space, you have to transfer or you have to usecertain kind of functionalities which are available at the kernel.So, you require a interfacing between the user space and the kernel space. And rememberthat here we are talking about a UNIX based operating system. And, in a UNIX basedoperating system from the user space to kernel space that interaction will be done withthe set of APIs which we call as the system call. So, the system call transfers some userrequirement to the kernel corresponding kernel operations and performed operations atthe Linux or UNIX kernel. So, this entire protocol stack that is implemented inside thekernel we will make an interaction with that network protocol stack with the help of theoperating system system calls.So, interestingly just a pointer for you to explore further that you can look into this entireprotocol stack implementation inside a UNIX kernel. If you download the UNIX kernelsource inside the UNIX kernel source, you can check the net module under user sourceLinux net. And you can see this entire implementation of the kernel protocol stack there.(Refer Slide Time: 03:51)So, these things you have already learnt about that this TCP/IP protocol stack at thetransport layer it does application layer multiplexing. You can run multiple application indifferent devices. Now, different application can run different type of protocols. So, ifyou are having a browsing or HTTP application that will run TCP. If you have a filetransfer or FTP kind of application that may again run TCP. If you have some applicationlike VoIP, that may use UDP.Now, all these different transport layer instances of the protocol stack that interact withthe IP layer. Now, whenever you are talking among two devices the IP layer gets changedand whenever you are having multiple applications which are running on top of your IPlayer or the network layer of the protocol stack, they are multiplexed with the help ofdifferent type of protocol. So, to differentiate between two devices at the IP layer, we usethis IP address. In the subsequent lecture, whenever we discuss about IP layer, we willdiscuss about how you can configure these IP addresses. So, for the time being justunderstand that different devices, they are separated by the IP addresses. And then at theapplication side, we have these port numbers that actually help you to do the applicationlayer multiplexing.So that means, whenever we talk about that device A is communicating with device Bduring that time, it is actually if you are doing some kind of browsing application whichis using HTTP protocol during that time the device A, the application which is using theTCP protocol at a port 8081 on the machine with IP address 202.141.81.2, it isinteracting with the device B at IP address 203.12.23.43 over a port 8080, where yourHTTP server is running.Similarly, for the other application that way this application running at device A andapplication running a device B they can be segregated with the help of that port number.And remember that whenever we talk about this kind of application in a UNIX basedsystem, we basically represent it in the form of a process. So, there are multipleprocesses which are running in different machines. And those processes want tocommunicate with each other and during that time we make or we ensure this process toprocess communication with the help of that transport layer of the protocol stack.So, this process to process communication is achieved with the help of this IP address,which is there at the IP layer to uniquely identify a machine in the network. And then in amachine there are multiple processes running, they can use different protocols. Some ofthe processes may use the TCP protocol, some of the process may use UDP protocol, sothat are segregated with the help of the port numbers.Now, let us see what is a socket. So, socket is basically a logical connection from oneprocess to another process. So, here you can see that these two browsing applications atthe two devices they are communicating with each other. So, we have the socket onesocket - this red socket which is making a logical pipe between the application which isrunning at port 8081 on the machine 202.141.81.2 to a machine where the correspondingend of that pipe is running at a port 8080 at a machine with IP address 203.12.23.43. Sothat way, we can have multiple such logical pipes at the transport layer which we call asthe socket.Now, sending the data over the internet means sending the data over these logical pipes.So, these logical pipes which we call as the socket they basically creates this end to endconnection in case of TCP or end to end data transmission semantics in case of UDP totransfer the data from one machine or better to say one process running at one machineto another process running at another machine.(Refer Slide Time: 08:29)So, here let us see that how we can implement such a socket in a UNIX based system.So, for that we use this concept of socket programming. So, in a socket programmingframework, we have a set of system calls that we can execute from the C program. Andthis system calls will help us to get the service from the TCP/IP protocol stack which isimplemented inside the net module of the OS kernel.So, let us see that how this entire thing works. So, at the transport layer, we are talkingabout a client server this programming. So, we have a server where the idea is that theserver has opened a port, announced the port that this particular port I am listening andthe client need to make a connection to that particular port.Now, in that case how the server actually works. So, in the server side, you have to firstmake a socket system call. So, the socket system call, it will create the server sideopening of the logical pipe and it will bind the socket with your TCP/IP protocol stack.So, to bind the socket with the TCP/IP protocol stack, you have to call this bind function.So, this bind function, what it will do that with the port number that you are specifying itwill bind that port number with the socket, so that way it will create a logical end of theconnection at the server side.So, just think of the server in this way that the server is always running, and the serveractually need to announce that hey, I am actually listening in this particular port, say port8080. So, if anyone wants to talk to me, you can send data at the port 8080. So, thisannouncement you have to done through this bind and a listen system call that we havehere. So, the bind system call actually bind the port with the corresponding socket end,and the listen system call will help you just to make the server to go in the listening state.So, the server is now say bind that port 8080, and it is listening for the incomingconnection.Now, let us move at the client side. At the client side you have the socket system call. So,this socket system call again create a client side end of the logical pipe. And after that ata client site you do not require this bind and listen, because just understand the nature ofthe communication between the server and the client. So, the server is actuallyannouncing or making an announcement that hey, I am listening at this port 8080. So,anyone wants to connect to me, you can directly connect to me at port 8080 that theclient do not need to know because the client is actually initiating the connection to theserver.Because the client is initiating the connection to the server, the client do not need tomake such kind of announcement. So, the client can just initiate the connection to theport which is being announced by the server and that is why you do not require the bindand the listen call at the client side. So, at the server side, you require the bind and thelisten call so that the server can bind itself to a port to a fixed port and it can announcethat fixed port to the outside that anyone can connect to that particular port by creating asocket.Now, after these things are done, after you have created the end of the socket at the clientside, from the client side you make this connect call to initiate a connection to the portnumber which is announced by the server. Now, that is actually a kind of well knownthings, like say you know that if you are running a HTTP server, then you are eitherrunning at port 80 or you are running at port 8080 or some other ports which is beingannounced by the server. So, the client already knows that what is the IP address wherethe server is running, and what is the port number where the server is running. So, theclient initiate a connect call there and this connect makes a connection towards theserver. So, once the server gets this connection, it makes a accept call.Now, in case of a TCP kind of protocol within that connect and accept, you have the TCPthree way handshaking procedure that we have discussed. So, the client initiates theconnection by sending a SYN packet, the server accept the connection by returning backan ACK; and also initiating the connection to the client side by sending another SYN. So,we are having a SYN plus ACK from the server to the client, and then finally, the clientsends an acknowledgement, so that way through this three way handshaking of TCPwhich happens whenever you are making this connect system call at the client side andaccept call at the server side to make the connection in case of a TCP.Now, once this connection is established, then you can make this send and receive call tosend the data and receive the data. So, whenever you are making a send call, it is sendingthe data; at the other end you can receive that data by making a receive system call oryou can make a send to the from the server side to send some data from the server to theclient. And the client accept that data from this receive system call. So, once this datacommunication is done, then you make this close call, finally the close call to close thecorresponding connection, so that way this entire flow of socket programming works.Now, let us look into that how you will actually write this system call in the format of aC syntax.(Refer Slide Time: 14:07)So, we will start with different type of sockets as we have discussed long back that theinternet is a tradeoff between performance and reliability and that is why we have twodifferent protocols at the transport layer, the transmission control protocol or TCP or theuser datagram protocol and the UDP. Now, some application they require fine grainedperformance like the multimedia applications and some others requires reliability like afile transfer. And accordingly we have two services like a reliable transmission protocolor TCP kind of protocol and the unreliable transmission protocol like a which is UDPprotocol.Now, accordingly we have these two different types of sockets; one socket we call as thestream socket which is initiated by sock stream. So, this sock stream is, create a socketwhich is reliable and connection oriented. So, it is necessarily a TCP kind of socket. Onthe other hand, we have this UDP based socket which is unreliable and connectionlessthat we call as a datagram socket, which is termed as SOCK_DGRAM, so that way wehave two broad kind of socket, stream socket and the datagram socket.Apart from that we have a third kind of socket that is called raw socket. Using the rawsocket, you can actually bypass the transport layer and you can directly in turn interactwith the IP layer. So, we will not going to discuss this raw socket here in details. We aregoing to give you an overview about this stream socket and the datagram socket.(Refer Slide Time: 15:35)Now, whenever you are declaring a socket, so what you can do you can declare a verytwo variable called integer s integer type of variable which hold the socket id that youare going to define. So, this socket system call it takes these parameters, three parameters- the domain, type and the protocol. Now, it creates a socket with this socket system call.Now, this domain parameter it is the communication domain. Normally, we use IPv4protocol or IPv4 address. So, we set this domain value as AF_INET which is a standardfor the time being most of the time you will use AF_INET. You can always explore whatare the other possibilities in this domain field.Then the type of the field it is type of the socket either SOCK stream or SOCK datagrambased on whether you are going to create a TCP socket or a UDP socket. And finally, theprotocol specifies the protocol family that we are going to use. Usually it is set to 0. So, Iwill suggest you to explore this that why we set the protocol field at 0 in most of thecases.Now, once the socket system call is done, you have created the socket. At the server siteyou have to create the next calls to bind, to bind the port to the particular socket. So, thisbind system call works in this way it returns the status whether the bind is successful ornot. So, you can have the status as integer variable. And the bind takes three parametersthe socket id. The socket id that is returned by this socket system call and yeah the socketid that is returned by this socket system call, so this s value is the socket id which hasbeen returned that you can put here.Now, that particular socket is bind to a address port kind of variable which is a structure.So, this structure contains struct sockaddr in. So, this structure contains the IP addressand the port of the machine. So, usually set to inaddr any to choose a local address. So, ifyou are run it as inaddr any, it will choose the IP address which is used by your machineand then the size. So, the size is the address size of thus this sockaddr structure.
Socket Programming- Part 2
So, the sockaddr structure looks something like this, which actually stores the IP addressand the corresponding port number. It has these three fields, one is the sin family. So, thisis the address family. So, the address family we keep it as AF INET for IPv4 protocolwhich we are going to use. So, this concept of IPv4 we are going to discuss in thesubsequent lecture. So, when we talk about the IP addressing scheme, so normally ingeneral in today’s network we mostly used IPv4 address. So, the address familiesnormally set to AF INET. .Then we have this socket in address dot s addr it is the source address. So, the sourceaddress we keep it as inaddr any as I have mentioned to choose the local address of themachine where I am running the code. And then I have the port number in the variablessin port the port number. So, now one interesting fact is here that we need to use thisfunction called h to ns to convert the port number from host byte order to network byteorder. Now, let us look at quickly that what is the source byte order and the network byteorder.(Refer Slide Time: 19:25)So, in a computer system, the computer can be of two type either it can be a little endiansystem or it can be a big endian system. Now, the difference between a little endiansystem or a big endian system is something like this. Yeah, it is like how you are storingthe data in the memory. Now, in case of a little endian system, you will store the datafrom left to right sorry from right to left. So, this 0D will be stored fast then it will store0C, then it will store 0B, and finally, it will store 0A.Whereas, in a big endian system, it is just opposite. So, it is left to right associativity kindof things. So, in a register if your data is something like this, in the memory it will firststore 0A, then it will store 0B, then it will store 0C and finally, it will store 0D. So, thatway depending on whether your machine is following a little endian platform or a bigendian platform, the representation of the data inside memory may get changed.So, assume a communication from a little endian to a big endian system. Now, if you aretransferring data from a little endian system, you will transfer the data in the form of abyte stream in the sequence of bytes. So, you will first say and possibly 0D, then 0C,then 0B, then 0A. And whenever it big endian system will get 0D, it will put the 0D first,then the 0C second and that way whenever it will interpret it will interpret the numberjust in the opposite direction. So, that way there may be a kind of inconsistencywhenever you are transferring the system, so that is why we use this concept of post byteorder to the network byte order, the idea is that the host can be little endian or big endian.(Refer Slide Time: 20:57)They have a kind of byte order. Now, network is a has a fixed byte order. So, wheneveryou are transferring the data over the network, you convert it from the host byte order tothe network byte order, transfer it over the network. At the other end, you fetch the dataconvert it again to the host byte order based on whether the your system is little endian orbig endian and store it there. So that way this kind of inconsistency which may come dueto the representation difference of two machines that can be solved. So that is the idea ofconverting the port number from the host byte order to the network byte order.So, here is an example how you can initiate the address variable. So, you setup the portat 3028 which is you are taking it as an integer variable then you have this sin family AFINET that I have discussed, sin address dot addresses inaddr any to take the localaddress. If you want you can also put some IP address there but that IP address need tobe matched with the IP address used by your network interface. And then in the sin portyou make this call to h to ns over the port number to convert it to the network byte order.(Refer Slide Time: 22:15)Well, now to accept a socket connection, so you in the client side you create a clientaddress. Now, the server it is listening on this particular socket. So, this listen functionhas a parameter called 5 here. So, this particular parameter indicates that how manymaximum connection can be backlogged when multiple clients are trying to connect tothe server. And then you take this size of this address variable that you have declared andmake a accept call. So, this accept call will take the sock fd where the socket is listeningthe client address that will be provided. So, when the client will get connected theaddress of the client will get stored in this variable, and the length of that client address.Now, this accept call whenever you are initiating a connection as I have mentioned thatthe server need to always in the mode where it is waiting for any incoming connectionand the clients need to initiate the connection. So, accordingly we have two kind ofconnection called active open and the passive open. Now, as I have mentioned that theserver needs to announce it address remain in the open state and waits for any incomingconnection, so that is the kind of passive open. And the client it only opens a connectionwhere there is a need for data transfer that is the kind of active open. And the connectionit is initialized by the client.(Refer Slide Time: 23:41)Now, these are the data transfer format. So, we have two different type of socket; thestream socket and the datagram socket. In case of a stream socket, you can use thefunction called read and write by providing the socket identifier. So, here is aninteresting fact that whenever you are accepting a connection, you are getting a newsocket id. So, why you are getting a new socket id, because the server is listening on onesocket; now when the client is initiating there can be multiple clients which areconnecting simultaneously. Now, when multiple clients are connecting simultaneously,you need to create separate logical pipe to separate client, so that are actually indicatedby the socket address which is written by this accept system call.So, the accept system call will return an address and that particular address will beassigned to the new sock fd and while you are sending the data you use that new socketid because that has created end to end stream or end to end pipe to a specific client andyou will send a message. So, for the stream kind of socket, you can use the read and thewrite function to read data from a buffer or to write data to a buffer. For the datagramsocket you can use the function called the receive from and send to; receive fromfunction to receive data from the socket or sent to function to send data to that particularsocket.(Refer Slide Time: 25:09)Now, here are some, so I will show you some demo of the entire thing. So, here are somelink that you can follow to learn socket programming in more details. So, what I willsuggest you to go to these particular links and start writing your own networkprogramming using the socket. So, let us quickly go to some demo of this entire idea ofsocket programming.(Refer Slide Time: 25:37)So, we will first look into UDP server and a corresponding UDP client. So, let us openlook into the UDP server first.(Refer Slide Time: 26:05)So, here is your code for the UDP server. So, in the UDP server code, you can see thatwe have included some header, these are the kind of standard headers that we have toinclude. And then inside the main function, we are declaring the entire thing. So, in themain function, we have this we are first defining a socket, which is the struct sockaddr inand the corresponding server address. Then we are defining a socket identifier. And weare defining a port number.Now, here this we are first making a socket system call. In the socket system call, youhave this AF INET the parameter that we have mentioned we need to specify the datagram socket. We are specifying because we are trying to create a UDP socket and finalparameter is equal to 0. The 0 parameter that we send for the protocol field that we havementioned. Then once the socket is created, if there is an error, we print some errormessage.(Refer Slide Time: 27:17)Otherwise we declare the server address as we have discussed earlier. So, we declare it asthe protocol family as AF INET the address as inaddr any with a address of this machineand then the port number. Then we make the bind system call. The bind system call is tobind the socket with the corresponding port number that we are specifying as a commandline argument. And finally, we make a call to a function called setsockopt, this setsockoptis to set some option to the socket, here we have set the option is so_reuse addr. Thisso_reuse addr will help us to use the same port for multiple connections together and thatis not a safe idea, but sometime you can use that.So, what you can do once you have created the socket at the server side, you can directlymake the call to the receive from function to receive the data. And this receive fromfunction will contain the client details. So, this is the client address which is clear in thereceive form function. And once you are receiving data we are printing some data andmaking a send to call. So, this send to call is sending the data to the corresponding client,so that is the port at the server side.After that we are creating the server address by using that setting the sin family the hostaddress and the server port. So, the value which are being provided by the by the client atthe through the command line. So, after that once you have got the server address, againyou do not need to initiate a connection, you can directly make the send to call to sendsome data. So, from here we are making a send to call. So, this send to call is sending thedata to the particular socket with this server address which we are specifying here.So, now let us first run the server. So, according to our syntax we that is we have to runthe server and specify a port address where the server will connect itself the bind throughthe bind system call. So, let us give the port number as 2333. So, the server is nowrunning. Now, from the client side we can run the client. And we are running the serverin the same machine, so the host name of the server you can give it as local host. And toyou are running the server at the port 2333; at port 2333, so we can provide the serverport as 2333. So, it has send a message to the server you can see that the server hasreceived the message from the client and it is returning back that message. So, the clienthas received the message and printed it.So, you can again run the client. And you can see that it has received a message. Now,note the note one thing here you are printing the server IP the client IP and the clientport, the client IP is the local IP of this machine. And a client port whenever we arerunning multiple client, the client code gets changed. Now, at the client side as the clientdo not bind itself to our well known port during the runtime, the client randomly choosesone port address and initiate the client transfer from that particular port address, so that iswhy a different run the port address gets changed.So, if I run it again multiple time at different time, the port address gets changed. So, atdifferent instances it takes different port. So, this is a demo about the UDP server and aUDP client which is the possibly the simplest form of the socket programming. We willshare the code with you, we will request you to browse through the code run into yourown machine and see what is happening and understand it more details. So, in the nextclass, we will show you the demo about the TCP server, TCP client and some variants ofTCP server and the TCP client.Thank you all for attending the class.

Notification
Você recebeu uma nova notificação
Clique aqui para visualizar todos eles