[go: up one dir, main page]

0% found this document useful (0 votes)
3 views4 pages

CN Lab 7

Uploaded by

Aadesh Lawande
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)
3 views4 pages

CN Lab 7

Uploaded by

Aadesh Lawande
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/ 4

LAB ASSIGNMENT - 7

Problem Statement : Write a program using UDP Sockets to enable file


transfer between two machines.

Code -

UDPFileServer.java:

import java.io.*;
import java.net.*;

public class UDPFileServer {

private static final int PORT = 9876;


private static final int BUFFER_SIZE = 1024;

public static void main(String[] args) {


DatagramSocket socket = null;

try {
// Create a socket to listen on the port
socket = new DatagramSocket(PORT);
byte[] receiveBuffer = new byte[BUFFER_SIZE];
System.out.println("Server is ready to receive files...");

// Receive the file name first


DatagramPacket fileNamePacket = new DatagramPacket(receiveBuffer,
receiveBuffer.length);
socket.receive(fileNamePacket);
String fileName = new String(fileNamePacket.getData(), 0,
fileNamePacket.getLength());
System.out.println("Receiving file: " + fileName);

// Create a file output stream to write the file


FileOutputStream fileOutputStream = new FileOutputStream(new
File("received_" + fileName));

// Receive file data in chunks


while (true) {
DatagramPacket receivePacket = new
DatagramPacket(receiveBuffer, receiveBuffer.length);
socket.receive(receivePacket);
String receivedData = new String(receivePacket.getData(), 0,
receivePacket.getLength());
// If the client sends "EOF", means file transfer is complete
if (receivedData.equals("EOF")) {
System.out.println("File transfer complete.");
break;
}

// Write the received bytes to the file


fileOutputStream.write(receivePacket.getData(), 0,
receivePacket.getLength());
}

fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
}
}

Client.java:

import java.io.*;
import java.net.*;

public class UDPFileClient {

private static final String SERVER_IP = "127.0.0.1"; // Change to


server's IP address
private static final int SERVER_PORT = 9876;
private static final int BUFFER_SIZE = 1024;

public static void main(String[] args) {


DatagramSocket socket = null;

try {
// Create a UDP socket
socket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName(SERVER_IP);

// Get the file to send


File file = new File("test.txt"); // Specify the file
String fileName = file.getName();

// Send the file name to the server


byte[] fileNameBytes = fileName.getBytes();
DatagramPacket fileNamePacket = new DatagramPacket(
fileNameBytes, fileNameBytes.length,
serverAddress, SERVER_PORT);
socket.send(fileNamePacket);
System.out.println("Sending file: " + fileName);

// Read the file and send it in chunks


FileInputStream fileInputStream = new FileInputStream(file);
byte[] sendBuffer = new byte[BUFFER_SIZE];
int bytesRead;

while ((bytesRead = fileInputStream.read(sendBuffer)) != -1){


DatagramPacket sendPacket = new DatagramPacket(
sendBuffer, bytesRead, serverAddress, SERVER_PORT);
socket.send(sendPacket);
}

// Send "EOF" to indicate the end of the file


byte[] eofMessage = "EOF".getBytes();
DatagramPacket eofPacket = new DatagramPacket(
eofMessage, eofMessage.length, serverAddress, SERVER_PORT);
socket.send(eofPacket);
System.out.println("File transfer complete.");

fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
}
}

test.txt:
Output -

You might also like