Java Practical No 26
Java Practical No 26
Title code
Server Side:
import java.net.*;
public class UDPSender {
public static void main(String[] args) {
try {
DatagramSocket socket = new DatagramSocket();
String message = "Hello, this is UDP!";
InetAddress receiverAddress = InetAddress.getByName("localhost");
byte[] sendData = message.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, receiverAddress, 5000);
socket.send(sendPacket);
System.out.println("Message sent: " + message);
socket.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Client Side
import java.net.*;
public class UDPReceiver {
public static void main(String[] args) {
try {
DatagramSocket socket = new DatagramSocket(5000);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
System.out.println("Waiting for message...");
socket.receive(receivePacket);
String message = new String(receivePacket.getData(), 0,
receivePacket.getLength());
System.out.println("Message received: " + message);
socket.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
O/P:
Server side:
Client Side:
3. Write a program using DatagramPacket and
DatagramSocket to create chat application.
Ans:
Sender Code:
import java.net.*;
import java.util.Scanner;
public class Sender {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
InetAddress ip = InetAddress.getByName("localhost");
Scanner sc = new Scanner(System.in);
while (true) {
String msg = sc.nextLine();
byte[] buffer = msg.getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, ip,
5000);
socket.send(packet);
if (msg.equalsIgnoreCase("exit")) {
break;
}
}
socket.close();
sc.close();
}
}
Receiver Side Code:
import java.net.*;
public class Receiver {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(5000);
byte[] buffer = new byte[1024];
while (true) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String msg = new String(packet.getData(), 0, packet.getLength());
System.out.println("Received: " + msg);
if (msg.equalsIgnoreCase("exit")) {
break;
}
}
socket.close();
}
}
FileSender:
import java.io.*;
import java.net.*;
public class FileSender {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();
InetAddress address = InetAddress.getByName("localhost");
File file = new File("source.txt");
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
DatagramPacket packet = new DatagramPacket(buffer,
bytesRead, address, 5000);
socket.send(packet);
}
byte[] endSignal = "END".getBytes();
DatagramPacket endPacket = new DatagramPacket(endSignal,
endSignal.length, address, 5000);
socket.send(endPacket);
fis.close();
socket.close();
}
}
O/P:
FileSender:
FileReceiver:
FileReceiver:
import java.io.*;
import java.net.*;
public class FileReceiver {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(5000);
File file = new File("C:\\Dipakjavapr\\destination.txt"); // Updated
destination path
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
while (true) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String receivedData = new String(packet.getData(), 0,
packet.getLength());
if (receivedData.equals("END")) {
break;
}
System.out.println("Received: " + receivedData);
fos.write(packet.getData(), 0, packet.getLength());
}
fos.close();
socket.close();
System.out.println("File received successfully at: " +
file.getAbsolutePath());
}
}
O/P:
FileSender Terminal:
FileReceiver Terminal: