Networking Code CSCI 201 Principles of Software Development












- Slides: 12
Networking Code CSCI 201 Principles of Software Development Jeffrey Miller, Ph. D. jeffrey. miller@usc. edu
Outline • Server Networking • Client Networking • Program USC CSCI 201 L
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 Server. Socket 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 USC CSCI 201 L 3/12
Networking Diagram Client connects to Server IP address on port 6789 Server accepts connection to Client IP address on whatever port it dynamically selected Client Server Listening on port 6789 on what IP address? 4/12
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 be synchronous and only need one program to send data then wait for a response – that would not require multi-threading USC CSCI 201 L 5/12
Server Networking Example (no multi-threading) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import java. io. *; // to save space 31 if (ss != null) import java. net. *; // to save space 32 ss. close(); public class Networking. Server { 33 } catch (IOException ioe) { public Networking. Server() { 34 System. out. println(“ioe: “ + ioe. get. Message()); Server. Socket ss = null; 35 } Socket s = null; 36 } // ends finally Print. Writer pw = null; 37 } Buffered. Reader br = null; 38 public static void main(String [] args) { try { 39 new Networking. Server(); System. out. println("Starting Server"); 40 } ss = new Server. Socket(6789); 41 } s = ss. accept(); br = new Buffered. Reader(new Input. Stream. Reader(s. get. Input. Stream())); pw = new Print. Writer(s. get. Output. Stream()); String line = br. read. Line(); System. out. println("Line Received: " + line); String str = "CSCI 201"; System. out. println("Sending: " + str); pw. println(str); pw. flush(); } catch (IOException ioe) { System. out. println("IOE: " + ioe. get. Message()); } finally { try { if (pw != null) pw. close(); if (br != null) br. close(); if (s != null) s. close(); USC CSCI 201 L 6/12
Flushing ▪ Operating systems try to optimize networking similar to how they optimize file I/O ▪ Data is written into a buffer before it is sent along the socket ▪ The contents of the buffer will not be transmitted over the network until it fills up unless we explicitly flush the data ▪ Never forget to flush! USC CSCI 201 L 7/12
Outline • Server Networking • Client Networking • Program USC CSCI 201 L
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 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 USC CSCI 201 L 9/12
Client Networking Example (no multi-threading) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import java. io. *; // to save space import java. net. Socket; public class Networking. Client { public Networking. Client() { Socket s = null; Buffered. Reader br = null; Print. Writer pw = null; try { System. out. println("Starting Client"); s = new Socket("localhost", 6789); br = new Buffered. Reader(new Input. Stream. Reader(s. get. Input. Stream())); pw = new Print. Writer(s. get. Output. Stream()); String str = "Line being sent"; System. out. println("Sending: " + str); pw. println(str); pw. flush(); 33 } // ends Networking. Client() String line = br. read. Line(); 34 public static void main(String [] args) { System. out. println("Line Received: " + line); 35 new Networking. Client(); } catch (IOException ioe) { 36 } System. out. println("IOE: " + ioe. get. Message()); 37 } } finally { try { if (pw != null) pw. close(); if (br != null) br. close(); if (s != null) s. close(); } catch (IOException ioe) { System. out. println(“ioe: “ + ioe. get. Message()); } } // ends finally USC CSCI 201 L 10/12
Outline • Server Networking • Client Networking • Program USC CSCI 201 L
Program ▪ Write a single threaded chat program that allows two clients to communicate with each other in a synchronous manner – one user can only send a message after the other has sent one. C: >java Chat. Client localhost 6789 Hello, how are you? Them: Fine, and you? Good, thanks. C: >java Chat. Client localhost 6789 Them: Hello, how are you? Fine, and you? Them: Good, thanks. • Program USC CSCI 201 L 12/12