I'm transferring a file from a C# client to a Java server using TCP Sockets. On the C# client I convert the file to a byte array for transmission and send it using a NetworkStream.
On the Java server I use the following code to convert the received byte array back into a file;
public void run() {
try {
byte[] byteArrayJAR = new byte[filesize];
InputStream input = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(
"Controller " + controllerNumber + ".jar");
BufferedOutputStream output = new BufferedOutputStream(fos);
int bytesRead = input.read(byteArrayJAR, 0, byteArrayJAR.length);
int currentByte = bytesRead;
System.out.println("BytesRead = " + bytesRead);
do {
bytesRead = input.read(
byteArrayJAR,
currentByte,
(byteArrayJAR.length - currentByte));
if (bytesRead >= 0) {
currentByte += bytesRead;
}
}
while (bytesRead > -1);
output.write(byteArrayJAR, 0, currentByte);
output.flush();
output.close();
socket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
The code above works if the received byte array comes from a client programmed with Java but for C# clients the code hangs in the do-while loop at the bytesRead = input.read(...) method.
Does anybody know why the code is hanging at this point for a C# client but not a Java client? According to the output from the println message data is definately being received by the InputStream and is read once at the bytesRead variable initialization, but not during the do-while loop.
Any solutions or suggestions for overcoming this problem would be welcome.
Regards,
Midavi.
Flush()?