[go: up one dir, main page]

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

Experiment 8

This document describes a Java program to establish a socket connection using the DatagramSocket class. It includes code for a UDP server that listens on port 9876 and receives data packets, extracting the message from each one. It also includes code for a UDP client that creates a datagram socket, sends a message "Hello, UDP Server!" to the server on localhost port 9876, and then closes the socket. When run, the server program outputs the received message.

Uploaded by

Anshika Singh
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)
27 views3 pages

Experiment 8

This document describes a Java program to establish a socket connection using the DatagramSocket class. It includes code for a UDP server that listens on port 9876 and receives data packets, extracting the message from each one. It also includes code for a UDP client that creates a datagram socket, sends a message "Hello, UDP Server!" to the server on localhost port 9876, and then closes the socket. When run, the server program outputs the received message.

Uploaded by

Anshika Singh
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
You are on page 1/ 3

EXPERIMENT-8

Objective: Write a java program to establish Socket


connection using DatagramSocket class.
Program:

UDP Server:

import java.net.DatagramPacket;

import java.net.DatagramSocket;

public class UDPServer {

public static void main(String[] args) {

try {

// Create a DatagramSocket that listens on a


specific port (e.g., 9876)

DatagramSocket serverSocket = new


DatagramSocket(9876);

byte[] receiveData = new byte[1024];

while (true) {

// Create a DatagramPacket to receive data

DatagramPacket receivePacket = new


DatagramPacket(receiveData, receiveData.length);

// Receive the data

serverSocket.receive(receivePacket);

// Extract the data from the packet

ANSHIKA SINGH (2100300130026)


String receivedMessage = new
String(receivePacket.getData(), 0,
receivePacket.getLength());

System.out.println("Received message: " +


receivedMessage);

// Clear the buffer for the next iteration

receiveData = new byte[1024];

} catch (Exception e) {

e.printStackTrace();

UDP Client:

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

public class UDPClient {

public static void main(String[] args) {

try {

// Create a DatagramSocket

DatagramSocket clientSocket = new


DatagramSocket();
ANSHIKA SINGH (2100300130026)
// Server address and port to send the data

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

int serverPort = 9876;

// Message to send

String messageToSend = "Hello, UDP Server!";

byte[] sendData = messageToSend.getBytes();

// Create a DatagramPacket to send data to the


server

DatagramPacket sendPacket = new


DatagramPacket(sendData, sendData.length,
serverAddress, serverPort);

// Send the data

clientSocket.send(sendPacket);

// Close the socket

clientSocket.close();

} catch (Exception e) {

e.printStackTrace();

OUTPUT:
Received message: Hello, UDP Server!
ANSHIKA SINGH (2100300130026)

You might also like