1 / 8

IP provides a datagram (connectionless) transport service across the network.

UDP – User Datagram Protocol. IP provides a datagram (connectionless) transport service across the network.

Download Presentation

IP provides a datagram (connectionless) transport service across the network.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UDP – User Datagram Protocol • IP provides a datagram (connectionless) transport service across the network. • UDP provides applications with an end-to-end datagram (connectionless) service, packets send individually with full destination address, may not arrive, may arrive out of order, e.g. used by ping, finger, etc. • TCP provides applications with a a virtual circuit (connection-oriented) communication service across the network • Used by most applications (error free) HTTP, FTP, POP, SMTP, etc.

  2. UDP – User Datagram Protocol UDP provides applications with an end-to-end datagram (connectionless) service, packets are sent individually with full destination address, may not arrive, may arrive out of order, e.g. used by ping, finger, etc i.e. connectionless transmission is like posting letter • You post a letter it may get there it may not – you don’t know • If you break a bit package into a number of letters there may not arrive in the same order as you post them

  3. A server using UDP (server. java) A client uses a datagram to send a text message to the server which echos it back using a datagram – both display in a JTextArea In the server method main() (at end of file) • Creates a new Server object to create the GUI and a datagram socket Server app = new Server(); • Calls the waitForPackets method to wait for a client datagram app.waitForPackets(); Server’s constructor creates the GUI (extends JFrame) and a datagram socket (using port 5000) for communication with the client • JTextArea display for text output • socket = new DatagramSocket( 5000 );

  4. Method waitForPackets creates a DatagramPacket, waits for the client to send a datagram and displays it Step 1: create a DatagramPacket to hold received datagram receivePacket = new DatagramPacket( data, data.length ); Step 2: wait for datagram packet socket.receive( receivePacket ); Step 3: display the packet contents in the JTextField display.append( "\nPacket received:" + "\nFrom host: " + receivePacket.getAddress() + "\nHost port: " + receivePacket.getPort() + "\nLength: " + receivePacket.getLength() + "\nContaining:\n\t" + new String( receivePacket.getData(), 0, receivePacket.getLength() ) ); Datagram packets carry arrays of byte[] – the last two lines, calling receivePacket.getData(), convert this to a String

  5. Step 4: creates a new DataPacket from received datagram sendPacket = new DatagramPacket( receivePacket.getData(), receivePacket.getLength(), receivePacket.getAddress(), receivePacket.getPort() ); Step 5: send the datagram to the client socket.send( sendPacket ); Then waits for the next datagram

  6. The client (client. java) waits for the user to enter text in a JTextFeild and sends it in a datagram to the server In the server method main() (at end of file) • Creates a new Client object to create the GUI and a datagram socket Client app = new Client(); 2. Calls the waitForPackets method to wait for a packet from server app.waitForPackets(); Client’s constructor creates the GUI (extends JFrame) and a DatagramSocket to receive messages from the server • JTextField enter for text input to send to server • JTextArea display for text output • Create DatagramSocket bound to any available port socket = new DatagramSocket( );

  7. Method waitForPackets creates a DatagramPacket, waits for the client to send a datagram and displays it Step 1: create a DatagramPacket to hold received datagram receivePacket = new DatagramPacket( data, data.length ); Step 2: wait for datagram packet socket.receive( receivePacket ); Step 3: display the packet contents in the JTextField display.append( "\nPacket received:" + "\nFrom host: " + receivePacket.getAddress() + "\nHost port: " + receivePacket.getPort() + "\nLength: " + receivePacket.getLength() + "\nContaining:\n\t" + new String( receivePacket.getData(), 0, receivePacket.getLength() ) ); Then waits for the next datagram

  8. When text is entered in the JTextField method actionPerformed is called Step 4: convert the String s entered into a array of byte[] byte data[] = s.getBytes(); Step 2: create a DatagramPacket to send to the server (on localhost) port 5000 (the client MUST know the port the server is listening on) sendPacket = new DatagramPacket( data, data.length, InetAddress.getLocalHost(), 5000 ); Step 3: send the DatagramPacket socket.send( sendPacket );

More Related