Socket Communication q Sockets provide an abstraction of
Socket Communication q Sockets provide an abstraction of two-point communication q The two sides only read/write without concern for how data is translated (encoded) and transmitted q Socket is a pair (address, port) Ø address: www. google. com, turing. cc. gettysburg. edu, 135. 24. 78. 123 Ø port: 21 (ftp), 22 (ssh), 23 (telnet), 80 (http), 101 (pop mail)
Socket Communication server (turing. cc. gettysburg. edu) client (135. 24. 78. 123) web server listen on port 80
Socket Communication server (turing. cc. gettysburg. edu) request web page client (135. 24. 78. 123) turing. cc…: 80 socket 135. 24. 78. 123: 1234 web server listen on port 80
Socket Communication server (turing. cc. gettysburg. edu) request web page client (135. 24. 78. 123) turing. cc…: 80 socket 135. 24. 78. 123: 1234 accept request socket tuing. cc…. . : 5789 web server listen on port 80
Socket Communication server (turing. cc. gettysburg. edu) client (135. 24. 78. 123) socket tuing. cc…. . : 5789 web server listen on port 80 socket 135. 24. 78. 123: 1234
Socket Communication server (turing. cc. gettysburg. edu) client (135. 24. 78. 123) socket tuing. cc…. . : 5789 socket 135. 24. 78. 123: 1234 request web page web server listen on port 80 client (135. 24. 78. 567) turing. cc…: 80 socket 135. 24. 78. 567: 1024
Socket Communication server (turing. cc. gettysburg. edu) client (135. 24. 78. 123) socket tuing. cc…. . : 5789 socket 135. 24. 78. 123: 1234 request web page web server listen on port 80 client (135. 24. 78. 567) turing. cc…: 80 socket 135. 24. 78. 567: 1024 accept request socket tuing. cc…. . : 8019
Socket Communication server (turing. cc. gettysburg. edu) client (135. 24. 78. 123) socket tuing. cc…. . : 5789 socket 135. 24. 78. 123: 1234 client (135. 24. 78. 567) web server listen on port 80 socket tuing. cc…. . : 8019 socket 135. 24. 78. 567: 1024
Multi-Threaded Server client (135. 24. 78. 123) request web page server socket (addr : port) turing. cc. gettysburg. edu : 80 socket tuing. cc…. . : 5789 turing. cc…: 80 socket (addr : port) 135. 24. 78. 123 : 1234 request web page turing. cc…: 80 client (128. 4. 99. 100) socket (addr : port) 128. 4. 99. 100 : 1234 socket tuing. cc…. . : 8019 listening thread (wait for connect)
Simple Server (one client) 1. Open the Server Socket 1. Server. Socket server = new Server. Socket(5678); 2. Wait for Client Request 1. Socket connection = server. accept(); 3. Create Streams for Communication Output. Stream os = connection. get. Output. Stream(); Data. Output. Stream output = new Data. Output. Stream (os); 4. Carry Out Communication 1. output. write. UTF("Hi there"); 5. Close Communication output. close(); os. close(); connection. close(); server. close();
Simple Server (one client) public static void main(String[] args) { // Register service on port 1234 Server. Socket server = new Server. Socket(1234); // Wait and accept a connection Socket connection = server. accept(); // Get a communication stream associated with the socket Output. Stream os = connection. get. Output. Stream(); Data. Output. Stream output = new Data. Output. Stream(os); // Send a string! output. write. UTF("Hi there"); // Close the connection output. close(); os. close(); connection. close(); server. close(); }
Multi-Threaded Server 1. Open the Server Socket 1. 2. Wait for Client Request 1. q Server. Socket server = new Server. Socket(5678); Socket connection = server. accept(); Spawn a client thread (give the connection) 1. Create Streams for Communication Output. Stream os = connection. get. Output. Stream(); Data. Output. Stream output = new Data. Output. Stream (os); 2. Carry Out Communication output. write. UTF("Hi there"); 3. Close Communication output. close(); os. close(); connection. close(); server. close();
Simple Server (one client) public static void main(String[] args) { // Register service on port 1234 Server. Socket server = new Server. Socket(1234); // Wait and accept a connection while (true) { Socket connection = server. accept(); Thread client = new Client. Thread(connection); } } Code for Client. Thread: // get communication channel associated with the socket Output. Stream os = connection. get. Output. Stream(); Data. Output. Stream output = new Data. Output. Stream (os); output. write. UTF("Hi there"); os. close(); output. close(); connection. close(); // communicate // close resources
Simple Client MIDlet (no change) 1. Create a Socket Connection Socket. Connection connection = (Socket. Connection)Connector. open("socket: //gbcs 10: 5678"); 2. Create Streams for Communication Input. Stream is = connection. open. Input. Stream(); Data. Input. Stream input = new Data. Input. Stream(is); 3. Carry Out Communication String data = new String (input. read. UTF()); 4. Close Communication is. close(); input. close(); connection. close();
Simple Client MIDlet (no change) try { Socket. Connection connection = (Socket. Connection) Connector. open("socket: //gbcs 10: 1234"); Input. Stream is = connection. open. Input. Stream(); Data. Input. Stream input = new Data. Input. Stream(is); String data = new String (input. read. UTF()); is. close(); input. close(); connection. close(); } catch (. . . ) { }
Lab Exercise Synchronous Multi-Threaded Chat Server
- Slides: 16