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

UDP Client-Server Communication Example

The document provides Java code for a UDP client-server application where the server listens on port 9876 and responds to messages from the client. The server receives messages, processes them, and sends back a confirmation response. The client sends messages to the server and waits for responses, demonstrating the UDP communication process.

Uploaded by

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

UDP Client-Server Communication Example

The document provides Java code for a UDP client-server application where the server listens on port 9876 and responds to messages from the client. The server receives messages, processes them, and sends back a confirmation response. The client sends messages to the server and waits for responses, demonstrating the UDP communication process.

Uploaded by

YouTube Genix
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q. Send packet between client and server using UDP.

SERVER SIDE
import [Link].*;

public class udpsocket {

public static void main(String[] args) {

DatagramSocket socket = null;

try {

// Create a DatagramSocket to listen on a specific port

socket = new DatagramSocket(9876); // Server port

[Link]("Server is listening on port 9876...");

byte[] receiveData = new byte[1024];

while (true) {

// Create a DatagramPacket to receive data from the client

DatagramPacket receivePacket = new DatagramPacket(receiveData, [Link]);

// Receive data from the client

[Link](receivePacket);

// Extract message from the packet

String message = new String([Link](), 0, [Link]());

[Link]("Received message: " + message);

// Get client's address and port to send a response

InetAddress clientAddress = [Link]();

int clientPort = [Link]();

// Prepare response message

String responseMessage = "Message received: " + message;

byte[] sendData = [Link]();

// Send the response to the client

DatagramPacket sendPacket = new DatagramPacket(sendData, [Link], clientAddress, clientPort);

[Link](sendPacket);

[Link]("Response sent to client.");

}
} catch (Exception e) {

[Link]();

} finally {

if (socket != null && ![Link]()) {

[Link]();

CLIENT SIDE
import [Link].*;

import [Link];

public class UDPClient {

public static void main(String[] args) {

DatagramSocket socket = null;

Scanner scanner = new Scanner([Link]);

try {

// Create a DatagramSocket to send data to the server

socket = new DatagramSocket();

InetAddress serverAddress = [Link]("localhost"); // Server address (can be changed to the server's IP)

int serverPort = 9876; // Server port

while (true) {

// Get the message from the user

[Link]("Enter message to send to server: ");

String message = [Link]();

// Convert message to bytes

byte[] sendData = [Link]();

// Create a DatagramPacket to send the message to the server

DatagramPacket sendPacket = new DatagramPacket(sendData, [Link], serverAddress, serverPort);

[Link](sendPacket);
[Link]("Message sent to server.");

// Receive the response from the server

byte[] receiveData = new byte[1024];

DatagramPacket receivePacket = new DatagramPacket(receiveData, [Link]);

[Link](receivePacket);

// Extract the server's response message

String serverMessage = new String([Link](), 0, [Link]());

[Link]("Response from server: " + serverMessage);

} catch (Exception e) {

[Link]();

} finally {

if (socket != null && ![Link]()) {

[Link]();

[Link]();

OUTPUT

SERVER SIDE CLIENT SIDE

Server is listening on port 9876… Enter message to send t o server: Hello World

Received Message : Hello World Message sent to server

Response sent to client Response from server : Message Received:

Hello World
ALGORITHM

SERVER SIDE :
Start

Create a UDP socket using socket()

Bind the socket to a specific IP address and port using bind()

Loop:

Wait to receive data from client using recvfrom()

Extract the data and client address

Process the data if needed

Optionally, send a response using sendto()

End Loop (or close socket on termination)

Close the socket

End

CLIENT SIDE:
Start

Create a UDP socket using socket()

Get the server IP address and port number

Prepare the data to be sent

Use sendto() to send the packet to server

Optionally, wait and receive the response from server using recvfrom()

Close the socket

End

You might also like