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);
    }
  • Lanky_Pomegranate530@midwest.socialOP
    link
    fedilink
    arrow-up
    1
    ·
    4 days ago

    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.