Jaqualembo
Jaqualembo

Reputation: 201

Java UDP file transfer application

I'm trying to create a simple UDP file transfer application (server and client) in Java, I have barely any experience in this though, still going through some of the basics. I've managed to make a functional application that will send a chosen file from my laptop to my desktop for instance, but using TCP.

Essentially, what I want to do is an application where you can choose an option to list the files on the server (a specific default folder, in this case C:\Sharefolder), an option to upload a file to the server, and another option to download a file from the server. How can I do this in terms of methods in the client?

This is the code I have so far for the client, I have no idea if I'm going in the right direction here. I also still have some chunks of my old TCP program code in the bottom area, so nevermind that.

public class Client {

private final static int PACKETSIZE = 100 ;

public static void upload(String ipadd, String iport, String fname) {

    DatagramSocket socket = null ;

    try {
        InetAddress host = InetAddress.getByName(ipadd);
        int port = Integer.parseInt(iport);

        socket = new DatagramSocket(port, host) ;

        File file = new File("C:\\Sharefolder\\" +fname);
        long size = file.length();
        if (size > Integer.MAX_VALUE) {
            System.out.println("File too big");
        }
        byte[] bytes = new byte[(int) size];
        for(int i=0; i<size; i++) {
            //Send file bytes to a byte array?
        }

        DatagramPacket packet = new DatagramPacket(bytes, bytes.length, host, port ) ;


        byte[] bytes = new byte[(int) size];
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

        int count;

        while ((count = bis.read(bytes)) > 0) {
            out.write(bytes, 0, count);
        }

        out.flush();
        out.close();
        fis.close();
        bis.close();
        socket.close();

If anyone could point me in any direction that could help me check what I should be doing on my code that'd be much appreciated. Thanks!

Upvotes: 0

Views: 6657

Answers (2)

Yue Zhu
Yue Zhu

Reputation: 311

This programming assignment is to let you design a simple file transfer protocol, which is an application layer protocol. But not like FTP, you have to use UDP as underlying transport protocol. If you want it to be a little robust, I think you may have to address the following concerns:

  • UDP protocol is an unreliable protocol; it provides no guarantee that all packets are delivered and delivered in order. So you have to implement your own integrity check at application level. The integrity check can be SHA-1. Before you send a chunk, you specify the length and SHA-1 for this chunk and include them in your protocol message header. So the receiver is able to check the meta information. If not match, the sender has to retransmit the previous chunk.
  • Regarding the protocol, you may design a text-based protocol. The text-based protocol contains protocol status line, body and content. Each line is terminated by "\r\n", and each part is terminated by "\r\n". similar to HTTP protocol. The status line indicates your command - list, upload, load, and body indicates the sequence number, length, SHA-1, etc..

Upvotes: 1

SJuan76
SJuan76

Reputation: 24885

I do not understand the request of do this in terms of method in the client. At high level, you will have the methods that your operations define (a listFiles(path), a uploadFile(localPath, remotePath) and a downloadFile(localPath, remotePath)).

The issues is that UDP is unreliable so you will have to control that the data really comes.

A few tips of what you need:

1) a mini protocol describing your content format (which message for each command commands, how to pass the parameters/data), etc.

2) Once a message has been send, you need to set a timeout and (if no reply has been received) send the message again.

3) When you receive/send files, it will be in several chunks (Datagrams max payload is 64K, unless you are using IPv6). Control the order, check if all of them arrive, request back the ones that did not arrive.

4) To have fun!

It is way more complicated than with TCP, because you will have to control a lot of things that TCP controlled for you, but can be done. Since you do not mention them, I would check the restrains with the people who gave you the assignment (can you use IPv6? Which is the maximum file size needed to pass?)

Upvotes: 1

Related Questions