Computer/Human InteractionLecture 18Overview:Basic networkingNetwork applicationsJava socketsReferences:JTUTBasic networkingHosts have domain names (e.g., csserver.evansville.edu) and IP addresses (e.g., 10.10.0.9 – internal; 192.195.228.35 – external)Communication between hosts is through a port. Low-numbered ports are reserved for and assigned to well-known services. E.g., sshd listens on port 22. (List is in /etc/services on Unixes.)Network applicationsNetwork applications usually are structured in a client-server architecture. Often multiple clients talk to single server.Commonly used protocol is TCP/IP which is a reliable, connection-oriented protocol.Servers listen on (assigned) ports. Client uses ephemeral or short-lived ports, usually assigned automatically by TCP.Purple Pizza Parlor, v3Download from web or copy files /home/hwang/cs350/lecture17/*.*The application is a network client of a server that receives order data and writes it to the server’s filesystem.Client interface is same as Version 1 with an added field for the port number of server. Automatically tries to connect .Server consists of main server class and a handler thread class.SocketsA socket is an (IP address, port) pair that uniquely identifies an endpoint of a connection.A server typically will create a listening socket and wait for client connections. When a connection is received and accepted, the server will fork off a thread to handle the connection via a connected socket.Java SocketsTwo socket classesServerSocket: a listening socket; constructor receives port number, value of 0 has system choose a free port numberSocket: a connected socket; constructor receives host domain name and port number; handler socket created by calling accept method of ServerSocketServerSocket listenSocket = new ServerSocket(0);Socket connectSocket = listenSocket.accept();Socket clientSocket = new Socket(host, port);Server ThreadsPPP server creates a PizzaServerThread object to handle each connection.PizzaServerThread is a class that extends the Thread class. Must implement run() method that is called automatically when the thread is created.Socket I/OWrap socket streams into buffered streams to read and write data using readLine and println.BufferedReader in;PrintWriter out;in = new BufferedReader( new InputStreamReader( socket.getInputStream()));out = new PrintWriter( socket.getOutputStream(), true);String s = in.readLine();out.println(s);Message ProtocolClient and server must agree on the format of messages between them.PPP transmits/receives data in lines in order of the form. Address field may have more than one line and may have more than one topping, so transmission of those data have extra end data markers.NotesPPP client-server code is not as robust as it could be. It is possible to get into situations where the client locks up waiting for something.Handler threads automatically exit when the client disconnects, but the server process will execute indefinitely if put in the background.This example is given primarily for completeness and is not required. In particular, applets cannot write to the local machine, so they must talk to a server to save
View Full Document