发送大量数据throught TCP套接字的 [英] sending a large amount of data throught TCP socket

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

问题描述

这是我张贴在这个论坛的第一个问题,我在C#中的世界是初学者,所以这是一种令人兴奋的对我,但我面临着一些问题,通过套接字发送大量数据,所以这是关于我的问题的详细信息:

This is my first question posted on this forum, and I'm a beginner in c# world , so this is kind of exciting for me, but I'm facing some issues with sending a large amount of data through sockets so this is more details about my problem:

我通过TCP套接字发送的5沫二进制图像,在接收部分,我保存的结果(接收的数据),并只得到1.5沫==>数据已被丢失(Ⅰ比较原始和生成的文件,它显示了我错过的部分) 这是code我用:

I'm sending a binary image of 5 Mo through a TCP socket, at the receiving part I'm saving the result(data received ) and getting only 1.5 Mo ==> data has been lost (I compared the original and the resulting file and it showed me the missed parts) this is the code I use:

private void senduimage_Click(object sender, EventArgs e)
{
    if (!user.clientSocket_NewSocket.Connected)
    {
        Socket clientSocket_NewSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        user.clientSocket_NewSocket = clientSocket_NewSocket;
        System.IAsyncResult _NewSocket = user.clientSocket_NewSocket.BeginConnect(ip_address, NewSocket.Transceiver_TCP_Port, null, null);
        bool successNewSocket = _NewSocket.AsyncWaitHandle.WaitOne(2000, true);
     }
     byte[] outStream = System.Text.Encoding.ASCII.GetBytes(Uimage_Data);
     user.clientSocket_NewSocket.Send(outStream);
 }

在他们说来将数据分成块的论坛,这是一个解决方案,如果是的话我该怎么做,我做过尝试,但它没有工作!

In forums they say to divide data into chunks, is this a solution, if so how can I do this, I've tried but it didn't work!

推荐答案

有很多不同的解决方案,但分块的通常是一个很好的解决方案,你可以做到这一点盲目的,你保持填补临时缓冲区,然后将其放入一些有状态的缓冲区,直到你打一些任意的标记或缓冲区不完全填满,或者你可以坚持某种每个TCP合同的的消息的(一个消息是整体数据才能收到)。

There are lots of different solutions but chunking is usually a good solution, you can either do this blindly where you keep filling your temporary buffer and then putting it into some stateful buffer until you hit some arbitrary token or the buffer is not completely full, or you can adhere to some sort of contract per tcp message (a message being the overall data to recieve).

如果你看一下在做某种形式的合同,然后做类似消息的前N个字节是描述符,它可以使大或你想要小,但你的临时缓冲区将只读取本尺寸从流面前。

If you were to look at doing some sort of contract then do something like the first N bytes of a message is the descriptor, which you could make as big or as small as you want, but your temp buffer will ONLY read this size up front from the stream.

一个典型的头可能是这样的:

A typical header could be something like:

public struct StreamHeader // 5 bytes
{
   public byte MessageType {get;set;} // 1 byte
   public int MessageSize {get;set;}  // 4 bytes
}

所以,你会读到的话,如果它足够小,配备完整的邮件大小的临时缓冲区,并在阅读这一切,如果你认为它太大块成部分,并继续读,直到你收到的总字节数匹配头结构的MessageSize部分。

So you would read that in then if its small enough allocate the full message size to the temp buffer and read it all in, or if you deem it too big chunk it into sections and keep reading until the total bytes you have received match the MessageSize portion of your header structure.

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

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