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

Practical 2 AJAVA 49

The document describes a practical assignment for implementing client-server communication using UDP Socket API in Java. It includes a Java program where a client sends a string message to a server, which counts the characters and sends the count back, with the capability to handle multiple clients simultaneously. The provided code outlines both the client and server implementations, demonstrating file transfer and data reception processes.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Practical 2 AJAVA 49

The document describes a practical assignment for implementing client-server communication using UDP Socket API in Java. It includes a Java program where a client sends a string message to a server, which counts the characters and sends the count back, with the capability to handle multiple clients simultaneously. The provided code outlines both the client and server implementations, demonstrating file transfer and data reception processes.

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 PDF, TXT or read online on Scribd
You are on page 1/ 3

AJAVA (3150707) Enroll no.

220280107049

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
}
AJAVA (3150707) Enroll no. 220280107049

// 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
AJAVA (3150707) Enroll no. 220280107049

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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy