需要C#TCP套接字帮助 [英] Need C# TCP socket help

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

问题描述

以下是这种情况:

我正在与内部公司Market Data Server进行通信。我正在使用AddressFamily.Internetwork,SocketType.Stream和ProtocolType.TCP创建一个客户端套接字。我正在使用异步套接字I / O向该服务器发出多个请求。一切都运行正常,但有一个场景,我不知道如何处理。

首先,这里是一个描述数据的外观,一旦收到我的字节数组:

有一个4字节的头,包含消息长度,然后是消息。一旦转换为字符串,该消息就有一个标题部分,其中包含用户提供的RequestID(来自发送到服务器的请求)一个LastMessage标志,用于通知客户端是否没有更多数据要接收该特定响应然后消息内容。

服务器一次可以发回的最大数据量是8K数据。我发送的缓冲区是32K。

当响应中的字节返回时,字节数组中可能包含许多服务器消息。它可以从4字节头开始,或者如果字节在消息边界,则接收的字节可以在消息的中间开始。

当我向服务器发出请求时,我使用RequestID作为键在Dictionary中保存StringBuilder对象。当数据返回时,我将字节转换为字符串,然后解析字符串并提取其中的每条消息(以| SOH = |标志开头,以| EOM = |标志结束。一旦我提取消息,我得到RequestID,然后使用它来访问字典中的相应StringBuilder,并将新到达的消息附加到StringBuilder。

这里可能存在问题。可能存在这样的情况:从服务器接收字节(由于带宽问题等)并且一旦转换为字符串就没有RequestID,因为数据块超过了RequestID的点。我不知道在这种情况下这是新收到的数据量应该被路由到。潜在地,我可以有几个未完成的消息等待附加到字典中,如果我没有RequestID,我就无法将它们路由到正确的位置,因此我的数据将被分段。

我该如何处理这个问题 问题?有没有办法检查IP数据包的某些识别信息?是否有其他方法可以知道哪个字节数组属于哪个消息?

解决方案

TCP / IP提供有序的字节流。这就是全部。

所以,在接收时,你需要有一个缓冲区用于不完整的响应,并附加到它,直到你有一个完整的(部分)响应(记住下一个响应的开始可能存在)以及)。如果我正确理解你的协议,这意味着你应该存储接收缓冲区,直到得到| EOM = |,然后将该缓冲区附加到StringBuilder字典。


-Steve


Here is the situation:

I am communicating with an internal company Market Data Server. I am creating a client socket using AddressFamily.Internetwork, SocketType.Stream and ProtocolType.TCP. I am firing off multiple requests to the this server using asynchronous socket I/O. Everything is working fine, however there is one scenario that I am not sure how to handle.

First, here is a description of how the data looks, once received in my byte array:

There is a 4 byte header that contains the message length and then the message. Once converted to a string, the message has a header section that contains a user supplied RequestID (from the request that was sent to the server) a LastMessage Flag to notify the client whether there is no more data to receive for that paricular response and then the message content.

The maximum amount of data that the server can send back at one time is 8K of data. The buffer I am sending in is 32K.

When the bytes come back in the response, there can be many server messages contained in the byte array. It can start at the 4 byte header, or if the bytes are at a message boundary, the bytes received can start in the middle of a message.

When I fire off my requests to the server, I am holding a StringBuilder object in a Dictionary using the RequestID as the key. When the data gets returned, I am converting the bytes into a string and then parsing the string and extracting each message within it (starting with a |SOH=| flag and ending with a |EOM=| flag. Once I extract a message, I get the RequestID and then use that to access the appropriate StringBuilder within the dictionary and append the newly arrived message to the StringBuilder.

Here is where a problem can be. There can potentially be a situation where only a few bytes are received from the server (because of bandwith issues or the like) and once converted to a string there is no RequestID because the chunk of data is past the point of the RequestID. I don't know in this scenario where this newly received amount of data should be routed to. Potentially, I can have several outstanding messages waiting to be appended to in the Dictionary and if I don't have a RequestID, I cannot route them to the proper place and therfore my data would be fragmented.

How can I handle this issue? Is there some way to inspect the IP packet for some sort of identifying information? Is there some other way to know which byte array belongs to which message?

解决方案

TCP/IP provides an ordered stream of bytes. That's all.

So, when receiving, you need to have a buffer for incomplete responses, and append to it until you have a complete (partial) response (keeping in mind the beginning of the next response may be present as well). If I understand your protocol correctly, this means that you should store the receive buffer until you get the |EOM=|, then append that buffer to the StringBuilder dictionary.

       -Steve


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

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