Socket Programming: exercises
Transcription
Socket Programming: exercises
SC250 Computer Networking I Socket Programming: Exercises Prof. Matthias Grossglauser / Dr. Jörg Widmer LCA/I&C http://lcawww.epfl.ch 1 Exercise 1 Assume you have our Upcase-client on one host and the server on another host. a) Suppose you run the TCP client without starting the server. What happens exactly and why? b) Suppose you run a UDP client without starting a UDP server. What is different from the case of TCP and why? c) What happens with TCP and UDP if you start the server after the client but before typing any input? d) What happens if you connect the TCP client to a web server running on port 80 instead of the Upcase server? 2 Solution to Exercise 1 a) If you run a TCP client first, the client will attempt to make a TCP connection with a non-existent server process. Creating the client socket will fail. b) With UDP, the client doesn't establish a connection with the server. Creating a socket works but sending a datagram will fail. c) With TCP, again creating the socket will fail. Since no connection is established with UDP, everything should work fine if you first run the client, then run the server, and then type some input into the keyboard. d) The client will establish a connection. If the user types anything that is not HTTP, the server will respond with an error message. If the user types GET / HTTP/1.0 the server delivers the corresponding web page. 3 Exercise 2 TCP connection requests (SYN) are buffered at the server socket. The server then performs the three-way handshake. An accept() returns the next connection request where the three-way handshake is completed. Requests with and without completed three-way handshake end up in the same buffer. a) What happens to a new request if the buffer is full? b) What happens if a client sends a large number of bogus SYNs to a server without ever completing the threeway handshake (SYN flooding)? 4 Solution to Exercise 2 a) The request cannot be served and the SYN packet is dropped. b) The buffer at the server is full of SYN packets where the three-way handshake will never complete. Other clients cannot access the server since the buffer for connection requests is full. A better implementation would be to have separate buffers for unfinished connection requests and connection requests with completed three-way handshake. 5 Exercise 3 a) In the transport header of a packet you have the source port and the destination port. The receiver needs the destination port for demultiplexing but what is the source port used for? b) Assume a server has a server socket open on port X for incoming TCP connections. What happens if on the same server you open a UDP socket to receive datagrams also using number X? Are there conflicts? c) Is it possible to have multiple threads to handle clients simultaneously with a UDP server? If yes, how? If no, why not? d) What about using multiple threads and multiple sockets for receiving as with TCP? 6 Solution to Exercise 3 a) The TCP receiver needs the source port to know where to send the SYN/ACK to. For UDP, the source port is not as important as for TCP but is usually used to determine where to send responses to. b) There are no conflicts. The OS has 2^16 ports for TCP and a separate 2^16 ports for UDP. Remember, a connection is uniquely identified by source IP and port, destination IP and port, AND protocol type. c) This is no problem. Create a new thread and hand over the clients datagram. The thread can then process the request in the background and then send the response. d) It is possible, but there is no such thing as a server socket for UDP. Thus, this has to be done "by hand". Create a new socket upon first client contact and send the new port number back to the client so it can send subsequent datagrams there. This is rarely done! 7 Exercise 4 Rewrite the UDP Java server using multiple threads. (Simply annotate in the source code with what changes have to be made.) What do you put in a separate thread? Why? Should you use a new socket for each client? 8 Exercise 4: UDPServerMultiThreads There are several correct ways to do this. This is one example: import java.io.*; import java.net.*; class UDPServerMultiThreads { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); System.out.println("I am waiting on port 9876"); 9 Exercise 4: UDPServerMultiThreads while(true) { byte[] receiveData = new byte[1024]; DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); ServerThread serverT = new ServerThread( receivePacket); serverT.startSocket(); } } } 10 Exercise 4: ServerThread import java.io.*; import java.net.*; public class ServerThread extends Thread { private DatagramPacket receivePacket; public ServerThread(DatagramPacket receivePacket) throws SocketException { this.receivePacket = receivePacket; } 11 Exercise 4: ServerThread public void startSocket() throws Exception { byte[] sendData = new byte[1024]; DatagramSocket responseSocket = new DatagramSocket(); String clientSentence = new String (receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); System.out.println(port); 12 Exercise 4: ServerThread String capitalizedSentence = clientSentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket( sendData, sendData.length, IPAddress, port); responseSocket.send(sendPacket); responseSocket.close(); } } 13 Exercise 5: using UDP vs TCP Consider a file transfer application that transmits data at a steady rate (for example, the sender generates a N-bit unit of data every k time units, where k is small and fixed). Also, when such an application starts, it will stay on for a relatively long period of time. Which transport protocol is more appropriate for this application, TCP or UDP? Why? Assume you have web transfers where each page fits into one packet. What are the advantages and disadvantages of using UDP instead of TCP? 14 Solution to Exercise 5: UDP vs TCP For such file transfers, TCP is preferable but what to use is not always that clear (in theory, one could do HTTP with UDP). TCP: useful if the application involves long sessions with predictable smooth bandwidth requirements data needs to be transmitted in order and with no losses; connection-oriented architecture helps with no significant waste at application level overhead costs and delay of three-way handshake can be amortized over the length sessions UDP: useful if the application involves short transactions with only a few packets to be sent (no need for flow control, avoid overhead of three-way handshake) error recovery (e.g. retransmission/ordering) has to be done at application layer if necessary 15