Tuesday, April 23, 2019


Socket Programming using UDP Socket

Socket:
Sockets allow communication between two different processes on the same or different machines. A Unix Socket is used in a client-server application framework. A server is a process that performs some functions on request from a client. The client in socket programming must know two information:
  • IP Address of Server, and
  • Port number.

Java DatagramSocket class:
Java DatagramSocket class represents a connection-less socket for sending and receiving datagram packets.
A datagram is basically an information but there is no guarantee of its content, arrival or arrival time.
Commonly used Constructors of DatagramSocket class:
  • DatagramSocket() throws SocketEeption: it creates a datagram socket and binds it with the available Port Number on the localhost machine.
  • DatagramSocket(int port) throws SocketEeption: it creates a datagram socket and binds it with the given Port Number.
  • DatagramSocket(int port, InetAddress address) throws SocketEeption: it creates a datagram socket and binds it with the specified port number and host address.



Java Program for Server side using UDP:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class udpBaseServer_2 {
            public static void main(String[] args) throws IOException {
                        DatagramSocket ds = new DatagramSocket(1234);
                        byte[] receive = new byte[65535];
                        DatagramPacket DpReceive = null;
                        while (true) {
                                    DpReceive = new DatagramPacket(receive, receive.length);
                                    ds.receive(DpReceive);
                                    System.out.println("Client:-" + data(receive));
                                    if (data(receive).toString().equals("bye")) {
                                                System.out.println("Client sent bye.....EXITING");
                                                break;
                                    }
                                    receive = new byte[65535];
                        }
            }
            public static StringBuilder data(byte[] a) {
                        if (a == null)
                                    return null;
                        StringBuilder ret = new StringBuilder();
                        int i = 0;
                        while (a[i] != 0) {
                                    ret.append((char) a[i]);
                                    i++;
                        }
                        return ret;
            }
}

Java Program for Client side using UDP:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class udpBaseClient_2 {
            public static void main(String args[]) throws IOException {
                        Scanner sc = new Scanner(System.in);
                        DatagramSocket ds = new DatagramSocket();
                        InetAddress ip = InetAddress.getLocalHost();
                        byte buf[] = null;
                        while (true) {
                                    String inp = sc.nextLine();
                                    buf = inp.getBytes();

                                    DatagramPacket DpSend =
                                                new DatagramPacket(buf, buf.length, ip, 1234);
                                    ds.send(DpSend);
                                    if (inp.equals("bye"))
                                                break;
                        }
            }
}


Snapshots:

                        Client                                                                                      Server

No comments:

Post a Comment

Socket Programming using TCP Socket Socket: Sockets allow communication between two different processes on the same or different ma...