BITS-Pilani Hyderabad Campus CS C461/IS C461/CS F303/ IS F303 (Computer Networks) Laboratory 4 Aim: To give an introduction to Socket Programming in TCP and UDP. TCP Socket Programming: A socket is the mechanism that most popular operating systems provide to give programs access to the network. It allows messages to be sent and received between applications (unrelated processes) on different networked machines. The sockets mechanism has been created to be independent of any specific type of network. A socket address is the combination of an IP address and a port number, much like one end of a telephone connection is the combination of a phone number and a particular extension. Based on this address, internet sockets deliver incoming data packets to the appropriate application process or thread. An Internet socket is characterized by a unique combination of the following: Local socket address: Local IP address and port number Remote socket address: Only for established TCP sockets. This is necessary since a TCP server may serve several clients concurrently. The server creates one socket for each client, and these sockets share the same local socket address from the point of view of the TCP server. Protocol: A transport protocol (e.g., TCP, UDP, raw IP, or others). TCP port 53 and UDP port 53 are consequently different, distinct sockets. Tools used: gedit, terminal
Experiment 1: Working of a TCP concurrent server 1. Download tcp_client.c and tcp_server.c from the CMS Website 2. Compile server first (as shown below). gcc -o tcp_server tcp_server.c 3. Similarly, compile the client using the following command. gcc -o tcp_client tcp_client.c 4. Run the server using the below command. After running, the server would wait for an incoming connection (as shown in the Figure.1)./tcp_server Fig1. Server waiting for connections 5. On a separate terminal window, run the client using./tcp_client <localhost> Fig2. Client waiting for User input 6. Enter a character as input and it will be echoed back by the server (Refer Figure.3). Use netstat (on client side) to see the status of server-client communication. Fig3. Client input-output
Fig4. Server output 7. A concurrent server can allow multiple clients to connect simultaneously. Fig.5: Concurrent server 8. Kill the server by using a 'kill' command. Use netstat at the client side to see the status of the TCP communication. Write your observations. (To start the server again, wait for a couple of seconds (Why?) ). Experiment 2: Modification of the TCP Client-Server programs 1. Download tcp_client.c and tcp_server.c from the CMS Website (Concurrent server) 2. Modify the filenames as tcp_client_str.c and tcp_server_str.c 3. Work in teams of two. One of you run the server, and the other should connect as a client. One of you should edit the server code, and the other should edit the client code. Use appropriate IP address(es). 4. Modify the program such that a string of 14 characters is sent as a message. Ensure the server echoes back the same string. 5. Compile server first (as shown below). gcc -o tcp_server_str tcp_server_str.c 6. Similarly, compile the client using the following command. gcc -o tcp_client_str tcp_client_str.c 7. Run the server using the below command../tcp_server_str 8. The server will start, waiting for a client to connect. On a separate terminal window, run the client using./tcp_client_str <server IP> 9. Type hi im client! in the client terminal window. As you see the figure.7, the same message is echoed back from the server.
Fig6. Client sending string Fig7: Server receiving String 10. Close the client window while the server is running. Observe the socket communication details using netstat at the client side. Questions Answer the following questions based on your understanding of the experiments. 1. Which field in the socket function specifies the type of transport layer protocol (like TCP, UDP, etc.)? 2. What is the IP address and port no. of the server? 3. What is the purpose of bind function? 4. Which of the functions mentioned below are blocking calls? a. socket b. connect c. bind d. listen e. accept f. send g. recv h. close 5. Which function in the client program involved in connection establishment? 6. Which function in the server program involved in connection establishment?
UDP Socket Programming: A datagram socket is a type of connectionless network socket, which is the sending or receiving point for packet delivery services. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may arrive in any order and might not arrive at the receiving computer. A datagram socket provides a symmetric data exchange interface without requiring connection establishment. Each message carries the destination address. Tools used: gedit, terminal Experiment 3: Working of an UDP Client-Server program 1. Download udp_client.c and udp _server.c from the CMS Website. 2. The programs are partially complete. Complete the rest of the program, so that, it compiles and runs successfully (Implement echo server described in Experiment.2.) 3. Compile server first (as shown below). gcc -o udp_server udp_server.c 4. Similarly, compile the client using the following command. gcc -o udp_client udp_client.c 5. Run the server using the below command../udp_server 6. The server will start, waiting for a client to connect. On a separate terminal window, run the client using./udp_client <localhost> 7. Enter a character as input and it will be echoed back by the server (Refer Figure.8). Observe connection details in netstat. Fig8: Client sending and receiving a character 8. To stop the server and client, click Ctrl +C in their respective terminals. (To start the server again, wait for a couple of seconds).
Experiment 4: Modification of the UDP Client-Server programs 1. Download udp_client.c and udp_server.c from the CMS Website 2. Modify the filenames as udp_client_n.c and udp_server_n.c 3. Modify the program such that a string is sent in the message. Ensure the server echoes back the same string. (Hint: modify read and write functions). 4. Compile server first (as shown below). gcc -o udp_server_n udp_server_n.c 5. Similarly, compile the client using the following command. gcc -o udp_client_n udp_client_n.c 6. Run the server using the below command../udp_server_n 7. The server will start, waiting for a client to connect. On a separate terminal window, run the client using./udp_client_n <server IP or localhost> 8. Type hello in the client terminal window. As you see the Figure.9, the same message is echoed back from the server. Fig9: Client sending and receiving a string 9. To stop the server and client, click Ctrl +C in their respective terminals. (To start the server again, wait for a couple of seconds). Answer the following questions based on your understanding of the experiment. 1. Using netstat, did you observe any 'listen' state of UDP communication? Reason out. 2. Can you differentiate between TCP being stream-based and UDP being datagram based? References Unix Manual: http://man7.org/linux/man-pages/man2/socket.2.html