2

It would seem that the Client - Server application i wrote does work however it seems that not all data is processed every time.

I am testing it on a local machine in Eclipse env.

Server:

    private void sendData() throws Exception
{
    DatagramPacket data = new DatagramPacket(outgoingData, outgoingData.length, clientAddress, clientPort);
    InputStream fis = new FileInputStream(responseData);

    int a;
    while((a = fis.read(outgoingData,0,512)) != -1)
    {
        serverSocket.send(data);
    }
}

Client:

    private void receiveData() throws Exception
{
    DatagramPacket receiveData = new DatagramPacket(incomingData, incomingData.length);
    OutputStream fos = new FileOutputStream(new File("1"+data));
    while(true)
    {
        clientSocket.receive(receiveData);
        fos.write(incomingData);
    }
}

I used to have if else in the while(true) loop to check if packet length is less than 512 bytes so it knew when to break;

I was thinking there was a problem whit that but seems that was oke for now i just wait few minutes and then stop the Client.java app

The file does transfer but the original file is 852kb and so far i got 777, 800, 850,.. but never all of it.

Sterling Duchess
  • 1,970
  • 16
  • 51
  • 91
  • Have you tried flushing the streams? – Doug Moscrop Mar 06 '12 at 18:47
  • 2
    UDP is unreliable. The spec for UDP states that UDP will make a "best effort" to get a packet from source to receiver, but packets may be dropped and the receive/sender will never be notified. If you need to reliably send data, you're going to need to implement some kind of packet loss detection scheme or use TCP instead. It is a little odd that you are losing so many packets in the local sense, but there is no contract stating that local UDP connections must be reliable. – CodeBlind Mar 06 '12 at 18:48
  • I understand that but still i am trying to figure out how i could help out to get as much out as possible. – Sterling Duchess Mar 06 '12 at 18:50
  • Must check how UDP works. Then, modify your work to handle messages with lost packets – Alfabravo Mar 06 '12 at 19:04

2 Answers2

1

The fundamental problem with your approach is that UDP does not guarantee delivery. If you have to use UDP (rather than, say, TCP), you have to implement a scheme that would detect and deal with packets that got lost, arrive out of order, or are delivered multiple times.

See When is it appropriate to use UDP instead of TCP?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

You should probably use TCP to transfer files. You are probably losing packets because you are sending them so fast in that while loop.

int a;
while((a = fis.read(outgoingData,0,512)) != -1)
{
   serverSocket.send(data);
}

since you're sending so fast I highly doubt it will have a chance to be received in the right order. some packets will probably be lost because of it too.

Also since your sending a fixed size of 512 bytes the last packet you send will probably not be exactly that size, so you will see the end of the file "look wierd."

WalterM
  • 2,686
  • 1
  • 19
  • 26