Java DatagramPacket收到的文本文件以HEX写出,并带有0x00数据填充

时间:2013-02-18 17:14:34

标签: java udp

对于家庭作业:我的任务是编写带有确认的UDP文件传输程序......基本上模拟TCP。

我决定分阶段完成这项任务,而且很早就收到了接收文件的问题。 我发送一个带有“test file”字符的小文本文件,另一端创建的文件包含匹配的ascii HEX值,填充为0x00。

我希望收到的文件与发送的文件完全相同。

有人可以指出问题吗?

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);
}   
}

1 个答案:

答案 0 :(得分:3)

我怀疑这至少是问题的一部分:

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

即使只读取了一个字节,您也总是发送整个字节数组。您的send方法应该接受要发送的字节数,您应该使用bytesRead来调用它:

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

不可否认,这无法解释为什么你会看到十六进制中的任何内容......