Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection void close()
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection void close() DIS openDataInputStream() DIS openInputStream() DOS openDataOutputStream() DOS openOutputStream()
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection void close() DIS openDataInputStream() DIS openInputStream() DOS openDataOutputStream() DOS openOutputStream() String getAddress() String getLocalAddress() int getLocalPort() int getPort()
Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated (encoded) and transmitted
Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated (encoded) and transmitted Socket is a pair (address, port) address: turing.cc.gettysburg.edu, port: 21 (ftp), 22 (ssh), 23 (telnet), 80 (http), 101 (pop mail)
Socket Communication server (turing.cc.gettysburg.edu) client ( ) web server listens on port 80
Socket Communication server (turing.cc.gettysburg.edu) client ( )request web page turing.cc…:80 socket :1234 web server listens on port 80
Socket Communication server (turing.cc.gettysburg.edu) client ( )request web page turing.cc…:80 socket :1234 socket tuing.cc…..:5789 accept request web server listens on port 80
Socket Communication server (turing.cc.gettysburg.edu) client ( ) socket :1234 socket tuing.cc…..:5789 web server listens on port 80
Socket Communication server (turing.cc.gettysburg.edu) client ( ) socket :1234 socket tuing.cc…..:5789 web server listens on port 80 client ( )request web page turing.cc…:80 socket :1024
Socket Communication server (turing.cc.gettysburg.edu) client ( ) socket :1234 socket tuing.cc…..:5789 web server listens on port 80 client ( )request web page turing.cc…:80 socket :1024 socket tuing.cc…..:8019 accept request
Socket Communication server (turing.cc.gettysburg.edu) client ( ) socket :1234 socket tuing.cc…..:5789 web server listens on port 80 client ( ) socket :1024 socket tuing.cc…..:8019
Simple Server (one client) 1.Open the Server Socket 2.Wait for Client Request 3.Create Streams for Communication 4.Carry Out Communication 5.Close Communication
Simple Server (one client) 1.Open the Server Socket ServerSocket server = new ServerSocket(5678); 2.Wait for Client Request 3.Create Streams for Communication 4.Carry Out Communication 5.Close Communication
Simple Server (one client) 1.Open the Server Socket ServerSocket server = new ServerSocket(5678); 2.Wait for Client Request Socket connection = server.accept(); 3.Create Streams for Communication 4.Carry Out Communication 5.Close Communication
Simple Server (one client) 1.Open the Server Socket ServerSocket server = new ServerSocket(5678); 2.Wait for Client Request Socket connection = server.accept(); 3.Create Streams for Communication OutputStream os = connection.getOutputStream(); DataOutputStream output = new DataOutputStream (os); 4.Carry Out Communication 5.Close Communication
Simple Server (one client) 1.Open the Server Socket ServerSocket server = new ServerSocket(5678); 2.Wait for Client Request Socket connection = server.accept(); 3.Create Streams for Communication OutputStream os = connection.getOutputStream(); DataOutputStream output = new DataOutputStream (os); 4.Carry Out Communication output.writeUTF("Hi there"); 5.Close Communication
Simple Server (one client) 1.Open the Server Socket ServerSocket server = new ServerSocket(5678); 2.Wait for Client Request Socket connection = server.accept(); 3.Create Streams for Communication OutputStream os = connection.getOutputStream(); DataOutputStream output = new DataOutputStream (os); 4.Carry Out Communication output.writeUTF("Hi there"); 5.Close Communication output.close(); os.close(); connection.close(); server.close();
public static void main(String[] args) { // Register service on port 1234 ServerSocket server = new ServerSocket(1234); // Wait and accept a connection Socket connection = server.accept(); // Get a communication stream associated with the socket OutputStream os = connection.getOutputStream(); DataOutputStream output = new DataOutputStream(os); // Send a string! output.writeUTF("Hi there"); // Close the connection output.close(); os.close(); connection.close(); server.close(); } Simple Server (one client)
Simple Client 1.Create a Socket Connection 2.Create Streams for Communication 3.Carry Out Communication 4.Close Communication
Simple Client 1.Create a Socket Connection SocketConnection connection = (SocketConnection)Connector.open("socket://gbcs10:123"); 2.Create Streams for Communication 3.Carry Out Communication 4.Close Communication
Simple Client 1.Create a Socket Connection SocketConnection connection = (SocketConnection)Connector.open("socket://gbcs10:5678"); 2.Create Streams for Communication InputStream is = connection.openInputStream(); DataInputStream input = new DataInputStream(is); 3.Carry Out Communication 4.Close Communication
Simple Client 1.Create a Socket Connection SocketConnection connection = (SocketConnection)Connector.open("socket://gbcs10:5678"); 2.Create Streams for Communication InputStream is = connection.openInputStream(); DataInputStream input = new DataInputStream(is); 3.Carry Out Communication String data = new String (input.readUTF()); 4.Close Communication
Simple Client 1.Create a Socket Connection SocketConnection connection = (SocketConnection)Connector.open("socket://gbcs10:5678"); 2.Create Streams for Communication InputStream is = connection.openInputStream(); DataInputStream input = new DataInputStream(is); 3.Carry Out Communication String data = new String (input.readUTF()); 4.Close Communication is.close(); input.close(); connection.close();
Simple Client try { SocketConnection connection = (SocketConnection) Connector.open("socket://gbcs10:1234"); InputStream is = connection.openInputStream(); DataInputStream input = new DataInputStream(is); String data = new String (input.readUTF()); is.close(); input.close(); connection.close(); } catch (...) { }
Lab Exercise Client-Server Communication