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

Parallel and Distributed Computing

The document contains an assignment submission for a Parallel and Distributed Computing course, authored by M. Danyal. It includes Java code for a server (MyServer.java) that listens for client messages and echoes them back, and a client (MyClient.java) that sends a message to the server and receives the echoed response. The server operates on port 9876 and uses DatagramSockets for communication.

Uploaded by

daniyal qadri
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)
6 views4 pages

Parallel and Distributed Computing

The document contains an assignment submission for a Parallel and Distributed Computing course, authored by M. Danyal. It includes Java code for a server (MyServer.java) that listens for client messages and echoes them back, and a client (MyClient.java) that sends a message to the server and receives the echoed response. The server operates on port 9876 and uses DatagramSockets for communication.

Uploaded by

daniyal qadri
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/ 4

Assignment No 3

Name M.DANYAL

Class BSCS-6A

Arid-No 22-ARID-3106

Subject Parallel And Distributed


Computing

Submitted To = Mam Sadia Zar

Solution : -

Server Code (MyServer.java)

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class MyServer {


public static void main(String[] args) {
final int SERVER_PORT = 9876;
try {
DatagramSocket serverSocket = new
DatagramSocket(SERVER_PORT);
byte[] receiveData = new byte[1024];
byte[] sendData;

System.out.println("Server started.
Waiting for clients...");

while (true) {
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);

String clientMessage = new


String(receivePacket.getData(), 0,
receivePacket.getLength());
System.out.println("Received from
client: " + clientMessage);

// Echo the message back


sendData = clientMessage.getBytes();

DatagramPacket sendPacket = new


DatagramPacket(
sendData,
sendData.length,
receivePacket.getAddress(),
receivePacket.getPort()
);
serverSocket.send(sendPacket);
}
} catch (Exception e) {
System.out.println("Server Error: " +
e.getMessage());
}
}
}

Client Code (MyClient.java)

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class MyClient {


public static void main(String[] args) {
final String SERVER_IP = "localhost"; // or
your server IP
final int SERVER_PORT = 9876;

try {
DatagramSocket clientSocket = new
DatagramSocket();
InetAddress serverAddress =
InetAddress.getByName(SERVER_IP);

Scanner scanner = new Scanner(System.in);


System.out.print("Enter your Name and
Registration Number: ");
String message = scanner.nextLine();

byte[] sendData = message.getBytes();


DatagramPacket sendPacket = new
DatagramPacket(sendData, sendData.length,
serverAddress, SERVER_PORT);
clientSocket.send(sendPacket);

byte[] receiveData = new byte[1024];


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

String echoedMessage = new


String(receivePacket.getData(), 0,
receivePacket.getLength());
System.out.println("Echoed from server: "
+ echoedMessage);

clientSocket.close();
scanner.close();
} catch (Exception e) {
System.out.println("Client Error: " +
e.getMessage());
}
}
}

You might also like