Reading a C # Byte Array in Java

I am transferring a file from a C # client to a Java server using TCP sockets. On a C # client, I convert the file to an array of bytes for transmission and send it using NetworkStream.

On a Java server, I use the following code to convert the resulting byte array to 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 above code works if the received byte array comes from a client programmed using Java, but for C # clients the code hangs in a do-while loop using the bytesRead = input.read (...) method.

Does anyone know why the code is currently hanging for the C # client, but not for the Java client? According to the output from the println message, an InputStream is definitely received and is read once when the bytesRead variable is initialized, but not during the do-while loop.

.

,

Midavi.

+5
3

sbyte # byte.

, do/while while. input.read, , read .

+1

: http://docs.oracle.com/javase/1.4.2/docs/api/java/io/InputStream.html

 int bytesRead = input.read(byteArrayJAR, 0, byteArrayJAR.length);

, , byteArrayJAR ?

EDIT:

... Android <= > # Server Application.

String data = input.readLine(); (java) _sw.WriteLine("Testdata"); (#)

0

Since you cannot know the amount of data sent in advance in the general case, it would be better to get the data in pieces and ignore the offset to your large array. Your as-is code does not handle this case very well if the data is larger than your array, or if it is smaller than the array.

A simpler case is to remove the offset currentByte:

InputStream input = socket.getInputStream();
BufferedOutputStream output = new BufferedOutputStream(
    new FileOutputStream("test.exe"));

byte[] bytes = new byte[2048];
int bytesRead;
do {
    bytesRead = input.read(bytes, 0, bytes.length);
    System.out.println("size: " + bytes.length + " read(): " + bytesRead);

    if (bytesRead > 0) {
        output.write(bytes, 0, bytesRead);
    }
} while (bytesRead > -1);

output.close();
socket.close();
input.close();

And I used the following client-side C # code:

if (!args.Any())
{
    Console.Error.WriteLine("Usage: send.exe <executable>");
    Environment.Exit(-1);
}

using (var client = new TcpClient("localhost", 10101))
using (var file = File.Open(args.First(), FileMode.Open, FileAccess.Read))
{
    file.CopyTo(client.GetStream());
}

Client side results:

C:\temp>csc send.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.


C:\temp>send send.exe

C:\temp>

Server side results:

C:\temp>javac.exe Server.java

C:\temp>java Server
size: 2048 read(): 2048
size: 2048 read(): 2048
size: 2048 read(): 512
size: 2048 read(): -1

C:\temp>test.exe
Usage: send.exe <executable>
0
source

All Articles