290 likes | 663 Views
User Datagram Protocol (UDP). Chapter 5. Overview.
E N D
User Datagram Protocol (UDP) Chapter 5 Prepared By E. Musa Alyaman
Overview The User Datagram Protocol (UDP) is a protocol for sending data over IP that is very quick, but not reliable. That is, when you send UDP data, you have no way of knowing whether it arrived, much less whether different pieces of data arrived in the order in which you sent them. However, the pieces that do arrive generally arrive quickly. Prepared By E. Musa Alyaman
UDP • Unreliable Datagram Protocol • Packet Oriented, not stream oriented like TCP/IP • Much faster but no error correction • Must fit data into packets of about 8K or less Prepared By E. Musa Alyaman
Advantages of UDP • UDP communication can be more efficient than guaranteed-delivery data streams. • Unlike TCP streams, which establish a connection, UDP causes fewer overheads. • Real-time applications that demand up-to-the-second or better performance may be candidates for UDP, as there are fewer delays due to the error checking and flow control of TCP. • UDP sockets can receive data from more than one host machine. Prepared By E. Musa Alyaman
The UDP Classes • Java's support for UDP is contained in two classes: java.net.DatagramSocket java.net.DatagramPacket • A datagram socket is used to send and receive datagram packets. Prepared By E. Musa Alyaman
java.net.DatagramPacket • An array of bytes from which data will be sent or into which data will be received. • also contains the address and port to which the packet will be sent. Prepared By E. Musa Alyaman
java.net.DatagramSocket • A DatagramSocket object is a local connection to a port that does the sending and receiving. • There is no distinction between a UDP socket and a UDP server socket. • Also unlike TCP sockets, a DatagramSocket can send to multiple, different addresses. • The address to which data goes is stored in the packet, not in the socket. Prepared By E. Musa Alyaman
UDP ports • Separate from TCP ports. • Each computer has 65,536 UDP ports as well as its 65,536 TCP ports. • A server socket can be bound to TCP port 20 at the same time as a datagram socket is bound to UDP port 20. Prepared By E. Musa Alyaman
Two DatagramPacket Constructors • Constructors for receiving datagram public DatagramPacket(byte[] buffer, int length) public DatagramPacket(byte[] buffer, int offset, int length) byte[ ] buffer = new byte[8192]; DatagramPacket dp = new DatagramPacket(buffer, buffer.length); • Constructors for sending datagram public DatagramPacket(byte[] data, int length, InetAddress remote, int port) public DatagramPacket(byte[] data, int offset, int length, InetAddress remote, int port) Prepared By E. Musa Alyaman
Example: String s = “This is a test"; byte[] data = s.getBytes(“ASCII”); try { InetAddress ia = InetAddress.getByName(“www.ju.edu.jo"); int port = 7; DatagramPacket dp = new DatagramPacket(data, data.length, ia, port); } catch (UnknownHostException e) { System.err.println(e); } Prepared By E. Musa Alyaman
DatagramPackets Get Methods • The get Methods: • public InetAddress getAddress() • public int getPort() • public byte[] getData() public String(byte[] buffer, String encoding) String s = new String(dp.getData( ), "ASCII"); public ByteArrayInputStream(byte[] buffer, int offset, int length) InputStream in = new ByteArrayInputStream(packet.getData( ), packet.getOffset( ), packet.getLength( )); DataInputStream din = new DataInputStream(in); • public int getLength() • public int getOffset() Prepared By E. Musa Alyaman
DatagramPackets Set Methods • The set Methods: There are used to change the data, remote address and remote port after the datagram has been created • public void setData(byte[] data) • public void setData(byte[] data, int offset, int length) • public void setAddress(InetAddress remote) • public void setPort(int port) • public void setLength(int length) Prepared By E. Musa Alyaman
Example: setData() try { InetAddress ia = InetAddress.getByName(“www.ju.edu.jo"); int port = 7; int offset = 0; int bytesSent = 0; DatagramPacket dp = new DatagramPacket(bigarray, offset, 512, ia, port); while (bytesSent < bigarray.length) { socket.send(dp); bytesSent += dp.getLength( ); int bytesToSend = bigarray.length - bytesSent; int size = (bytesToSend > 512) ? 512 : bytesToSend; dp.setData(bigarray, bytesSent, size); } } catch (UnknownHostException e) { System.err.println(e); } Prepared By E. Musa Alyaman
Example: setAddress() String s = "Really Important Message"; byte[] data = s.getBytes("ASCII"); DatagramPacket dp = new DatagramPacket(data, data.length); dp.setPort(2000); String network = "128.238.5."; for (int host = 1; host < 255; host++) { try { InetAddress remote = InetAddress.getByName(network + host); dp.setAddress(remote); socket.send(dp); } catch (IOException ex) { // slip it; continue with the next host } } Prepared By E. Musa Alyaman
java.net.DatagramSocket • public DatagramSocket() throws SocketException • public DatagramSocket(int port) throws SocketException • public DatagramSocket(int port, InetAddress laddr) throws SocketException • The first is for client datagram sockets; that is sockets that send datagrams before receiving any. • The second two are for server datagram sockets since they specify the port and optionally the IP address of the socket Prepared By E. Musa Alyaman
Sending UDP Datagrams • To send data to a particular server • Convert the data into byte array. • Pass this byte array, the length of the data in the array (most of the time this will be the length of the array) and the InetAddress and port to which you wish to send it into the DatagramPacket() constructor. • Next create a DatagramSocket and pass the packet to its send() method • public void send (DatagramPacket dp) throws IOException Prepared By E. Musa Alyaman
For example InetAddress ia = InetAddress.getByName(“localhost"); int Port = 19; String s = "My second UDP Packet"; byte[] b = s.getBytes(); DatagramPacket dp = new DatagramPacket(b, b.length, ia, Port); DatagramSocket sender = new DatagramSocket(); sender.send(dp); Prepared By E. Musa Alyaman
Receiving UDP Datagrams • Construct a DatagramSocket object on the port on which you want to listen. • Pass an empty DatagramPacket object to the DatagramSocket's receive() method. • public void receive(DatagramPacket dp) throws IOException Prepared By E. Musa Alyaman
dp is filled with the data from that datagram. • Use getPort() and and getAddress() to tell where the packet came from, getData() to retrieve the data, and getLength() to see how many bytes were in the data. • If the received packet was too long for the buffer, it's truncated to the length of the buffer. • Length is reset when packet is received Prepared By E. Musa Alyaman
public int getLocalPort(): a DatagramSocket method returns an int that represents the local port on which the socket is listening. • Public InetAddress getLocalAddress():a DatagramSocket method returns an InetAddress object that represents the local address to which the socket is bound. Prepared By E. Musa Alyaman
Managing Connections • public void connect (InetAddress host, int port): It does not establish a connection in the TCP sense. It does specify that the DatagramSocket will send packets to and receive packets from only the specified remote host on the specified remote port. Prepared By E. Musa Alyaman
For example, try { byte buffer = new byte[65536]; DatagramPacket incoming = new DatagramPacket(buffer, buffer.length); DatagramSocket ds = new DatagramSocket(2134); ds.receive(incoming); byte[] data = incoming.getData(); String s = new String(data, 0, data.getLength()); System.out.println("Port " + incoming.getPort() + " on " + incoming.getAddress() + " sent this message:"); System.out.println(s); }catch (IOException e) { System.err.println(e);} Prepared By E. Musa Alyaman
Managing Connections • public void disconnect() breaks the connection of a connected DatagramSocket so that it can once again send packets to and receive packets from any host and port. Prepared By E. Musa Alyaman
Socket Options • SO_TIMEOUT: is the amount of time that receive() waits for an incoming datagram before throwing an InterruptedIOException public synchronized void setSoTimeout(int timeout) throws SocketException public synchornized int getSoTimeout() throws IOException Prepared By E. Musa Alyaman
Socket Options • SO_RCVBUF • SO_SNDBUF Prepared By E. Musa Alyaman
Disadvantages of UDP • Lack of Guaranteed Delivery • Lack of Guaranteed Packet Sequencing • Lack of Flow Control Prepared By E. Musa Alyaman
Chapter Highlights In this chapter, you have learned: • How to bind to a local port using DatagramSocket • How to create a DatagramPacket • How to read from, and write to, a DatagramPacket using ByteArrayInputStream and ByteArrayOutputStream • How to listen for UDP packets • How to send UDP packets • How to create a UDP server and a UDP client Prepared By E. Musa Alyaman