0% found this document useful (0 votes)
8 views

Java Practical No 26

The document provides Java code examples for implementing UDP communication using DatagramPacket and DatagramSocket. It includes a simple chat application with sender and receiver classes, as well as file transfer functionality to copy contents from one file to another. The code demonstrates both sending and receiving messages and files over a network using UDP protocol.

Uploaded by

dipya2121
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Practical No 26

The document provides Java code examples for implementing UDP communication using DatagramPacket and DatagramSocket. It includes a simple chat application with sender and receiver classes, as well as file transfer functionality to copy contents from one file to another. The code demonstrates both sending and receiving messages and files over a network using UDP protocol.

Uploaded by

dipya2121
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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();
}
}

Sender Side Terminal:

Receiver side Terminal:


4. Write a program using DatagramPacket and
DataGramSocket to copy the contents of one
file into other.
Ans:
FileReceiver:
import java.io.*;
import java.net.*;
public class FileReceiver {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(5000);
FileOutputStream fos = new FileOutputStream("destination.txt");
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();
}
}

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:

5. Write a program using DatagramPacket and


DatagramSocket to transfer the file from one
location to another.
Ans:
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("C:\\Dipakjavapr\\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();
System.out.println("File sent successfully.");
}
}

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:

You might also like