Practical 2 AJAVA 49
Practical 2 AJAVA 49
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.
Client Side:
package practicals;
import java.net.*;
import java.io.*;
/**
*
* @author janvi
*/
try{
DatagramSocket clientSocket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("localhost"); // Server address
byte[] sendData;
}
catch(Exception e){
}
}
Server Side :
package practicals;
import java.net.*;
import java.io.*;
/**
*
* @author janvi
*/
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
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
serverSocket.receive(receivePacket); // Receive data
AJAVA (3150707) Enroll no. 220280107049
}
catch(Exception e){
}
}
}
Output :