Saturday 20 April 2013

UDP client-server




AIM:
            Program for creating UDP client-server.

THEORY:
Socket Programming using UDP

            A UDP socket provides an unreliable bi-directional communication channel from one process to another
» A “datagram” abstraction
            Datagram packets are used to implement a connectionless packet delivery service supported by the UDP protocol. Each message is transferred from source machine to destination based on information contained within that packet. That means, each packet needs to have destination address and each packet might be routed differently, and might arrive in any order. Packet delivery is not guaranteed.
The format of datagram packet is:
                        | Msg | length | Host | serverPort |

A simple UDP server program that waits for client’s requests and then accepts the message (datagram) and sends back the same message is given below. Of course, an extended server program can manipulate client’s messages/request and send a new message as a response.

                        SERVER                                                            CLIENT
Fig :Client-Server communication in UDP


CONCLUSION:
            A UDP socket provides an unreliable bi-directional communication channel.









PROGRAM:
UDPServer.java
import java.util.*;
import java.io.*;
import java.net.*;
class UDPServer
{
public static void main(String S[])throws IOException
{
byte[] receive_data=new byte[1024];
byte[] send_data=new byte[1024];
DatagramSocket Server_Socket=new DatagramSocket(5000);
System.out.println("UDPServer waiting for client on port 5000");
while(true)
{
DatagramPacket receive_packet=new DatagramPacket(receive_data,receive_data.length);
Server_Socket.receive(receive_packet);
String data=new String(receive_packet.getData(),0,receive_packet.getLength());
InetAddress IPAddress=receive_packet.getAddress();
int receive_port=receive_packet.getPort();
if(data.equals("q")||data.equals("Q"))
break;
else
System.out.println("IPAdress"+receive_port+"said:"+data);
}
}
}
UDPClient.java
import java.util.*;
import java.io.*;
import java.net.*;
class UDPClient
{
public static void main(String S[])throws IOException
{
Scanner K=new Scanner(System.in);
byte[] send_data=new byte[1024];
DatagramSocket Client_Socket=new DatagramSocket();
InetAddress IPAddress=InetAddress.getByName("127.0.0.1");
while(true)
{
System.out.println("Type Something (q or Q to Quit)");
String data =K.nextLine();
if(data.equals("q")||data.equals("Q"))
break;
else
{
send_data=data.getBytes();
DatagramPacket send_Packet=new DatagramPacket(send_data,send_data.length,IPAddress,5000);
Client_Socket.send(send_Packet);
}
}
}
}

OUTPUT:
ۑ閩睞隌睞퍌퍜障睞

督卬ۑ閩睞隌睞障睞

Objective Questions
1 ______  is a connection-oriented and reliable protocol, ______ is a less reliable protocol.
2 The TCP and UDP protocols use ______ to map incoming data to a particular process running on a computer.
3 Datagrams are normally sent by ______  protocol.
4 Class ______ is used to create a packet of data for UDP protocol.
5 If something goes wrong related to the network, ______ will be thrown when dealing with TCP/UDP programming in Java.
6 The same port number can be reused many times when binding with sockets simultaneously: True or False.
7.UDP ( User Datagram Protocol) is
   (A)  Connectionless               (B)  Message Oriented
   (C)  Connection oriented       (D)  Both ( A) and ( B)

1 comment:

  1. 1) TCP, UDP
    2) Ports
    3) UDP
    4) DatagramPacket
    5) exception
    6) true
    7) ans D

    ReplyDelete