Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.

Similar presentations

Presentation on theme: "Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5."— Presentation transcript:

1 Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5

2 Prepared By E. Musa Alyaman2 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.

3 Prepared By E. Musa Alyaman3 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

4 Prepared By E. Musa Alyaman4 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.

5 Prepared By E. Musa Alyaman5 The UDP Classes Java's support for UDP is contained in two classes: A datagram socket is used to send and receive datagram packets.

6 Prepared By E. Musa Alyaman6 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.

7 Prepared By E. Musa Alyaman7 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.

8 Prepared By E. Musa Alyaman8 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.

9 Prepared By E. Musa Alyaman9 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)

10 Prepared By E. Musa Alyaman10 Example: String s = “This is a test"; byte[] data = s.getBytes(“ASCII”); try { InetAddress ia = InetAddress.getByName(“"); int port = 7; DatagramPacket dp = new DatagramPacket(data, data.length, ia, port); } catch (UnknownHostException e) { System.err.println(e); }

11 Prepared By E. Musa Alyaman11 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()

12 Prepared By E. Musa Alyaman12 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)

13 Prepared By E. Musa Alyaman13 Example: setData() try { InetAddress ia = InetAddress.getByName(“"); 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); }

14 Prepared By E. Musa Alyaman14 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 } }

15 Prepared By E. Musa Alyaman15  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

16 Prepared By E. Musa Alyaman16 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

17 Prepared By E. Musa Alyaman17 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);

18 Prepared By E. Musa Alyaman18 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

19 Prepared By E. Musa Alyaman19 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

20 Prepared By E. Musa Alyaman20 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.

21 Prepared By E. Musa Alyaman21 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.

22 Prepared By E. Musa Alyaman22 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);}

23 Prepared By E. Musa Alyaman23 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.

24 Prepared By E. Musa Alyaman24 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

25 Prepared By E. Musa Alyaman25 Socket Options SO_RCVBUF SO_SNDBUF

26 Prepared By E. Musa Alyaman26 Disadvantages of UDP Lack of Guaranteed Delivery Lack of Guaranteed Packet Sequencing Lack of Flow Control

27 Prepared By E. Musa Alyaman27 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

Download ppt "Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5."

Similar presentations

Ads by Google