图像数据流通过套接字发送 - 服务器死锁问题 [英] Image Stream sent over Socket - Server Deadlock Issue

查看:129
本文介绍了图像数据流通过套接字发送 - 服务器死锁问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个Android应用程序,拍照与SP摄像头,并将其发送通过套接字服务器。

I've implemented an Android application that takes a picture with the SP camera and sends it over a socket to the server.

我用下面(JAVA)code读取本地存储的图像文件,并在连续的块在插槽发送:

I'm using the following (JAVA) code to read the image file stored locally and send it in successive chunks over the socket:

FileInputStream fileInputStream = new FileInputStream( "my_image_file_path" );

int nRead;
byte[] data = new byte[16384];

try {
    while( (nRead = fileInputStream.read(data, 0, data.length)) != -1 ){
        networkOutputStream.write( data, 0, nRead );
    }

} catch( IOException e ){
    e.printStackTrace();
}
fileInputStream.close();

和以下(C / C ++)code读取它并将其存储在服务器上:

And the following (C/C++) code to read it and store it on the server:

char newbuffer[MAX_BUF_SIZE];
int checkOperation;

ofstream outfile( "image_file_path".c_str(), ofstream::binary );

do{
    checkOperation = read( clientSocketDescriptor, newbuffer, sizeof(newbuffer) );

    if( checkOperation < 0 ){

        cout << "Error in recv() function, received bytes = " << checkOperation << endl;
        exit(1);
    }else if (checkOperation != 0 ){

       /*
        * some data was read
        */
       cout << endl << "READ Bytes: " << checkOperation << endl;
       outfile.write( newbuffer, checkOperation );

       /*
        * emptying buffer for new incoming data
        */
       for(int i = 0; i < sizeof(newbuffer); i++){
          newbuffer[i] = 0;
       }
    }
}while( checkOperation =! 0 );

outfile.close();

Android客户端应用程序似乎正确地写入所有字节插座,并成功地从,而循环退出。

但服务器code卡在其,而循环的最后一次迭代,并且不能够继续执行。

However the server code gets stuck in the last iteration of its while loop, and isn't able to continue execution.


  • 为什么不能在服务器阅读 EOF

  • 是我的code发送的图像或阅读它不正确?

在提前为任何形式的帮助,谢谢,因为我真的卡住了!

Thanks in advance for any kind of help, since I'm really stuck!

推荐答案

你没有关闭 networkOutputStream ,所以C ++ code不认识你再完成。

You're not closing the networkOutputStream, so the C++ code doesn't know you're finished.

你要么需要关闭输出流 - 这是唯一可行的,如果你不需要送其上的任何更多的数据,很明显 - 或者你需要在协议要么一些元数据表明你开始写之前如何更多的数据有,或事后包括一些为完成的指示。一般来说,我preFER长度preFIX - 它比不必担心逃跑,如果完成指标数据出现自然简单得多

Either you need to close the output stream - which is only viable if you don't need to send any more data on it, obviously - or you need to include some metadata in the protocol to either indicate before you start writing how much more data there is, or include something afterwards as a "done" indicator. Generally I prefer a length prefix - it's much simpler than having to worry about escaping if the "done" indicator appears naturally in data.

一个中途宿舍是重复说:这里的数据长X的一大块,然后是我做的,没有更多的块的消息(这可能是这里的数据长度为0的一大块为例)。这样,你不需要知道总长度事先。

A half-way house is to repeatedly say "here's a chunk of data length X" and then an "I'm done, no more chunks" message (which could be "here's a chunk of data length 0" for example). This way you don't need to know the total length beforehand.

这篇关于图像数据流通过套接字发送 - 服务器死锁问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆