简体   繁体   English

Android的检索的InputStream字节

[英]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: 我正在尝试检索已在c#中编程的服务器传输的字节,如下所示:

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. 当我在C#中编写客户端并在PC中运行它时,以上代码可以正常工作。 However I want to retrieve the bytes transferred by the server in the android device. 但是我想检索由服务器在android设备中传输的字节。

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'. android已成功连接到服务器,但无法完成其工作,基本上它不会通过“波纹管代码中的while循环”。 I think there is something wrong with the byte length because it's never get to '-1'. 我认为字节长度有问题,因为它永远不会达到“ -1”。

Android java code (Client): Android Java代码(客户端):

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. 我不了解您的Java代码。 The read(byte[]) method you are using writes 1024 bytes (or less) in the buffer again and again. 您使用的read(byte[])方法一次又一次地在缓冲区中写入1024个字节(或更少)。 You are overwriting your previous buffer content. 您正在覆盖以前的缓冲区内容。 You probably would like to write the downloaded data to somewhere, like an OutputStream . 您可能希望将下载的数据写入某个地方,例如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. 在服务器关闭连接之前,客户端的read()方法将永远不会返回-1。 You're not doing that so it never happens. 您没有这样做,所以它永远不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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