Presentation is loading. Please wait.

Presentation is loading. Please wait.

Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Similar presentations


Presentation on theme: "Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L."— Presentation transcript:

1 Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

2 Outline USC CSCI 201L2/11 ▪S▪Server Networking ▪C▪Client Networking ▪P▪Program

3 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 USC CSCI 201L3/11 Server Networking

4 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 USC CSCI 201L4/11 Server Networking

5 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 } USC CSCI 201L5/11 Server Networking

6 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! USC CSCI 201L6/11 Server Networking

7 Outline USC CSCI 201L7/11 ▪S▪Server Networking ▪C▪Client Networking ▪P▪Program

8 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 USC CSCI 201L8/11 Client Networking

9 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 } USC CSCI 201L9/11 Client Networking

10 Outline USC CSCI 201L10/11 ▪S▪Server Networking ▪C▪Client Networking ▪P▪Program

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. USC CSCI 201L11/11 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


Download ppt "Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L."

Similar presentations


Ads by Google