UDP Datagrams and Sockets Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.

Slides:



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

Sockets For Servers 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.
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.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
1 Java Networking – Part I CS , Spring 2008/9.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
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.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
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.
Process-to-Process Delivery:
Babak Esfandiari (based on slides by Qusay Mahmoud)
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Multicast Sockets What is a multicast socket?
University of Calgary – CPSC 441.  UDP stands for User Datagram Protocol.  A protocol for the Transport Layer in the protocol Stack.  Alternative to.
TCP/IP: Basics1 User Datagram Protocol (UDP) Another protocol at transport layer is UDP. It is Connectionless protocol i.e. no need to establish & terminate.
 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.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 26.
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.
1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net.
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.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
Networking Java (2/3)
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
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.
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.
Client/Server Design Issues Based on Java Network Programming and Distributed Computing, Chapter 6 Also Online Java Tutorial, Sun.
Process-to-Process Delivery:
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.
Socket Programming Ameera Almasoud
Object-Orientated Analysis, Design and Programming
Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
NETWORK PROGRAMMING CNET 441
Network Programming Introduction
Network Programming Introduction
Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Lecture 11 Socket Programming.
Process-to-Process Delivery:
NETWORK PROGRAMMING CNET 441
Process-to-Process Delivery: UDP, TCP
Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Presentation transcript:

UDP Datagrams and Sockets Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Contents TCP vs. UDP UDP protocol Examples

TCP vs. UDP TCP is designed for reliable transmission of data If data is lost or damaged in transmission, TCP ensures that the data is resent If data arrive out of order, TCP puts them back in the correct order If the data is coming too fast for the connection, TCP throttles the speed back so that packets won’t lost Thus a program never needs to worry about receiving data that is out of order or incorrect

TCP vs. UDP TCP’s reliability comes at a price – speed Establishing and tearing down TCP connections can take a fair amount of time For short transmission (such as HTTP) TCP may be inefficient UDP (User Datagram Protocol) is an alternative protocol for sending data over IP UDP: very quick but not reliable

TCP vs. UDP When you send UDP data, you have no way of knowing –whether it arrived –Whether different pieces of data arrived in the order in which they are sent

UDP Protocol The obvious question to ask is why anyone would ever use an unreliable protocol Check some applications –FTP: require reliable transmission –Real-time audio or video stream: UDP –Domain Name system (DNS) (UDP or TCP)

UDP Protocol Reliable UDP protocol –If a client sends a short UDP request to a server, it may assume that the packet is lost if no response is returned –Applications are responsible for reliable transmission –Examples: NFS: Network File System Trivial FTP FSP (an FTP)

UDP Protocol In Java, DatagramPacket and DatagramSocket classes support UDP protocol. DatagramPacket: Data to be sent DatagramSocket: packet sender/receiver Sending data: –Data are put in a DatagramPacket –Send the packet by using DatagramSocket Receiving data: –Receive a DatagramPacket object from a DatagramSocket –Read data of the packet

UDP Protocol UDP uses the same kind of socket to send and receive data (I.e. DatagramSocket) –Note that in TCP you have Socket and ServerSocket Data to be sent are in UDP packets. Packets’ order is not preserved. –TCP sockets allow you to treat network connection as a stream. A single DatagramSocket can send data to and receive data from many independent hosts –UDP does not have any concept of a connection between hosts –Multi-casting is possible

DatagramPacket UDP datagrams add very little to an IP datagrams See Fig 13-1 –UDP header adds only eight bytes to the IP header –Source and destination port numbers –Length –Optional checksum –Each has two bytes

DatagramPacket Port numbers are unsigned 2-bytes integers –65536 possible UDP port per host The number of bytes in a datagram is limited to –IP header requires 20 bytes + options (0~40 bytes) (see page 27 of IP datagram format) –Thus theoretical maximum amount –On many platform the actual limit is more likely to be 8K bytes –Very risky: sending UDP packets more than 8KB –For maximum safety UPD packet size <= 512B

Choosing a Datagram Size If the network is highly unreliable (e.g. Packet radio network) small packets are perferrable –Small packets are less likely to be corrupted in transit If the network is highly reliable (e.g. Local Area Network) largest packets are perferrable 8K bytes is a good compromise for many types of networks

DatagramPacket constructors DatagramPacket(byte[] buf, int length) –Constructs a DatagramPacket for receiving packets of length length. –buf - buffer for holding the incoming datagram. –length - the number of bytes to read (I.e. buf.length). DatagramPacket(byte[] buf, int offset, int length) –Constructs a DatagramPacket for receiving packets of length length, specifying an offset into the buffer. –offset - the offset for the buffer

DatagramPacket constructors DatagramPacket(byte[] buf, int length, InetAddress address, int port) –Constructs a datagram packet for sending packets of length length to the specified port number on the specified host. –buf - the packet data. –length - the packet length. –address - the destination address. –port - the destination port number. DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port) –offset - the packet data offset.

DatagramPacket Sending a packet Example: sending a packet (page 417) String s = "This is a test."; // data to be sent byte[] data = s.getBytes(); // convert to byte array try { InetAddress ia = InetAddress.getByName("metalab.unc.edu"); int port = 7; DatagramPacket dp = new DatagramPacket(data, data.length, ia, port); // send the packet … } catch (UnknownHostException e) { System.err.println(e); }

DatagramPacket Get methods InetAddress getAddress() –Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received byte[] getData() –Returns the data received or the data to be sent. int getLength() –Returns the length of the data to be sent or the length of the data received.

DatagramPacket Get methods int getOffset() –Returns the offset of the data to be sent or the offset of the data received. int getPort() –Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received. Get methods example: Page 420 –DatagramExample.java

DatagramPacket Set methods void setAddress(InetAddress iaddr) –Sets the IP address of the machine to which this datagram is being sent. void setData(byte[] buf) –Set the data buffer for this packet. void setData(byte[] buf, int offset, int length) –Set the data buffer for this packet. void setLength(int length) –Set the length for this packet. void setPort(int iport) –Sets the port number on the remote host to which this datagram is being sent.

DatagramSocket To send or receive a DatagramPacket, one needs to open a datagram socket (i.e. DatagramSocket). All datagram sockets are bound to a local port –Datagram sockets listen for incoming data on that port –Or send data to that port

DatagramSocket For datagram servers, clients need to know on which port a server is listening for incoming datagrams –Servers must specify the local port on which it will listen. For datagram clients, any unused port can be used to send requests (a datagram packet) to server There is no distinction between client sockets and server sockets. –One class only – DatagramSocket class

DatagramSocket constructor DatagramSocket() –Constructs a datagram socket and binds it to any available port on the local host machine. –Good for clients (may be not good for UDP Servers) DatagramSocket(int port) –Constructs a datagram socket and binds it to the specified port on the local host machine. DatagramSocket(int port, InetAddress laddr) –Creates a datagram socket, bound to the specified local address.

UDPPortScanner UDPPortScanner.java on page 424 scans and reports all the used UDP ports

DatagramSocket Send and Receive Datagrams void send(DatagramPacket p) –Sends a datagram packet from this socket. DatagramSocket ds = new DatagramSocket(); DatagramPacket dp = new DatagramPacket(data, data.length, remote, port); ds.send(dp); void receive(DatagramPacket p) –Receives a datagram packet from this socket. DatagramSocket server = new DatagramSocket(port); DatagramPacket packet = new DatagramPacket(buffer, buffer.length); server.receive(packet);

DatagramSocket Send and Receive Datagrams Example 13-3 UDP Discard Client (UDPDiscardClient.java) –Read lines of user input –Send them to a discard server Example 13-4 UDP Discard Server (UDPDiscardServer.java) –Receive a single UDP datagram –Print out the info on the datagram

DatagramSocket close method void close() –Closes this datagram socket. –Free the port occupied by that socket try { DatagramSocket socket = new DatagramSocket(); socket.close(); } catch (SocketException e) { System.err.println(e); }

DatagramSocket Managing connections Datagram sockets can talk to anyone by default You may restrict the connections by using connect method. Possible Applications: –Applets are allowed to send datagrams to and receive datagrams from the applet host. –A NFS (network file system) client should accept packets only from the server it is talking to. –A networked game should listen to datagrams only from the people playing the game

DatagramSocket Managing connections void connect(InetAddress address, int port) –Connects the socket to a remote address for this socket. –When a socket is connected to a remote address, packets may only be sent to or received from that address. –Attempt to send packets to a different host or port will throw IllegalArgumentException. –Packets received from a different host or port will be discarded without an exception or other notification –By default a datagram socket is not connected. –When a socket is connected, receive and send will not perform any security checks on incoming and outgoing packets, other than matching the packet's and the socket's address and port.

DatagramSocket Managing connections void disconnect() –Disconnects the socket. –This does nothing if the socket is not connected. –The disconnected DatagramSocket can once again send packets to and receive packets from any host and port.

DatagramSocket Get methods InetAddress getInetAddress() –Returns the address to which this socket is connected. InetAddress getLocalAddress() –Gets the local address to which the socket is bound. int getLocalPort() –Returns the port number on the local host to which this socket is bound. int getPort() –Returns the port for this socket.

DatagramSocket Options int getReceiveBufferSize() –Get value of the SO_RCVBUF option for this DatagramSocket, that is the buffer size used by the platform for input on this DatagramSocket. –Larger buffers tend to improve performance (i.e. LAN) void setReceiveBufferSize(int size) –Sets the SO_RCVBUF option to the specified value for this DatagramSocket.

DatagramSocket Options int getSendBufferSize() –Get value of the SO_SNDBUF option for this DatagramSocket, that is the buffer size used by the platform for output on this DatagramSocket. Void setSendBufferSize(int size) –Sets the SO_SNDBUF option to the specified value for this DatagramSocket.

DatagramSocket Options int getSoTimeout() –Retrive setting for SO_TIMEOUT. –SO_TIMEOUT in milliseconds. –With this option set to a non-zero timeout, a call to receive() for this DatagramSocket will block for only this amount of time. –A timeout of zero is interpreted as an infinite timeout. –If the timeout expires, a java.io.InterruptedIOException is raised, though the ServerSocket is still valid. void setSoTimeout(int timeout) –Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.

DatagramSocket Options Examples of setSoTimeout and getSoTimeout methods on page 433

Useful UDP Applications When an IP packet is received by a host, the host determines whether the packet is a TCP or UDP datagram by inspecting IP header TCP and UDP ports are independent –TCP and UDP servers can share the same port number without problems. By convention, if a service has both TCP and UDP implementations, it uses the same port for both

Useful UDP Applications Simple UDP clients Several Internet services need to know the client’s address and port; they discard any data the client sends in its datagrams: –Daytime (TCP and UDP port: 13) –Quote of the day –Time (TCP and UDP port: 37) –Chargen (TCP and UDP port: 19) Clients for these protocols simply send a UDP datagram to the servers and read the response that comes back

Useful UDP Applications Simple UDP clients Example 13-5 UDPPoke.java Run the UDDPoke –java UDPPoke vision.poly.edu 19 !"#$%&'()*+,-./ :; RSTUVWXYZ[\]^_`abcde?? –java UDPPoke vision.poly.edu 13 Mon Apr 16 22:11:

Useful UDP Applications UDP Server Example 13-7 UDPServer.java (page 440) UDPServer is designed for reuse UDPServer is a thread Fields –int bufferSize: buffer size of DatagramPacket –DatagramSocket ds: the datagram socket Methods: –respond(packet): abstract method –run(): read packet and call respond()

Useful UDP Applications UDP Server Example 13-8 FastUDPDiscardServer.java (page 441) public void respond(DatagramPacket packet) {} Example 13-9 LoggingUDPDiscardServer.java (page 442) public void respond(DatagramPacket packet) { byte[] data = new byte[packet.getLength()]; System.arraycopy(packet.getData(),0,data,0, packet.getLength()); try { String s = new String(data, "ASCII"); System.out.println(packet.getAddress() + " at port " + packet.getPort() + " says " + s); } catch (java.io.UnsupportedEncodingException e) { } }

Useful UDP Applications UDP Server Example UDPEchoServer.java (page 443) public void respond(DatagramPacket incoming) { try { DatagramPacket outgoing = new DatagramPacket( incoming.getData(), incoming.getLength(), incoming.getAddress(), incoming.getPort()); ds.send(outgoing); System.out.println(incoming.getLength()); } catch (IOException e) { System.err.println(e); }

Useful UDP Applications UDP Server Example UDPDaytimeServer.java (page 444) public void respond(DatagramPacket packet) { try { Date now = new Date(); String response = now.toString() + "\r\n"; byte[] data = response.getBytes("ASCII"); DatagramPacket outgoing = new DatagramPacket(data, data.length, packet.getAddress(), packet.getPort()); ds.send(outgoing); } catch (IOException e) { System.err.println(e); } }

Useful UDP Applications UDP Echo Client Example UDPEchoClient.java (page 445) It has two threads –Senderhread: process the user input and send it to the echo server –ReceiverThread: accept input from the server and display it to the user