Presentation is loading. Please wait.

Presentation is loading. Please wait.

Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated.

Similar presentations


Presentation on theme: "Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated."— Presentation transcript:

1 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)

2 Socket Communication web server listen on port 80
server (turing.cc.gettysburg.edu) client ( ) web server listen on port 80

3 Socket Communication web server listen on port 80
server (turing.cc.gettysburg.edu) request web page turing.cc…:80 client ( ) socket :1234 web server listen on port 80

4 Socket Communication web server listen on port 80
server (turing.cc.gettysburg.edu) request web page turing.cc…:80 client ( ) socket :1234 accept request socket tuing.cc…..:5789 web server listen on port 80

5 Socket Communication web server listen on port 80
server (turing.cc.gettysburg.edu) client ( ) socket tuing.cc…..:5789 socket :1234 web server listen on port 80

6 Socket Communication web server listen on port 80
server (turing.cc.gettysburg.edu) client ( ) socket tuing.cc…..:5789 socket :1234 request web page turing.cc…:80 client ( ) web server listen on port 80 socket :1024

7 Socket Communication web server listen on port 80
server (turing.cc.gettysburg.edu) client ( ) socket tuing.cc…..:5789 socket :1234 request web page turing.cc…:80 client ( ) web server listen on port 80 socket :1024 accept request socket tuing.cc…..:8019

8 Socket Communication web server listen on port 80
server (turing.cc.gettysburg.edu) client ( ) socket tuing.cc…..:5789 socket :1234 client ( ) web server listen on port 80 socket tuing.cc…..:8019 socket :1024

9 Multi-Threaded Server
client ( ) request web page turing.cc…:80 server socket (addr : port) turing.cc.gettysburg.edu : 80 socket (addr : port) : 1234 socket tuing.cc…..:5789 request web page turing.cc…:80 client ( ) socket (addr : port) : 1234 socket tuing.cc…..:8019 listening thread (wait for connect)

10 Simple Server (one client)
Open the Server Socket ServerSocket server = new ServerSocket(5678); Wait for Client Request Socket connection = server.accept(); Create Streams for Communication OutputStream os = connection.getOutputStream(); DataOutputStream output = new DataOutputStream (os); Carry Out Communication output.writeUTF("Hi there"); Close Communication output.close(); os.close(); connection.close(); server.close();

11 Simple Server (one client)
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(); }

12 Multi-Threaded Server
Open the Server Socket ServerSocket server = new ServerSocket(5678); Wait for Client Request Socket connection = server.accept(); Spawn a client thread (give the connection) Create Streams for Communication OutputStream os = connection.getOutputStream(); DataOutputStream output = new DataOutputStream (os); Carry Out Communication output.writeUTF("Hi there"); Close Communication output.close(); os.close(); connection.close(); server.close();

13 Simple Server (one client)
public static void main(String[] args) { // Register service on port 1234 ServerSocket server = new ServerSocket(1234); // Wait and accept a connection while (true) { Socket connection = server.accept(); Thread client = new ClientThread(connection); } Code for ClientThread: // get communication channel associated with the socket OutputStream os = connection.getOutputStream(); DataOutputStream output = new DataOutputStream (os); output.writeUTF("Hi there"); // communicate os.close(); output.close(); // close resources connection.close();

14 Simple Client MIDlet (no change)
Create a Socket Connection SocketConnection connection = (SocketConnection)Connector.open("socket://gbcs10:5678"); Create Streams for Communication InputStream is = connection.openInputStream(); DataInputStream input = new DataInputStream(is); Carry Out Communication String data = new String (input.readUTF()); Close Communication is.close(); input.close(); connection.close();

15 Simple Client MIDlet (no change)
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 (. . .) {

16 Synchronous Multi-Threaded Chat Server
Lab Exercise Synchronous Multi-Threaded Chat Server


Download ppt "Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated."

Similar presentations


Ads by Google