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