TCP客户端的问题 [英] TCP Client Side Issue

查看:159
本文介绍了TCP客户端的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临在C#TCP客户端应用程序返回一个大问题。正常情况下,它工作正常,但在某些情况下,服务器发送给特定的客户端同时响应,所以这种情况下,所有的同时响应被接收为一个消息客户端side.So这种情况下,客户端无法识别个人信息。所以,这种情况下,我的客户端应用程序无法处理特定的活动。是否有可能在客户端阅读为个人信息?因此,对于解决这个问题,我有一些选择,但我不知道这是否是正确的方法。

I am facing a big issue in TCP client application return in c#. Normal case it's working fine but in some case server send simultaneous response to a particular client, so this case all the simultaneous response are received as a single message in client side.So this case client failed to identify individual message. So such a case my client application failed to handle a particular activity. Is it possible to read as individual message in client side ?. So for solving this issue, I have some choices but i don't know if it is correct way.

1 - 使用客户端的固定长度的缓冲区(但我的消息是可变长度)。

1- Using a fixed length buffer in client side(But my message are variable length).

2 - 添加分隔符来每个响应消息。

2- Add a delimiter to each response message.

如果有任何人知道为解决此类问题的解决方案,请帮助我。

If any one know the solution for resolving such problem please help me.

推荐答案

TCP是基于你的数据从客户端到服务器的流流。你需要反序列化是什么你从流预期,如果不能建立一个保证发送的每条消息的应用协议分别是(这通常不是一个明智的主意与TCP)。

TCP is stream based, you get a stream of data from a client to server. You need to "deserialize" what you're expecting from the stream, if you can't build an application protocol that ensures each message sent is separately (which usually isn't a wise idea anyway with TCP).

所以,你需要与每个消息的东西,可以让你区分数据是什么派。这通常与标签或类型的数据在每个消息的开始进行。有时候,一个字节会做(取决于你有多少个不同的消息类型)。然后,从流读取该字节,并决定下一步反序列化的东西。例如如果你想发送一个字符串,则可能连载1,那么字符串。如果你想发送的日期/时间,你可能会序列化'2',那么的DateTime 。在服务器上,如果这两个对象被罚大约在同一时间,你可以简单地反序列化字节,如果是'1',反序列化字符串,然后继续反序列化:获得下一个字节,如果它是一个'2',反序列化的DateTime

So, you need to send with each message something that allows you to differentiate what the data is. This is often done with tag or type data at the start of each message. Sometimes a byte will do (depending on how many different message types you have). You then read that byte from the stream and decide what to deserialize next. e.g. if you want to send a string, you might serialize '1', then the string. If you want to send a date/time, you might serialize '2' then the DateTime. On the server, if both of those objects got sent about the same time, you could simply deserialize the byte, and if it was '1', deserialize a string, then continue deserializing: get the next byte, and if it's a '2', deserialize a DateTime.

要记住,TCP是基于流的协议,而不是基于消息的协议是非常重要的。在应用层中实现基于信息的需求 - 例如。 。像上述

It's important to remember that TCP is a stream-based protocol, not a message-based protocol. Message-based needs to be implemented at the application level--e.g. something like the above.

和通过序列化和反序列化我的意思是典型的内置串行器和解串器中的序列化对象在.NET文档中获得。同样将保持JSON序列化真正纳入像JSON.NET或内置的 DataContractJsonSerializer 的JavaScriptSerializer 库。

And by "serialize" and "deserialize" I mean the typical built-in serializers and deserializers as described in Serializing Objects in the .NET documentation. The same would hold true for JSON serializers included in libraries like JSON.NET or the built-in DataContractJsonSerializer and JavaScriptSerializer.

这篇关于TCP客户端的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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