0% found this document useful (0 votes)
2 views4 pages

Develop

Uploaded by

Shilpa Sannamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Develop

Uploaded by

Shilpa Sannamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

8.

Develop a program on datagram socket for client/server to display the messages on client
side, typed at the server side.

UDP SERVER:
import java.net.*;
import java.net.InetAddress;

class UDPServer {
public static void main(String args[]) throws Exception {
// Create a DatagramSocket to listen on port 9876
DatagramSocket serverSocket = new DatagramSocket(9876);

// Buffers to store incoming and outgoing data


byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];

while (true) {
// Server status message
System.out.println("Server is Up");

// Create a packet to receive incoming data


DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

// Receive data from the client


serverSocket.receive(receivePacket);

// Extract the data from the received packet and convert it to a string
String sentence = new String(receivePacket.getData());
System.out.println("RECEIVED: " + sentence);

// Get the IP address and port of the client


InetAddress IPAddress =
receivePacket.getAddress();
int port = receivePacket.getPort();

// Convert the received string to uppercase


String capitalizedSentence = sentence.toUpperCase();

// Convert the uppercase string to bytes


sendData = capitalizedSentence.getBytes();

// Create a packet to send the data back to the client


DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);

// Send the packet back to the client


serverSocket.send(sendPacket);
}
}
}

UDP CLIENT:
import java.io.*;
import java.net.*;
import java.net.InetAddress;

class UDPClient {
public static void main(String[] args) throws Exception {
// Create a BufferedReader to read input from the user
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

// Create a DatagramSocket to send and receive data


DatagramSocket clientSocket = new DatagramSocket();

// Get the IP address of the server (localhost in this case)


InetAddress IPAddress = InetAddress.getByName("localhost");

// Buffers to store outgoing and incoming data


byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];

// Prompt the user to enter a string to be converted to uppercase


System.out.println("Enter the string to be converted into Upper
case"); String sentence = inFromUser.readLine();

// Convert the string to bytes and store it in sendData


sendData = sentence.getBytes();

// Create a packet to send the data to the server


DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);

// Send the packet to the server


clientSocket.send(sendPacket);

// Create a packet to receive the data from the server


DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

// Receive the data from the server


clientSocket.receive(receivePacket);

// Convert the received data to a string


String modifiedSentence = new String(receivePacket.getData());

// Print the modified string received from the server


System.out.println("FROM SERVER: " + modifiedSentence);

// Close the socket


clientSocket.close();
}
}

You might also like