Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review Communication via paired sockets, one local and one remote

Similar presentations


Presentation on theme: "Review Communication via paired sockets, one local and one remote"— Presentation transcript:

1 Socket-based IPC EEE466 Reference: [CDK00] chapter 4, [CW01] “All About Sockets”

2 Review Communication via paired sockets, one local and one remote
Listen, Accept, Connect

3 Principles Communication via paired sockets, one local and one remote
Sockets may be connectionless no notion of an ongoing connection between parties each message a separate, self-contained entity in sockets, implemented using datagrams connection-oriented ongoing connection between parties first establish connection, then carry on conversation in sockets, implemented using streams looks like file IO to the programmer

4 Mechanism: Datagrams server listens on a datagram socket for a packet
client creates a packet data in a packet is in a byte array; most Java objects can be serialized using ObjectOutputStream sends it out on a local datagram socket addressed to the server’s socket on server end, received packet indicates client socket; this allows server to respond if protocol requires, client may listen for response

5 Mechanism: Datagrams Example: client sends server a text string,
server sends it back, client prints returned string, repeats twenty times

6 UDP Client import java.net.*; import java.io.*;
public class UDPClient{ public static void main(String args[]){ // args give message contents and destination hostname DatagramSocket aSocket = null; try { aSocket = new DatagramSocket(); byte [] m = args[0].getBytes(); InetAddress aHost = InetAddress.getByName(args[1]); int serverPort = 6789; DatagramPacket request = new DatagramPacket(m,args[0].length(), aHost, serverPort); aSocket.send(request); byte[] buffer = new byte[1000]; DatagramPacket reply = new DatagramPacket(buffer, buffer.length); aSocket.receive(reply); System.out.println("Reply: " + new String(reply.getData())); }catch (SocketException e){System.out.println("Socket: " + e.getMessage()); }catch (IOException e){System.out.println("IO: " + e.getMessage()); }finally {if(aSocket != null) aSocket.close();} }

7 UDP Server import java.net.*; import java.io.*;
public class UDPServer{ public static void main(String args[]){ DatagramSocket aSocket = null; try{ aSocket = new DatagramSocket(6789); // create socket at agreed port byte[] buffer = new byte[1000]; while(true){ DatagramPacket request = new DatagramPacket(buffer, buffer.length); aSocket.receive(request); DatagramPacket reply = new DatagramPacket(request.getData(), request.getLength(), request.getAddress(), request.getPort()); aSocket.send(reply); } }catch (SocketException e){System.out.println("Socket: " + e.getMessage()); }catch (IOException e) {System.out.println("IO: " + e.getMessage()); }finally {if(aSocket != null) aSocket.close();}

8 Mechanism: Streaming Sockets
server listens on a server socket for a connection request (accept()) client makes connection request on a local socket, addressed to a server socket server receives the request which creates a new local socket, connects this to client socket client and server communicate until complete looks like file IO can use either Input/Output Streams (for arbitrary bytes) or Readers/Writers (for text) client and server close sockets

9 Mechanism: Streaming Sockets
Example: client sends server a text string, server sends it back, client prints returned string, repeats twenty times

10 TCP Client import java.net.*; import java.io.*;
public class TCPClient { public static void main (String args[]) { // arguments supply message and hostname Socket s = null; try{ int serverPort = 7896; s = new Socket(args[1], serverPort); DataInputStream in = new DataInputStream( s.getInputStream()); DataOutputStream out =new DataOutputStream( s.getOutputStream()); out.writeUTF(args[0]); // UTF is a string // encoding see Sn. 4.4 String data = in.readUTF(); // read a line of data //from the stream System.out.println("Received: "+ data) ; }catch (UnknownHostException e){System.out.println("Socket:"+e.getMessage()); }catch (EOFException e){System.out.println("EOF:"+e.getMessage()); }catch (IOException e){System.out.println("readline:"+e.getMessage()); }finally {if(s!=null) try {s.close();} catch (IOException e) {System.out.println("close:"+e.getMessage()); }

11 TCP Server import java.net.*; import java.io.*;
public class TCPServer { public static void main (String args[]) { try{ int serverPort = 7896; // the server port ServerSocket listenSocket = new ServerSocket(serverPort); while(true) { Socket clientSocket = listenSocket.accept(); Connection c = new Connection(clientSocket); } } catch(IOException e) {System.out.println("Listen socket:"+e.getMessage());} class Connection extends Thread { DataInputStream in; DataOutputStream out; Socket clientSocket; public Connection (Socket aClientSocket) { try { clientSocket = aClientSocket; in = new DataInputStream( clientSocket.getInputStream()); out =new DataOutputStream( clientSocket.getOutputStream()); this.start(); } catch(IOException e) {System.out.println("Connection:"+e.getMessage());} public void run(){ try { // an echo server String data = in.readUTF(); // read a line of data from the stream out.writeUTF(data); }catch (EOFException e){System.out.println("EOF:"+e.getMessage()); } catch(IOException e) {System.out.println("readline:"+e.getMessage()); } finally{ try {clientSocket.close();} catch (IOException e){/*close failed*/}

12 Connectionless vs Connection-Oriented Protocol
e.g. UDP vs TCP Both use a socket required to know the server host address and port number for specific services. Both required to marshal in/out data manually. Connection-Oriented build on top of a connectionless protocol. Connection-oriented guarantees datagram delivery. the order in which datagrams are delivered to either side (client/server) is as they were sent. This is why it is referred to as a data stream.

13 Communication Protocol Stack/Layers
Applications HTTP FTP Telnet SNMP Jini RMI UDP TCP WAP Connectionless like UDP IP Data Link Hardware


Download ppt "Review Communication via paired sockets, one local and one remote"

Similar presentations


Ads by Google