240 likes | 250 Views
Learn about Datagram sockets, data exchange, DatagramSocket API, key methods, constructors, and program flow in client-server programming with examples in Java this week.
E N D
Block 10: Datagram Jin Sa Client-server Programming
Datagram and examples this week • Next week, move multicast from next term. • Last week of the term: no CSP lecture on 20 Dec, but available in 2P18 • Assignment JOptionPane not considered GUI implementation for this assignment Client-server Programming
Datagram sockets and stream sockets • A socket programming construct can use either the UDP or TCP protocol. • Sockets that use UDP for transport are known as datagram sockets, while sockets that use TCP are termed stream sockets. Client-server Programming
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
The Java Datagram Socket API In Java, two classes are provided for the datagram socket API: • the DatagramSocketclass for the sockets. • the DatagramPacket class for the datagram exchanged. Client-server Programming
The Data Structures in the sender and receiver programs for datagram socket Client-server Programming
The Java Datagram Socket API To send a datagram to another process, a 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
The Java Datagram Socket API • In the receiving process, a DatagramSocket object must also be instantiated and bound to a local port. (The port number in the datagram packet of the sender must agree with this port number. ) • 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
Key Methods and Constructors for Datagram -- DatagramSocket • DatagramSocket() constructor, used if no need to receive datagram • DatagramSocket(int port) constructor, used if need to receive packet. The port number can then be specified in the datagram packet sent by the other process. • void close() to close the datagram socket Client-server Programming
Key Methods and Constructors for Datagram-- DatagramSocket • void send(DatagramPacket p) to send the datagram packet pointed by p using this datagram socket • void receive(DatagramPacket p) to receive a datagram packet, p will refer to the packet • void setSoTimeout(int timeout) Sets a timeout period for the blocking receive operations Client-server Programming
Key Methods and Constructors for Datagram -- DatagramPacket • DatagramPacket(byte[] buf,int l) constructor for receiving packets of length l, data received will be stored in buf • DatagramPacket(byte[] buf,int l,InetAddress ipAddress, int port) constructor for sending packets to the socket bound to the port number on the receiving host. Client-server Programming
The 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
Setting timeout • To avoid indefinite blocking, a timeout can be set with a socket object. • Once set, the timeout will be in effect for all blocking operations. Client-server Programming
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
Sender – version 1 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); mySocket.close( ); Client-server Programming
Receiver – version 1 final int MAX_LEN = 10; DatagramSocket mySocket = new DatagramSocket(port); byte[ ] buffer = new byte[MAX_LEN]; DatagramPacket datagram = new DatagramPacket(buffer, MAX_LEN); mySocket.setSoTimeout(5000); mySocket.receive(datagram); String message = new String(buffer); System.out.println(message); mySocket.close( ); Client-server Programming
In class exercises • 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 (see slide 18) • Explain what happens when you run Sender first and then Receiver (see slide 19) • Explain what happens when you run Receiver twice without running Sender. • Explain the effect of setting a timeout. • Explain what happens when you replace the message “hello!” with “hello, how are you?” Client-server Programming
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, e.g. send before receive or receive before send • If infinitely blocked, use timeout, length of the timeout period • Size of the buffer for holding the data Client-server Programming
More datagram APIs DatagramPacket datagram = new DatagramPacket(buffer, MAX_LEN) datagram.getAddress(); //returns address of datagram packet sender datagram.getPort(); //returns port of sender datagram.getLength(); //returns length of packet Client-server Programming
Example • 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 Client-server Programming
Sender – 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
Receiver – 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