1 / 44

Block 13: Datagram

Block 13: Datagram. Jin Sa. Datagram sockets and stream sockets. A socket programming construct can use either the UDP or TCP protocol. stream sockets for TCP and datagram sockets for UDP. Datagram socket. The datagram socket API supports the exchange of discrete units of data

allman
Download Presentation

Block 13: Datagram

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. Block 13: Datagram Jin Sa Client-server Programming

  2. Datagram sockets and stream sockets • A socket programming construct can use either the UDP or TCP protocol. • stream sockets for TCP and datagram sockets for UDP. Client-server Programming

  3. Datagram socket • The datagram socket API supports the exchange of discrete units of data • Datagram sockets usually are used to support connectionless communication • No need to establish connection • But each time we need to include the receiver’s address Client-server Programming

  4. The Java Datagram Socket API • the DatagramSocketclass for the sockets. • Sending and receiving • the DatagramPacket class for the datagram exchanged. • Receiver’s address • Data Client-server Programming

  5. Key Methods and Constructors for DatagramSocket • DatagramSocket() • used if no need to receive datagram • DatagramSocket(int port) • used if need to receive packet. The port number can then be specified in the datagram packet sent by the other process. Client-server Programming

  6. Key Methods and Constructors for DatagramSocket • void send(DatagramPacket p) • to send the datagram packet p • void receive(DatagramPacket p) • object referred to by p will hold the packet • void setSoTimeout(int timeout) • Sets a timeout period for the blocking receive operations Client-server Programming

  7. Key Methods and Constructors for DatagramPacket • DatagramPacket(byte[]buf, int l) • used for received packet • packet length upto l; data will be stored in the array buf • DatagramPacket(byte[]buf,int l, InetAddress rcvAddr, int port) • used for packet to be sent; • buf contains the data, rcvAddr and port identifies the receiver. Client-server Programming

  8. Relationships between DatagramSocket and DatagramPacket Client-server Programming

  9. How to send a datagram using Java The sender process: • creates a DatagramPacket object that represents the datagram itself. This object carries • the payload data as a reference to a byte array, • the destination address (the host ID and port number to which the receiver’s socket is bound). • Creates a DatagramSocket, and issues a call to the send method in the DatagramSocketobject, specifying a reference to the DatagramPacket object as an argument Client-server Programming

  10. How to receive a datagram using Java • In the receiving process, a DatagramSocket object must also be instantiated and bound to a local port. • To receive datagrams sent to the socket, the process creates a datagramPacket object which references a byte array • Calls the receivemethod in the DatagramSocket object, specifying as argument a reference to the DatagramPacket object. Client-server Programming

  11. Typical program flow in the sender and receiver programs Receiver program Sender program Create a datagram socket Place data in a byte array Create a datagram packet, specifying the data array and the receiver’s address (IP address and port same as in the receiver’s port number) Invoke the send method of the socket with a reference to the datagram packet Create a datagram socket and bind it to a local port number Create a byte array for receiving the data Create a datagram packet, specifying the data array Invoke the receive method of the socket with a reference to the datagram packet Extract the data from the byte array Client-server Programming

  12. Example • Sender – version 1 • Create a datagram socket • Put the message “Hello!” in a byte array • Create a datagram packet with the receiver’s address and port number, and the byte array • Send datagram in the packet through the datagram socket • Receiver – version 1 • Create a datagram socket bound to a port (e.g.12345) • Create a datagram packet includes a reference to a byte array • Receive a data packet through the datagram socket • On E:clientserverprogramming0708 Client-server Programming

  13. Sender import java.net.*; import java.io.*; public class Sender { public static void main(String[] args) { try { // set receiver’s address – see slide Sender (1) // create byte array for the message and // create datagram packet – see slide Sender (2) // create datagram socket – see slide Sender (3) // send packet – see slide Sender (4) // close datagram socket – see slide Sender (5) } // end try catch (Exception ex) { ex.printStackTrace( ); } } // end main } // end class Client-server Programming

  14. Sender (1): set receiver’s address import java.net.*; import java.io.*; public class Sender { public static void main(String[] args) { try { //set receiver’s address InetAddress receiverHost = InetAddress.getByName(“rcverHostname"); int receiverPort = 12345; // create byte array for the message and // create datagram packet – see slide Sender (2) // create datagram socket – see slide Sender (3) // send packet – see slide Sender (4) // close datagram socket – see slide Sender (5) } // end try catch (Exception ex) { … } } // end main } // end class Client-server Programming

  15. Sender (2): create datagram packet import java.net.*; import java.io.*; public class Sender { public static void main(String[] args) { try { //get receiver’s address – see slide Sender (1) // create byte array for the message and // create datagram packet String message = "hello"; byte[ ] buffer = message.getBytes( ); DatagramPacket datagramP = new DatagramPacket(buffer, buffer.length, receiverHost, receiverPort); //Create datagram socket – see slide Sender (3) // send packet – see slide Sender (4) // close datagram socket – see slide Sender (5) } // end try catch (Exception ex) { … } } // end main } // end class Client-server Programming

  16. Sender (3): create a datagram socket import java.net.*; import java.io.*; public class Sender { public static void main(String[] args) { try { // get receiver’s address – see slide Sender (1) // create byte array for the message and // create datagram packet – see slide Sender (2) // create datagram socket DatagramSocket mySocket = new DatagramSocket(); // send packet – see slide Sender (4) // close datagram socket – see slide Sender (5) } // end try catch (Exception ex) { ex.printStackTrace( ); } } // end main } // end class Client-server Programming

  17. Sender (4): Send a packet import java.net.*; import java.io.*; public class Sender { public static void main(String[] args) { try { //get receiver’s address – see slide Sender (1) // create byte array for the message and // create datagram packet – see slide Sender (2) // create datagram socket – see slide Sender (3) // send packet mySocket.send(datagramP); // close datagram socket – see slide Sender (5) } // end try catch (Exception ex) { ex.printStackTrace( ); } } // end main } // end class Client-server Programming

  18. Sender(5): close the datagram socket import java.net.*; import java.io.*; public class Sender { public static void main(String[] args) { try { //get receiver’s address – see slide Sender (1) // create byte array for the message and // create datagram packet – see slide Sender (2) // create datagram socket – see slide Sender (3) // send packet – see slide Sender (4) // close datagram socket mySocket.close( ); } // end try catch (Exception ex) { ex.printStackTrace( ); } } // end main } // end class Client-server Programming

  19. Receiver import java.net.*; import java.io.*; public class Receiver { public static void main(String[] args) { try { // create datagram packet –see slide Receiver (1) // create a datagram socket for receiving // the data – see slide Receiver (2) //receive and display message // – see slide Receiver (3) //close socket – see slide Receiver (4) } // end try catch (Exception ex) { ex.printStackTrace( ); } } // end main } // end class Client-server Programming

  20. Receiver (1): create datagram packet import java.net.*; import java.io.*; public class Receiver { public static void main(String[] args) { try { // create datagram packet final int MAX_LEN = 10; byte[ ] buffer = new byte[MAX_LEN]; DatagramPacket datagramP = new DatagramPacket(buffer, MAX_LEN); // create a datagram socket for receiving // the data – see slide Receiver (2) //receive message – see slide Receiver (3) //close socket – see slide Receiver (4) } // end try catch (Exception ex) { ex.printStackTrace( ); } } // end main } // end class Client-server Programming

  21. Receiver (2): create a datagram socket import java.net.*; import java.io.*; public class Receiver { public static void main(String[] args) { try { //create datagram packet – see slide Receiver (1) // create a datagram socket for receiving // the data – see slide Receiver int port =12345; DatagramSocket mySocket = new DatagramSocket(port); //receive and display message // – see slide Receiver (3) //close socket – see slide Receiver (4) } // end try catch (Exception ex) { ex.printStackTrace( ); } } // end main } // end class Client-server Programming

  22. Receiver(3):receive and display message import java.net.*; import java.io.*; public class Receiver { public static void main(String[] args) { try { // create datagram packet– see slide Receiver (1) // create a datagram socket for receiving // the data – see slide Receiver (2) //receive message – see slide Receiver mySocket.receive(datagramP); System.out.println( new String(datagramP.getData())); //close socket – see slide Receiver (4) } // end try catch (Exception ex) { ex.printStackTrace( ); } } // end main } // end class Client-server Programming

  23. Receiver(4): close socket import java.net.*; import java.io.*; public class Receiver { public static void main(String[] args) { try { // create datagram packet–see slide Receiver (1) // create a datagram socket for receiving // the data – see slide Receiver (2) //receive message – see slide Receiver (3) //close socket – see slide Receiver (4) mySocket.close( ); } // end try catch (Exception ex) { ex.printStackTrace( ); } } // end main } // end class Client-server Programming

  24. Key statements in Sender – version 1 DatagramSocket mySocket = new DatagramSocket(); InetAddress receiverHost = InetAddress.getByName("localhost"); int receiverPort = 12345; String message = "hello"; byte[ ] buffer = message.getBytes( ); DatagramPacket datagramP = new DatagramPacket(buffer, buffer.length, receiverHost, receiverPort); mySocket.send(datagramP); mySocket.close( ); Client-server Programming

  25. Key statement in Receiver – version 1 DatagramSocket mySocket = new DatagramSocket(port); final int MAX_LEN = 10; byte[ ] buffer = new byte[MAX_LEN]; DatagramPacket datagramP = new DatagramPacket(buffer, MAX_LEN); mySocket.receive(datagramP); System.out.println(new String(datagramP.getData())); mySocket.close( ); Client-server Programming

  26. More Datagram Packet APIs • After receiving a datagram packet, • datagram.getAddress(); //returns address of datagram packet sender • datagram.getPort(); //returns port of sender Client-server Programming

  27. In class exercises (may skip) • On E:clientserverprogramming0708/streamstock package • Study the two programs: Sender.java and Receiver.java (send is non blocking, and receive is blocking) • Explain what happens when you run Receiver first and then Sender • Explain what happens when you run Sender first and then Receiver • Explain the effect of setting a timeout. • Explain what happens when you replace the message “hello!” with “hello, how are you?” Client-server Programming

  28. Client-server Programming

  29. Client-server Programming

  30. Issues to remember • Sockets normally provide non-blocking send and blocking receive • The method receive blocks until a datagram packet is received as seen in Receiver, unless it sets a time out period. • Synchronisation of the events • If infinitely blocked, use timeout, length of the timeout period • Size of the buffer for holding the data Client-server Programming

  31. Student activity • Sender – version 2 • Create a datagram socket • Put the message “Hello!” in a byte array • Create a datagram packet with the receiver’s address and port number, and the byte array • Send datagram in the packet through the datagram socket • Receive a reply from the Receiver • Receiver – version 2 • Create a datagram socket bound to a port (12345) • Create a datagram packet includes a reference to a byte array • Receive a datagram packet through the datagram socket • Get the sender’s address and port number from the datagram packet • Send a reply to the Sender using the address and the port number obtained from the datagram packet • On E:clientserverprogramming0708/streamstock package Client-server Programming

  32. import java.net.*; import java.io.*; public class SenderDatagram_1 { public static void main(String[] args) { try { InetAddress receiverHost = InetAddress.getByName("localhost"); int receiverPort = 12345; String message = "hello!"; DatagramSocket mySocket = new DatagramSocket(); byte[] buffer = message.getBytes(); DatagramPacket datagram = new DatagramPacket(buffer, buffer.length, receiverHost, receiverPort); mySocket.send(datagram); Create a datagram socket Create a datagram packet containing “hello” Send the packet using the datagram socket

  33. Receive datagram mySocket.receive(datagram); String ack = new String(datagram.getData()); System.out.println("The receiver's reply is: " + ack); mySocket.close(); } // end try catch (Exception ex) { ex.printStackTrace(); } } // end main } // end class Display message received

  34. import java.net.*; import java.util.Scanner; public class ReceiverDatagram_1 { public static void main(String[] args) { int port =12345; final int MAX_LEN = 10; try { DatagramSocket mySocket = new DatagramSocket(port); byte[ ] buffer = new byte[MAX_LEN]; DatagramPacket datagram = new DatagramPacket(buffer, MAX_LEN); mySocket.receive(datagram); Create a datagram socket for receiving (and sending) Create a datagram packet as a place holder Receive datagram

  35. Display message String message = new String(datagram.getData()); System.out.println("Message received is: “+message ); InetAddress senderAddr = datagram.getAddress(); int senderPort= datagram.getPort(); System.out.println("got sender Addr: “ +dsenderAddr); System.out.println("sender port: “ +senderPort); Get the sender’s address and port number

  36. Enter user’s name Scanner in=new Scanner(System.in); System.out.println("Enter your name to send back to the sender:"); String name=in.nextLine(); buffer=name.getBytes(); DatagramPacket datagram2= new DatagramPacket(buffer, buffer.length, senderAddr,senderPort); mySocket.send(datagram2); mySocket.close( ); } // end try catch (Exception ex) { ex.printStackTrace( ); } } // end main } // end class Create a datagram containing the user’s name Send a reply datagram

  37. import java.net.*; import java.io.*; public class SenderDatagram_1 { public static void main(String[] args) { try { InetAddress receiverHost = InetAddress.getByName("localhost"); int receiverPort = 12345; String message = "hello!"; Create a datagram socket Create a datagram packet containing “hello” Send the packet using the datagram socket

  38. Receive datagram mySocket.close(); } // end try catch (Exception ex) { ex.printStackTrace(); } } // end main } // end class Display message received

  39. import java.net.*; import java.util.Scanner; public class ReceiverDatagram_1 { public static void main(String[] args) { int port =12345; final int MAX_LEN = 10; try { Create a datagram socket for receiving (and sending) Create a datagram packet as a place holder Receive datagram

  40. Display message System.out.println("got sender Addr: “ +dsenderAddr); System.out.println("sender port: “ +senderPort); Get the sender’s address and port number

  41. Enter user’s name Scanner in=new Scanner(System.in); System.out.println("Enter your name to send back to the sender:"); String name=in.nextLine(); mySocket.close( ); } // end try catch (Exception ex) { ex.printStackTrace( ); } } // end main } // end class Create a datagram containing the user’s name Send a reply datagram

  42. Sender key statements – version 2 … DatagramSocket mySocket = new DatagramSocket(); byte[ ] buffer = message.getBytes( ); DatagramPacket datagram = new DatagramPacket(buffer, buffer.length, receiverHost, receiverPort); mySocket.send(datagram); //Now waiting for acknowledgement from receiver."); mySocket.receive(datagram); String ack=new String(buffer); System.out.println("The receiver's reply is; "+ack); mySocket.close( ); Client-server Programming

  43. Receiver key statements – version 2 DatagramPacket datagram = new DatagramPacket(buffer, MAX_LEN); mySocket.receive(datagram); String message = new String(buffer); System.out.println(message); //send a reply back to the sender Scanner in=new Scanner(System.in); System.out.println("Enter your name to send to the sender:"); String name=in.nextLine();//delay so that sender is ready buffer=name.getBytes(); DatagramPacket datagram2= new DatagramPacket(buffer,buffer.length, datagram.getAddress(),datagram.getPort()); mySocket.send(datagram2); mySocket.close( ); Client-server Programming

  44. Practical exercises • Implement the two examples in Java • Version 1: slide 12 • Version 2: slide 31 Client-server Programming

More Related