0% found this document useful (0 votes)
10 views3 pages

Experiment 11

Uploaded by

anok5062
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)
10 views3 pages

Experiment 11

Uploaded by

anok5062
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/ 3

EXP 11:UDP (User Datagram Protocol) Two-Way

CommunicatinCommunication

Algorithm:
Server Side:
1. Create a DatagramSocket to listen on a specific port.
2. Create a byte array to store incoming data (`receiveData`).
3. Continuously loop to receive and respond to data:
- Create a DatagramPacket to receive incoming data (`receivePacket`).
- Use `DatagramSocket.receive(receivePacket)` to receive data.
- Convert received data to a String (`receivedMessage`).
- Process or display the received message.
- Extract client's IP address and port from `receivePacket`.
- Prepare a response message and convert it to bytes (`sendData`).
- Create a DatagramPacket containing `sendData`, client's IP address, and port.
- Use `DatagramSocket.send(sendPacket)` to send the response.

Client Side:
1. Create a DatagramSocket (typically without specifying a port, let OS assign).
2. Convert the message to send into bytes (`sendData`).
3. Create a DatagramPacket containing `sendData`, the server's IP address, and port.
4. Use `DatagramSocket.send(sendPacket)` to send the packet.
5. Create a byte array to store incoming data (`receiveData`).
6. Create a DatagramPacket to receive incoming data (`receivePacket`).
7. Use `DatagramSocket.receive(receivePacket)` to receive data.
8. Convert received data to a String (`receivedMessage`).
9. Process or display the received message.
10. Close the DatagramSocket.
Source code:
In Two-Way Communicatincommunication, both sides can send and receive data simultaneously.

Server Side

import java.net.*;

public class FullDuplexUDPServer {


public static void main(String[] args) throws Exception {
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData;

while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String receivedMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Received from client: " + receivedMessage);

// Send response back to client


InetAddress clientIPAddress = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
String responseMessage = "Message received successfully!";
sendData = responseMessage.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientIPAddress,
clientPort);
serverSocket.send(sendPacket);
}
}
}

Client Side

import java.net.*;

public class FullDuplexUDPClient {


public static void main(String[] args) throws Exception {
DatagramSocket clientSocket = new DatagramSocket();
InetAddress serverIPAddress = InetAddress.getByName("localhost");
byte[] sendData;
byte[] receiveData = new byte[1024];
String message = "Hello UDP Server!";
sendData = message.getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverIPAddress,


9876);
clientSocket.send(sendPacket);

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


clientSocket.receive(receivePacket);
String receivedMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Received from server: " + receivedMessage);

clientSocket.close();
}
}

You might also like