200 likes | 390 Views
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.
E N D
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
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
Connection: client actions 1. Open the communication socket import java.io.* ; // streams import java.net.* ; // sockets Socket clientSocket = new Socket (“www.javasoft.com”, 80) ;
Connection: client actions 2. Get socket input/output streams: DataInputStream inbound = new DataInputStream ( clientSocket.getInputStream( ) ) ; DataOutputStream outbound = new DataOutputStream ( clientSocket.getOutputStream( ) ) ;
Connection: client actions 3. Use the input/output streams outbound.writeInt( 3 ); outbound.writeUTF( “Hello” ); int k = inbound.readInt( ); String s = inbound.readUTF() ; ...
Connection: client actions 4. Close in/out streams inbound.close () ; outbound.close () ; 5. Close the socket clientSocket.close() ;
Connection: server actions 1. Create server-side socket: ServerSocket serverSocket = new ServerSocket (80, 5) ; 2. Wait for client connections: Socket clientSocket = serverSocket.accept () ;
Connection: server actions 3. Create client in/out streams: DataInputStream inbound = new DataInputStream ( clientSocket.getInputStream( ) ) ; DataOutputStream outbound = new DataOutputStream ( clientSocket.getOutputStream( ) ) ;
Connection: server actions 4. Dialog with the client: int k = inbound.readInt( ); String s = inbound.readUTF() ; outbound.writeInt( 3 ); outbound.writeUTF( “Hello” );
Connection: server actions 5. Close streams and client socket: inbound.close () ; outbound.close () ; clientSocket.close() ; 6. Close server-side socket: serverSocket.close() ;
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]
Datagram, sender side 1. Client socket creation: // sender socket doesn’t need // a special port number DatagramSocket clientSocket = new DatagramSocket () ;
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) ;
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() ) ;
Datagram, sender side 4. Close the client socket: clientSocket.close() ;
Datagram, receiver side 1. Create a server socket: // listens on port 4545 DatagramSocket serverSocket = new DatagramSocket (4545) ;
Datagram, receiver side 2. Receive a datagram: DatagramPacket question = new DatagramPacket (new byte[512], 512) ; serverSocket.receive (question) ;
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) ;
Datagram, receiver side 4. Close the server socket: serverSocket.close() ;