TCP套接字单发==单接收? [英] TCP socket single send == single receive ?

查看:61
本文介绍了TCP套接字单发==单接收?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发一个使用TCP(套接字API)通过网络发送数据的appilcation。程序将循环字节数组发送到套接字,并在连接的另一端接收该数据,并将其解码为图像序列。



目前我还没有介绍任何用于区分顺序发送图像的自定义协议。高速(大约每秒30次)图像通过插座发送。到目前为止,如果我发送一个字节数组(例如50KB长),我总是收到相同的
字节数组。即使接收器不能像服务器发送的那样快地接收数据,这也可以工作,因此数据流没有任何"漏洞"。及时。




这是一个在接收器上区分独立顺序发送byte []数组的能力吗?


$
我是否保证接收方始终会收到与发送方发送的数据完全相同的数据?没有任何副作用  (将一个数组附加到先前发送的,分成不同的接收缓冲区,...)?
$



发件人:



for(int a = 0; a< 1000; a ++)

{

  &NBSP;   int bitmapNumber = a;

     byte [] array2Send = GetImageBytes(bitmapNumber);
$




     SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();

        socketEventArg.Completed + = OnSendCompleted;

        socketEventArg.SetBuffer(array2Send,0,array2Send.Length);
$


        socket.SendAsync(socketEventArg);
$


}



$
Receiver:



AsyncCallback RecievedDataCallback = new AsyncCallback(OnRecievedData);

New_Socket.BeginReceive(buffer,0,buffer。 Length,SocketFlags.None,RecievedDataCallback,this);
$


private void OnRecievedData(IAsyncResult ar)

{

        SocketCoderAudioClient client =(SocketCoderAudioClient)ar.AsyncState;

        byte [] arrayReceived = client.GetRecievedData(ar);


   &NBSP;&NBSP;&NBSP; //我是否总能收到发送方发送方套接字的相同内容?


       位图bmp = DecodeFromBytes(arrayReceived);

$
}



最好的问候,

Michal







$
$





$ b

解决方案

你看到的仅仅是巧合。 TCP是一种基于流的协议,因此您将接收单个流,并且流被分割的块保持未定义。


在您的情况下,最简单的方法是将数据包的大小放在数据包本身之前,然后接收最大为接收大小的数据。在更复杂的情况下,一些面向消息的中间件可以提供帮助(例如,我们的
MsgConnect 库)。


Hi,
I am developing an appilcation that uses TCP (Socket API) to send data over the network. The program sends in loop byte arrays to socket, and on the other end of the connection this data is received, and decoded into sequence of images.

Currently I have not introduced any custom protocol used to distinguish sequentially send images. With high speed (approximately 30 times per second) images are send through the socket. So far if I send a byte array (e.g 50KB long), I always receive the same byte array.
This also works even if receiver cannot receive data as fast as the server sends, and so the data stream does not have any "holes" in time.

Is this a coincedence that independently sequentially sent byte[] arrays are distinguished on the receiver ?

Do I have a guarantee that receiver will always receive exacty the same data as the sender has sent ? without any side-effect  (appending one array to the previously sent, dividing into different received buffers,..) ?


Sender:

for(int a =0; a< 1000; a++)
{
    int bitmapNumber = a;
    byte[] array2Send = GetImageBytes(bitmapNumber);


    SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
        socketEventArg.Completed += OnSendCompleted;
        socketEventArg.SetBuffer(array2Send, 0, array2Send.Length);

        socket.SendAsync(socketEventArg);

}


Receiver:

AsyncCallback RecievedDataCallback = new AsyncCallback(OnRecievedData);
New_Socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, RecievedDataCallback, this);

private void OnRecievedData(IAsyncResult ar)
{
        SocketCoderAudioClient client = (SocketCoderAudioClient)ar.AsyncState;
        byte[] arrayReceived = client.GetRecievedData(ar);

       //shall I always receive the same thing that has been sent to the socket on the sender side ?
        Bitmap bmp = DecodeFromBytes(arrayReceived);

}

Best regards,
Michal









解决方案

What you see is mere coincidence. TCP is a stream-based protocol so you will be receiving a single stream, and in what chunks the stream is split remains undefined.

In your case the simplest approach is to put the size of the packet before the packet itself, then receive the data up to received size. In more complex cases some message-oriented middleware can be of help (eg. our MsgConnect library).


这篇关于TCP套接字单发==单接收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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