Download presentation
Presentation is loading. Please wait.
Published byMyles Underwood Modified over 8 years ago
1
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection
2
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection void close()
3
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection void close() DIS openDataInputStream() DIS openInputStream() DOS openDataOutputStream() DOS openOutputStream()
4
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()
5
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
6
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: www.google.com, turing.cc.gettysburg.edu, 135.24.78.123 port: 21 (ftp), 22 (ssh), 23 (telnet), 80 (http), 101 (pop mail)
7
Socket Communication server (turing.cc.gettysburg.edu) client (135.24.78.123) web server listens on port 80
8
Socket Communication server (turing.cc.gettysburg.edu) client (135.24.78.123)request web page turing.cc…:80 socket 135.24.78.123:1234 web server listens on port 80
9
Socket Communication server (turing.cc.gettysburg.edu) client (135.24.78.123)request web page turing.cc…:80 socket 135.24.78.123:1234 socket tuing.cc…..:5789 accept request web server listens on port 80
10
Socket Communication server (turing.cc.gettysburg.edu) client (135.24.78.123) socket 135.24.78.123:1234 socket tuing.cc…..:5789 web server listens on port 80
11
Socket Communication server (turing.cc.gettysburg.edu) client (135.24.78.123) socket 135.24.78.123:1234 socket tuing.cc…..:5789 web server listens on port 80 client (135.24.78.567)request web page turing.cc…:80 socket 135.24.78.567:1024
12
Socket Communication server (turing.cc.gettysburg.edu) client (135.24.78.123) socket 135.24.78.123:1234 socket tuing.cc…..:5789 web server listens on port 80 client (135.24.78.567)request web page turing.cc…:80 socket 135.24.78.567:1024 socket tuing.cc…..:8019 accept request
13
Socket Communication server (turing.cc.gettysburg.edu) client (135.24.78.123) socket 135.24.78.123:1234 socket tuing.cc…..:5789 web server listens on port 80 client (135.24.78.567) socket 135.24.78.567:1024 socket tuing.cc…..:8019
14
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
15
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
16
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
17
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
18
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
19
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();
20
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)
21
Simple Client 1.Create a Socket Connection 2.Create Streams for Communication 3.Carry Out Communication 4.Close Communication
22
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
23
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
24
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
25
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();
26
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 (...) { }
27
Lab Exercise Client-Server Communication
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.