1

For a homework assignment: My task is to write a UDP file transfer program with acknowledgements... basically emulating TCP.

I decided to approach this task in stages and and very early on I have a problem with the received file. I send a small text file with the characters "test file" and the file created at the other end contains the matching ascii HEX values with padding of 0x00.

I want the received file to be exactly the same as the sent file.

Could someone please point out the problem?

tia

public class UDPRead{
    DatagramSocket socket;
    String filename, initString;
    byte[] buffer;
    DatagramPacket initPacket, receivedPacket;
    FileOutputStream fileWriter;
    int bytesReceived, bytesToReceive;

    public UDPRead(int port) throws IOException
    {

    socket = new DatagramSocket(port);
    buffer = new byte[512]; 

    initPacket = receivePacket();

    StringTokenizer token = new StringTokenizer(initString, "::");
    filename = token.nextToken();
    bytesToReceive = new Integer(token.nextToken()).intValue();

    //Send reply containing SEND to the sender
    send(initPacket.getAddress(), initPacket.getPort(), (new String("SEND")).getBytes());

    fileWriter = new FileOutputStream(filename);

    while(bytesReceived < bytesToReceive)
        {
        receivedPacket = receivePacket();
        fileWriter.write(receivedPacket.getData(), 0,  receivedPacket.getLength());
        bytesReceived = bytesReceived + receivedPacket.getLength();
        }
    }

    public DatagramPacket receivePacket() throws IOException{

    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    socket.receive(packet);

    return packet;
    }

    public byte[] receiveData() throws IOException{

    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    socket.receive(packet);

    return packet.getData();
    }

    public void send(InetAddress recv, int port,byte[] message)
       throws IOException {

       DatagramPacket packet = new DatagramPacket(message, message.length, recv, port);
       socket.send(packet);
   }    
}

UDPSEND

public UDPSend(InetAddress address, int port) throws IOException{
toPort = port;
toAddress = address;
msg = new byte[512];
buffer = new byte[512];
socket = new DatagramSocket();
socket.connect(toAddress, toPort);
}

@Override
public void sendFile(File theFile) throws IOException{

fileReader = new FileInputStream(theFile);
fileLength = fileReader.available();


send((theFile.getName()+"::"+fileLength).getBytes());

DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
socket.receive(reply);

if (new String(reply.getData(), 0, reply.getLength()).equals("OK"))
    {
    while (currentPos<fileLength){
        bytesRead = fileReader.read(msg);
        send(msg);
        currentPos = currentPos + bytesRead;
    }
    System.out.println("  -- File transfer complete...");
    }
else{System.out.println("Recieved something other than SEND... exiting");}
}

private void send(byte[] message) throws IOException {
DatagramPacket packet = new DatagramPacket(message, message.length);
socket.send(packet);
}   
}
4

1 に答える 1

3

これが少なくとも問題の一部であると思われます。

while (currentPos<fileLength){
    bytesRead = fileReader.read(msg);
    send(msg);
    currentPos = currentPos + bytesRead;
}

1バイトしか読み取られていない場合でも、常にバイト配列全体を送信しています。メソッドsendは送信するバイト数を受け入れる必要があり、bytesReadそれを呼び出すために使用する必要があります。

send(msg, bytesRead);
...
private void send(byte[] message, int length) throws IOException {
    DatagramPacket packet = new DatagramPacket(message, length);
    socket.send(packet);
} 

確かに、16進数で何かが表示されている理由は説明できません...

于 2013-02-18T17:46:36.730 に答える