简体   繁体   中英

Android retrieve bytes from inputstream

I am trying to retrieve the byte that transferred by the server that has been programmed in c# as follow:

static void Main(String[] args){
    Socket sListen = new Socket(AddressFamily.InterNetwork, SocketType.Stream,     ProtocolType.Tcp);
    IPAddress IP = IPAddress.Parse("10.0.0.92");
    IPEndPoint IPE = new IPEndPoint(IP, 4321);
    sListen.Bind(IPE);
    Console.WriteLine("Service is listening ...");
    sListen.Listen(2);
    while (true){
        Socket clientSocket;
        try{
            clientSocket = sListen.Accept();
        }
        catch{
            throw;
        }
        byte[] buffer = ReadImageFile("path to image");
        clientSocket.Send(buffer, buffer.Length, SocketFlags.None);
        Console.WriteLine("Send success!");
    }
}

private static byte[] ReadImageFile(String img){
    FileInfo fileinfo = new FileInfo(img);
    byte[] buf = new byte[fileinfo.Length];
    FileStream fs = new FileStream(img, FileMode.Open, FileAccess.Read);
    fs.Read(buf, 0, buf.Length);
    fs.Close();
    //fileInfo.Delete ();
    GC.ReRegisterForFinalize(fileinfo);
    GC.ReRegisterForFinalize(fs);
    return buf;
}

Above codes works fine when I write a client in the c# and run it in the pc. However I want to retrieve the bytes transferred by the server in the android device.

The android connected to the server successfully but it will not finish its job, basically it will not pass the 'while loop in the bellow code'. I think there is something wrong with the byte length because it's never get to '-1'.

Android java code (Client):

Socket socket = new Socket("ip", 4321);
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1024];
int read = is.read(buffer);
while(read != -1){
    read = is.read(buffer);
}

is.close();
socket.close();

I do appreciate any help in advance, Thanks,

I don't get the point of your Java code. The read(byte[]) method you are using writes 1024 bytes (or less) in the buffer again and again. You are overwriting your previous buffer content. You probably would like to write the downloaded data to somewhere, like an OutputStream . Example:

    Socket socket = new Socket("ip", 4321);
    InputStream is = socket.getInputStream();
    OutputStream os = ...; // Where to save data, for example, new ByteArrayOutputStream();

    copyStreams(is, os);

    is.close();
    socket.close();


    public static void copyStreams(InputStream is, OutputStream os) throws IOException {
        byte[] buffer = new byte[1024];
        int numBytesRead;

        for (;;) {
            bytesRead = is.read(buffer);
            if (bytesRead <= 0)
                break;
            os.write(buffer, 0, bytesRead);
        }
    }

The client read() method will never return -1 until the server closes the connection. You're not doing that so it never happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM