Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image Transfer with UDP on Android

I want to send an image through UDP on localhost Android but I don't know how to combine the image's packets. I read a jpeg file and send it to client side. I want to create this jpeg file on server side. What should I do? This my code but I have a permission denied error:

Client Side:

InetAddress serverAddr = InetAddress.getByName(Server.SERVERIP);
            FileInputStream fin = new FileInputStream("/sdcard/send.jpg");

             BufferedInputStream input = new BufferedInputStream(fin);
        Log.d("UDP", "C: Connecting...");
        et.append("C: Connecting...\n");

        /* Create new UDP-Socket */
        DatagramSocket socket = new DatagramSocket();
        byte[] sendData = new byte[Server.BUF_SIZE];
        byte[] buf = new byte[Server.BUF_SIZE];
        String sentence;
        int bytesRead = 0;
            while((bytesRead = input.read(buf, 0, Server.BUF_SIZE)) != -1) {
                sentence = new String(buf, 0, bytesRead);
                sendData = sentence.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
                        serverAddr, Server.SERVERPORT);
                socket.send(sendPacket);

Server Side:

File f = new File("/sdcard/out.jpg");

            f.createNewFile();

        FileOutputStream fos = new FileOutputStream(f); 
        BufferedOutputStream out = new BufferedOutputStream(fos);
        InetAddress serverAddr = InetAddress.getByName(SERVERIP);

        byte[] receiveData = new byte[BUF_SIZE];
        String sentence;
        Log.d("UDP", "S: Connecting...");
        et.append("S: Connecting...\n");
        DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr);
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        socket.receive(receivePacket);
        byte[] buf = new byte[Server.BUF_SIZE];
        sentence = new String(receivePacket.getData());
        et.append(sentence);
        buf= sentence.getBytes();   
        out.write( buf,0, Server.BUF_SIZE );
like image 313
ecakmak Avatar asked Nov 27 '25 13:11

ecakmak


1 Answers

If you're going to use UDP, you have to implement any features UDP doesn't provide that you need. Unfortunately, in this case UDP doesn't provide a long list of features that you need. There's simply no possible way this code can work.

You need, but don't provide:

1) Transmit pacing: Your code just sends data as fast as it can with no consideration for the capabilities of the network between the devices.

2) Packet ordering: Your code does not identify the packets with any kind of sequence number. If they are received out of order, the data cannot be recovered.

3) Retransmissions: If a packet is lost in transit, the data is just lost. There is no way the receiver can detect this and request a retransmission.

4) Connections: Your receiver has no way to know when the transmitter is going to start sending, when it should start receiving, when it is done receiving, and so on.

5) MTU detection: Unless you really don't care at all about performance, you want to detect the path MTU. Send datagrams that are larger than the path MTU and packet loss goes up exponentially. Send datagrams that are smaller than the path MTU and efficiency goes down.

6) Link sharing: Unless you have a link all to yourself, you need to work out a backoff and run up algorithm. Back off too much and your throughput will drop drastically when other connections use the same link. Ramp up too aggressively and you'll cause packet loss and traffic jams when you have to share a link with other traffic.

If you want to, you can implement all of these things in your own code. But TCP already has them all, and TCP has been designed, implemented, and refined by the finest minds on the planet. So it's very unlikely you would do a better job.

like image 181
David Schwartz Avatar answered Nov 30 '25 01:11

David Schwartz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!