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

Slides:



Advertisements
Similar presentations
Socket UDP H. Fauconnier 1-1 M2-Internet Java. UDP H. Fauconnier M2-Internet Java 2.
Advertisements

Umut Girit  One of the core members of the Internet Protocol Suite, the set of network protocols used for the Internet. With UDP, computer.
Intermediate TCP/IP TCP Operation.
UDP Datagrams and Sockets Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
© 2003 Andrea Calvagna 5/16/2015 Java Sockets and Server Sockets Low Level Network Programming Andrea Calvagna
© 1999 Elliotte Rusty Harold 5/16/2015 Java Sockets and Server Sockets Low Level Network Programming Elliotte Rusty Harold
Jan Java Networking UDP Yangjun Chen Dept. Business Computing University of Winnipeg.
Prepared By E. Musa Alyaman1 Java Network Programming TCP.
Prepared By E. Musa Alyaman1 URLs, InetAddresses, and URLConnections Chapter 9.
Socket Programming.
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Networking Support In Java 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Programming Section 9 James King 12 August 2003.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L24 (Chapter 25) Networking.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
Networking Support In Java 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Datagram Programming A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed.
Gursharan Singh Tatla Transport Layer 16-May
Process-to-Process Delivery:
Babak Esfandiari (based on slides by Qusay Mahmoud)
Socket Programming (C/Java)
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Multicast Sockets What is a multicast socket?
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
VIII. UDP Datagrams and Sockets. The User Datagram Protocol (UDP) is an alternative protocol for sending data over IP that is very quick, but not reliable:
CS 424/524: Introduction to Java Programming Lecture 25 Spring 2002 Department of Computer Science University of Alabama Joel Jones.
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Chapter 2 Applications and Layered Architectures Sockets.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
Java Sockets Programming
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
What is a Network? Computer network Computer network a set of computers using common protocols to communicate over connecting transmission media. a set.
Socket Programming Using JAVA Asma Shakil Semester 1, 2008/2009.
Sockets For Clients Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
Networking Java (2/3)
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
1 Netprog 2002 TCP/IP UDP/IP in Java Based on Java Network Programming and Distributed Computing.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
UDP User Datagram Protocol. About the UDP A commonly used transport protocol Does not guarantee either packet delivery or order The packets may travel.
1 Socket-based Client- Server Application Client-Server application architecture Choosing services – Connectionless atau Connection-oriented.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
UDP Programming. Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007 2/86 Overview.
Network Programming. These days almost all devices.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Network Programming Communication between processes Many approaches:
Java.net CS-328 Dick Steflik.
Object-Orientated Analysis, Design and Programming
Secure Sockets SSL (Secure Sockets Layer) is a standard security technology for establishing an encrypted link between a server and a client—typically.
NETWORK PROGRAMMING CNET 441
Network Programming Introduction
Network Programming Introduction
UNIT-6.
Process-to-Process Delivery:
Networking.
CSCD 330 Network Programming
CSCD 330 Network Programming
NETWORK PROGRAMMING CNET 441
Process-to-Process Delivery: UDP, TCP
Transport Layer 9/22/2019.
Based on Java Network Programming and Distributed Computing
Presentation transcript:

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

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.

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

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.

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

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)

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); }

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()

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)

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); }

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 = " "; 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 Alyaman15 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 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

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);

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

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

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.

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.

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);}

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.

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

Prepared By E. Musa Alyaman25 Socket Options SO_RCVBUF SO_SNDBUF

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

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