LAB 8


SOCKET PROGRAMMING USING UDP

UDP

There are two major transport layer protocols to communicate between hosts TCP and UDP.

In UDP, the client does not form a connection with the server like in TCP and instead just sends a datagram. Similarly, the server need not accept a connection and just waits for datagrams to arrive. Datagrams upon arrival contain the address of sender which the server uses to send data to the correct client.

SERVER SIDE IMPLEMENTATION


// Implementation using DatagramSocket
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
{
// Step 1 : Create a socket to listen at port 1234
DatagramSocket ds = new DatagramSocket(1234);
byte[] receive = new byte[65535];

    DatagramPacket DpReceive = null; 
    while (true) 
    { 

        // Step 2 : create a DatgramPacket to receive the data. 
        DpReceive = new DatagramPacket(receive, receive.length); 

        // Step 3 : revieve the data in byte buffer. 
        ds.receive(DpReceive); 

        System.out.println("Client:-" + data(receive)); 

        // Exit the server if the client sends "bye" 
        if (data(receive).toString().equals("bye")) 
        { 
            System.out.println("Client sent bye.....EXITING"); 
            break; 
        } 

        // Clear the buffer after every message. 
        receive = new byte[65535]; 
    } 
} 

// A utility method to convert the byte array 
// data into a string representation. 
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; 
} 

}

CLIENT SIDE IMPLEMENTATION


// Implementation using DatagramSocket
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);

    // Step 1:Create the socket object for 
    // carrying the data. 
    DatagramSocket ds = new DatagramSocket(); 

    InetAddress ip = InetAddress.getLocalHost(); 
    byte buf[] = null; 

    // loop while user not enters "bye" 
    while (true) 
    { 
        String inp = sc.nextLine(); 

        // convert the String input into the byte array. 
        buf = inp.getBytes(); 

        // Step 2 : Create the datagramPacket for sending 
        // the data. 
        DatagramPacket DpSend = 
            new DatagramPacket(buf, buf.length, ip, 1234); 

        // Step 3 : invoke the send call to actually send 
        // the data. 
        ds.send(DpSend); 

        // break the loop if user enters "bye" 
        if (inp.equals("bye")) 
            break; 
    } 
} 

}