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

Practical 2 - AJAVA

The document outlines a practical exercise for implementing client-server communication using UDP Socket API in Java. The client sends a string message to the server, which counts the characters and sends the count back, while also supporting multiple clients simultaneously. The provided Java code includes both client and server implementations for sending and receiving data, along with file handling.

Uploaded by

drishna4505
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)
6 views3 pages

Practical 2 - AJAVA

The document outlines a practical exercise for implementing client-server communication using UDP Socket API in Java. The client sends a string message to the server, which counts the characters and sends the count back, while also supporting multiple clients simultaneously. The provided Java code includes both client and server implementations for sending and receiving data, along with file handling.

Uploaded by

drishna4505
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

Practical 2

Aim : To implement client-server communication using UDP Socket API.


Given Problem:
Write a java program where client sends a string as a message and
sever counts the characters in the received message from client. Server sends this
value back to the client. Server should be able to serve multiple clients
simultaneously.

Solution (Java Code) :

Client Side:

package practicals;
import java.net.*;
import java.io.*;
/**
*
* @author janvi
*/

public class Pract_2_Client {


public static void main(String[] args) {

try{
DatagramSocket clientSocket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("localhost"); // Server address
byte[] sendData;

File f = new File("sample.txt"); // File to be sent


BufferedReader br = new BufferedReader(new FileReader(f));
String line;

System.out.println("Sending file to server...");

while ((line = br.readLine()) != null) {


sendData = line.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, serverAddress, 9876);
clientSocket.send(sendPacket); // Send data
Thread.sleep(100); // Small delay to avoid packet loss
}
// Send "END" signal
sendData = "END".getBytes();
DatagramPacket endPacket = new DatagramPacket(sendData,
sendData.length, serverAddress, 9876);
clientSocket.send(endPacket);

System.out.println("File sent successfully.");


br.close();
clientSocket.close();

}
catch(Exception e){
}
}

Server Side :

package practicals;
import java.net.*;
import java.io.*;
/**
*
* @author janvi
*/

public class Pract_2_Server {


public static void main(String[] args) {

try{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024]; // Buffer for receiving data
FileOutputStream fos = new FileOutputStream("received_file.txt"); // File to
store received data

System.out.println("Server is waiting for file...");

while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
serverSocket.receive(receivePacket); // Receive data
String receivedText = new String(receivePacket.getData(), 0,
receivePacket.getLength());
if (receivedText.equals("END")) break; // Stop when "END" is received

fos.write(receivedText.getBytes()); // Write data to file


fos.flush();
}

System.out.println("File received successfully.");


fos.close();
serverSocket.close();

}
catch(Exception e){
}
}
}

Output :

Signature of Faculty Grade

You might also like