1 / 20

Comunicação entre processos baseada em troca de mensagens através de sockets sobre IP

Comunicação entre processos baseada em troca de mensagens através de sockets sobre IP. Alcides Calsavara. Sockets. Basic TCP/IP mechanism Three access facilities: connection-oriented: Socket , ServerSocket datagram-oriented: DatagramSocket MulticastSocket raw IP data: SocketImpl.

hamlet
Télécharger la présentation

Comunicação entre processos baseada em troca de mensagens através de sockets sobre IP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Comunicação entre processos baseada em troca de mensagens através de sockets sobre IP Alcides Calsavara

  2. Sockets • Basic TCP/IP mechanism • Three access facilities: • connection-oriented: • Socket, ServerSocket • datagram-oriented: • DatagramSocket • MulticastSocket • raw IP data: • SocketImpl

  3. Connection-oriented sockets • Built over TCP/IP • Reliable services: • no data losses in the network • data ordering is insured • Trade-off: slower than datagrams • Data streams can be created • Asymmetric behaviors • client side Vs. server side

  4. Connection: client actions 1. Open the communication socket import java.io.* ; // streams import java.net.* ; // sockets Socket clientSocket = new Socket (“www.javasoft.com”, 80) ;

  5. Connection: client actions 2. Get socket input/output streams: DataInputStream inbound = new DataInputStream ( clientSocket.getInputStream( ) ) ; DataOutputStream outbound = new DataOutputStream ( clientSocket.getOutputStream( ) ) ;

  6. Connection: client actions 3. Use the input/output streams outbound.writeInt( 3 ); outbound.writeUTF( “Hello” ); int k = inbound.readInt( ); String s = inbound.readUTF() ; ...

  7. Connection: client actions 4. Close in/out streams inbound.close () ; outbound.close () ; 5. Close the socket clientSocket.close() ;

  8. Connection: server actions 1. Create server-side socket: ServerSocket serverSocket = new ServerSocket (80, 5) ; 2. Wait for client connections: Socket clientSocket = serverSocket.accept () ;

  9. Connection: server actions 3. Create client in/out streams: DataInputStream inbound = new DataInputStream ( clientSocket.getInputStream( ) ) ; DataOutputStream outbound = new DataOutputStream ( clientSocket.getOutputStream( ) ) ;

  10. Connection: server actions 4. Dialog with the client: int k = inbound.readInt( ); String s = inbound.readUTF() ; outbound.writeInt( 3 ); outbound.writeUTF( “Hello” );

  11. Connection: server actions 5. Close streams and client socket: inbound.close () ; outbound.close () ; clientSocket.close() ; 6. Close server-side socket: serverSocket.close() ;

  12. Datagram sockets • Based on UDP/IP • unreliable service: • messages can be lost • messages ordering is not guaranteed • but it’s much faster the TCP/IP • Each message is a “datagram”: • [sender, receiver, contents]

  13. Datagram, sender side 1. Client socket creation: // sender socket doesn’t need // a special port number DatagramSocket clientSocket = new DatagramSocket () ;

  14. Datagram, sender side 2. Build and send the datagram: InetAddress addr=InetAddress.getByName (“www.javasoft.com”) ; String toSend = “That’s my question!” ; byte[] buffer = toSend.getBytes() ; // datagram to receiver’s port 4545 DatagramPacket question = new DatagramPacket (buffer, buffer.length, addr, 4545) ; clientSocket.send (question) ;

  15. Datagram, sender side 3. Receive and open the answer: DatagramPacket answer = new DatagramPacket (new byte[512], 512); clientSocket.receive (answer) ; System.out.println (answer.getData() + “\n” + answer.getLength() + “\n” + answer.getAddress() + “\n” + answer.getPort() ) ;

  16. Datagram, sender side 4. Close the client socket: clientSocket.close() ;

  17. Datagram, receiver side 1. Create a server socket: // listens on port 4545 DatagramSocket serverSocket = new DatagramSocket (4545) ;

  18. Datagram, receiver side 2. Receive a datagram: DatagramPacket question = new DatagramPacket (new byte[512], 512) ; serverSocket.receive (question) ;

  19. Datagram, receiver side 3. Send the answer datagram: String toSend = “That’s the answer !” ; byte[] buffer = toSend.getBytes() ; DatagramPacket answer = new DatagramPacket (buffer, buffer.length, question.getAddress(), // sender info question.getPort() ) ; // sender info serverSocket.send (answer) ;

  20. Datagram, receiver side 4. Close the server socket: serverSocket.close() ;

More Related