CSE4232/5232 Spring 2006 Exam 1, open book, open notes. Name ______________ 1. How many bytes represent one character in each of the following encodings? (2 pts each) ANSWER ------ ASCII 1 UTF-8 1 to 3 ISO8859_1 1 latin1 1 Unicode 2 2. Fill in the number which describes the purpose of each of the following IP or TCP packet header fields (3 pts each). ANSWER ------ IP checksum 10 1. Total Transmission Length. IP ID number 9 2. Identifies application protocol. IP MF flag 19 3. Identifies transport layer protocol. IP protocol field 3 4. Identifies data link protocol. IP TTL 18 5. Request to open connection. 6. Close connection with handshake. TCP SYN flag 5 7. Close connection without handshake. TCP PSH flag 12 8. Number of packets sent. TCP RST flag 7 9. Identifier for packet reassembly. TCP acknowledgement no. 13 10. Transmission error detection. TCP window size 14 11. Packet contains urgent data. 12. Packet contains data. 13. Next sequence number I am expecting. 14. Number of bytes I can receive right now. 15. IP fragmentation not supported. 16. Number of packets I have received. 17. Size of payload. 18. Number of router hops allowed. 19. Not the last fragment. 20. Other (specify): 3. Write a java method that takes a hostname and port number of a server, and a message to send to it, then returns the first line of the response. Any exceptions should be passed to the caller. For example (20 pts): // Prints "HTTP/1.1 200 OK" if all goes well System.out.println(getFromServer("www.google.com", 80, "GET /\r\n")); // ANSWER String getFromServer(String host, int port, String message) throws Exception { Socket s=new Socket(host, port); InputStream in=s.getInputStream(); OutputStream out=s.getOutputStream(); PrintWriter pw=new PrintWriter(new OutputStreamWriter(out)); BufferedReader br=new BufferedReader(new InputStreamReader(in)); pw.print(message); pw.flush(); return br.readLine(); } 4. Write a Counter server. A Counter server listens on port 12345, ignores any input, and outputs a count (as an ASCII string) of the number of clients that have connected since it started, including the current connection, then closes the connection. Handle each client in a separate thread. For example, if the server was running locally (40 pts, use back of page). System.out.println(getFromServer("localhost", 12345, "")); // "1" System.out.println(getFromServer("localhost", 12345, "")); // "2" // ANSWER. This solution uses just one class, although it might be more // conventional to have a separate class for the client handler thread. // In either case the count must be incremented before it is passed to // the thread so that two threads do not try to increment the // count at the same time. Alternatively, the count can be stored in a // static variable accessible to all threads and the code to increment // it and read its value be synchronized. import java.io.*; import java.net.*; public class CounterServer extends Thread { Socket socket; int count; // Run the server public static void main(String[] args) throws Exception { ServerSocket serverSocket=new ServerSocket(12345); for (int i=1; ; ++i) { Socket s=serverSocket.accept(); new CounterServer(s, i); } } // Create a new thread to print i to client socket s CounterServer(Socket s, int i) throws Exception { socket=s; count=i; start(); } // Handle the client public void run() { try { PrintWriter pr=new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); pr.println(count); pr.close(); } catch (Exception x) {} } }