Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP://WWW-SCF.USC.EDU/~CSCI201 USC CSCI 201L
Outline Server Networking Client Networking Program USC CSCI 201L 2/11
Server Software A server application is only able to serve requests that are addressed to the computer on which the server application is running A server program cannot listen to a port on another physical server So, the only data the server application needs is the port on which to listen The ServerSocket constructor only takes a port as a parameter Multiple networked applications can be running on the same computer as long as they are all using different ports Server Networking USC CSCI 201L 3/11
Multi-Threading with Networking Multi-threading is usually necessary with networking since there are two things that often are done at the same time The ability to send data The ability to receive data If sending and receiving are not performed in series, multi-threading will be needed Some applications may only need one program to send data then wait for a response that would not require multi-threading Server Networking USC CSCI 201L 4/11
Server Networking Example (no multi-threading) 1 import java.io.bufferedreader; 2 import java.io.ioexception; 3 import java.io.inputstreamreader; 4 import java.io.printwriter; 5 import java.net.serversocket; 6 import java.net.socket; 7 8 public class NetworkingServer { 9 10 public NetworkingServer() { 11 try { 12 System.out.println("Starting Server"); 13 ServerSocket ss = new ServerSocket(6789); 14 Socket s = ss.accept(); 15 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); 16 PrintWriter pw = new PrintWriter(s.getOutputStream()); 17 18 String line = br.readline(); 19 System.out.println("Line Received: " + line); 20 String str = "CSCI 201"; 21 System.out.println("Sending: " + str); 22 pw.println(str); 23 pw.flush(); 24 pw.close(); 25 br.close(); 26 s.close(); 27 ss.close(); 28 } catch (IOException ioe) { 29 System.out.println("IOE: " + ioe.getmessage()); 30 } 31 } 32 public static void main(string [] args) { 33 new NetworkingServer(); 34 } 35 } Server Networking USC CSCI 201L 5/11
Flushing Operating systems try to optimize networking similar to how they optimize file I/O A buffer exists where data is written into before sending along the socket The buffer will not be transmitted over the network until it fills up unless we explicitly flush the data Never forget to flush! Server Networking USC CSCI 201L 6/11
Outline Server Networking Client Networking Program USC CSCI 201L 7/11
Client Software A client application is able to connect to any server application to which it has access, whether running on the same computer as the client or a different computer The client application needs both the IP address of the server and the port on which the server application with which it wants to communicate is listening Remember that multiple server applications can be running on the same computer as long as they are listening on different ports A Socket is the combination of the IP address and port number needed by the client Client Networking USC CSCI 201L 8/11
Client Networking Example (no multi-threading) 1 import java.io.bufferedreader; 2 import java.io.ioexception; 3 import java.io.inputstreamreader; 4 import java.io.printwriter; 5 import java.net.socket; 6 7 public class NetworkingClient { 8 9 public NetworkingClient() { 10 try { 11 System.out.println("Starting Client"); 12 Socket s = new Socket("localhost", 6789); 13 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); 14 PrintWriter pw = new PrintWriter(s.getOutputStream()); 15 16 String str = "Line being sent"; 17 System.out.println("Sending: " + str); 18 pw.println(str); 19 pw.flush(); 20 String line = br.readline(); 21 System.out.println("Line Received: " + line); 22 pw.close(); 23 br.close(); 24 s.close(); 25 } catch (IOException ioe) { 26 System.out.println("IOE: " + ioe.getmessage()); 27 } 28 } 29 public static void main(string [] args) { 30 new NetworkingClient(); 31 } 32 } Client Networking USC CSCI 201L 9/11
Outline Server Networking Client Networking Program USC CSCI 201L 10/11
Program Write a multi-threaded chat program that allows multiple clients to communicate with each other in an asynchronous manner. The clients should communicate with a server program. C:>java ChatClient localhost 6789 hello Them: how are you? fine, and you? Them: good, thanks C:>java ChatClient localhost 6789 Them: hello how are you? Them: fine, and you? good, thanks Program USC CSCI 201L 11/11