What Is Udp

  • Uploaded by: tismon
  • 0
  • 0
  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View What Is Udp as PDF for free.

More details

  • Words: 471
  • Pages: 4
Q. Explain what is UDP? Write a java program to implement one way communication in a network using UDP protocol? The user datagram protocol (UDP) is called a connectionless, unreliable protocol. It does not add anything to the services of IP except to provide process to process communication instead of host to host communication and also error checking. UDP is a very simple protocol using a minimum of overhead. If a process want to send a small message and does mote care much about reliability, it can use UDP. Sending a small message using UDP takes much less interaction between the sender and the receiver than TCP. User Datagram UDP packets, called user datagrams have a fixed-size header of 8 bytes and the fields are as follows. Source port number: This a port number used by the process running on the source host and having 16 bit long. Destination port number: this is used by the process running on the destination host having 16 bit long. Length : this is a 16 bit field that defines the total length of the user datagram header plus data Checksum : this field is used to detect errors over the entire user datagram (header plus data) . the checksum is described below.

The UDP checksum calculation is different from the one for IP and ICMP. Here the checksum includes three sections; a pseudo header, the UDP header and the data coming from the application layer

Java program to implement one way communication in a network using UDP protocol UDP Send import java.io.*; import java.net.*; public class UDPSend { static final int port=6010; public static void main(String args[]) throws Exception { byte[] message = new byte[1024]; int msglen = 0; String s; InetAddress address = InetAddress.getByName("localhost"); System.out.println("Write your message? (Bye for exit)"); BufferedInputStream in=new BufferedInputStream(System.in); boolean done=false; while(!done) { msglen = in.read(message,0,1024); // Initilize the packet with data and address DatagramPacket packet = new DatagramPacket(message, msglen, address, port);

// Create a socket, and send the packet through it. DatagramSocket socket = new DatagramSocket(); socket.send(packet); s = new String(message,0,msglen-2); //System.out.println("String message: "+s+" Length "+msglen-2); if (s.equals("Bye")) { done=true; } } } } UDP Receive

import java.io.*; import java.net.*; public class UDPReceive { public static void main(String args[]) throws Exception { byte[] buffer = new byte[1024]; String s; // Create a socket to listen on the port. DatagramSocket socket = new DatagramSocket(6010); for(;;) { //Constructs a DatagramPacket for receiving packets of length length. DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

// Wait to receive a datagram socket.receive(packet); // Convert the contents to a string s = new String(buffer, 0, packet.getLength()-2); // And display them System.out.println("UDPReceive: received from " + packet.getAddress().getHostName() + ":" + packet.getPort() + ": " + s); if(s.equals("Bye")) break; } } }

Reference 1 Forouzan A Behrouz; Data communications and Networking; The McGraw Hill Publications; 2006

Related Documents

What Is Udp
December 2019 13
Udp
November 2019 23
Udp
December 2019 18
Udp
June 2020 3
What Is
October 2019 115
What Is
May 2020 60

More Documents from "Randolph Dible"

What Is Udp
December 2019 13
Green Computing
November 2019 21
Acids And Bases
November 2019 17