this post was submitted on 11 Nov 2024
3 points (100.0% liked)

Java

1376 readers
27 users here now

For discussing Java, the JVM, languages that run on the JVM, and other related technologies.

founded 1 year ago
MODERATORS
 

So I am working on a homework project for my network architure class were I have to devlop an FTP program using UDP (not TCP). I was able to get my GET and PUT functions to send a 21byte test file and it worked. However when I tried to do a 1MB file the program hanged and did not send all of the data. I need to be able to send files of 1MB, 25MB, 50MB, and 100MB. Here is my code.

GET (Client Side)

 private static void getFile(String serverFilePath, String clientFilePath) throws IOException 
	{
        clientFilePath = clientFilePath.replaceAll("^\"|\"$", ""); //Strip quotes if any.
		if(!clientFilePath.startsWith("/"))
			clientFilePath = currentDir + "/" + clientFilePath;
		
		sendMessage("GET " + serverFilePath);
		
		//Write file to client
        try (FileOutputStream fos = new FileOutputStream(clientFilePath)) 
		{
            while (true) 
			{
                byte[] buffer = new byte[BUFFER_SIZE];
                DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
                socket.receive(packet);

                String data = new String(packet.getData(), 0, packet.getLength());
                if (data.equals("DONE")) break;

                fos.write(packet.getData(), 0, packet.getLength());
            }
        }
        System.out.println("[Client] File downloaded successfully.");
    }

PUT (Client Side)

private static void putFile(String clientFilePath, String serverFilePath) throws IOException 
	{
        clientFilePath = clientFilePath.replaceAll("^\"|\"$", ""); //Strip quotes if any.
		if(!clientFilePath.startsWith("/"))
			clientFilePath = currentDir + "/" + clientFilePath;
			
		sendMessage("PUT " + serverFilePath);
		
		//Upload file to server
        try (FileInputStream fis = new FileInputStream(clientFilePath)) 
		{
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) 
			{
                DatagramPacket packet = new DatagramPacket(buffer, bytesRead, serverAddress, SERVER_PORT);
                socket.send(packet);
            }
            sendMessage("DONE");
        }
        System.out.println("[Client] File uploaded successfully.");
    }

GET (Server Side)

 private static void handleGet(String filePath, InetAddress clientAddress, int clientPort) throws IOException 
	{
        filePath = filePath.replaceAll("^\"|\"$", ""); //Strip quotes if any.
		if(!filePath.startsWith("/"))
			filePath = currentDir + "/" + filePath;
		
		//Check is file exists
		File file = new File(filePath);
        if (!file.exists()) 
		{
            sendMessage("[Server] Error File not found", clientAddress, clientPort);
            return;
        }
		
		//Write file to server
        try (FileInputStream fis = new FileInputStream(file)) 
		{
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) 
			{
                DatagramPacket packet = new DatagramPacket(buffer, bytesRead, clientAddress, clientPort);
                socket.send(packet);
            }
            sendMessage("DONE", clientAddress, clientPort);
        }
    }

PUT (Server Side)

private static void handlePut(String filePath, InetAddress clientAddress, int clientPort) throws IOException 
	{
        filePath = filePath.replaceAll("^\"|\"$", ""); //Strip quotes if any.
		if(!filePath.startsWith("/"))
			filePath = currentDir + "/" + filePath;
		
		File file = new File(filePath);
        try (FileOutputStream fos = new FileOutputStream(file)) 
		{
            while (true) 
			{
                byte[] buffer = new byte[BUFFER_SIZE];
                DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
                socket.receive(packet);

                String data = new String(packet.getData(), 0, packet.getLength());
                if (data.equals("DONE")) break;

                fos.write(packet.getData(), 0, packet.getLength());
            }
        }
        sendMessage("DONE", clientAddress, clientPort);
    }
top 2 comments
sorted by: hot top controversial new old
[โ€“] OneCardboardBox@lemmy.sdf.org 5 points 6 days ago* (last edited 6 days ago) (1 children)

Just from a quick glance, you might want to check your client-side GET request: Ask yourself what happens when the file is larger than your packet buffer? Use the debugger or print statements to confirm if your expectations match reality. It might help to do some testing with a really tiny buffer (eg 5 bytes) so you can step through your 21 byte payload easily. What happens when your buffer is smaller than the DONE packet? If your implementation is correct then we would expect that even a 1 byte buffer should work in all cases.

I see a similar code pattern in your server-side PUT request, so if you can solve it for one half, you should be able to fix both.

I lowered the buffersize to 5 and sent the 21 byte test file and it was able to go through. However When I made the buffersize 3 (because my DONE packet was 4 bytes) is hanged and it appended the word DONE without the E.