通过TCP连接发送包? [英] Sending packages via a TCP connection?

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

问题描述

Hello Codeproject,



我目前有一个TCP服务器和客户端,有线程。



我面临的问题是TCP只允许字节发送和接收。

但这不是问题,在事实上,这是它的美丽。但是,不可能确定客户端和服务器在没有限制的情况下发送多少字节,

例如''4096''。那么,如果我想发送课程怎么办,例如:



  public   class 电子邮件
{
字符串邮件{获得; set ;}
string Recept { get < /跨度>; set ; }
string 已发送{获取; set ; }

public 电子邮件()
{

}

< span class =code-keyword> public
SetEmail( string Receptant, string 发​​件人, string 消息)
{
Mail = Message;
Recept = Receptant;
已发送=发件人;
}
}





基本上,如果我使用XML Serializer将此类序列化为StringBuilder,拿了MemoryStream并用它做了一个字节[],我可以发送一个全班,对吧?一点都不。由于Mail,Recept和Sent是变量,它们可以并且将被更改。所以假设我的消息长度超过4000个字符,我就会遇到问题。所以我的问题是,如果可以确定传入的字节数组有多大,我该怎么做?或者,是否有人有任何其他建议?

解决方案

TCP本身只是一个传输层。您的问题是,您忘记在其上方实现图层:应用程序层(请参阅: http://en.wikipedia.org/ wiki / Internet_protocol_suite [ ^ ])。

更确切地说,您正确地体验过它,您不能仅在您可以在TCP通信中写入/读取的流中继,您必须实现协议,您的应用程序协议在它上面。所有互联网应用程序都使用某种二进制或基于文本的协议,有些是复杂的,有些则更简单。通常,这些协议定义消息格式。在你的情况下,一个简单的可能是这样的(发送者,B接收者):

A - > B:我会给你发一条4541字节的消息

B - > A:好的,我准备好了

A - > B:[发送那些4541字节]

B - > A:我知道了

(当然,这是英语,但你可以使用序列化为字节流的预定义结构 - 实际内容除外。)



以下文章将为您提供c#中可能性的亮点。

C#.NET中的完整TCP服务器/客户端通信和RMI框架 - 实现 [ ^ ]

高性能TCP / IP通信库的设计与实现 [ ^

Hello Codeproject,

I currently have a TCP Server and Client, threaded that is.

The problem I am facing is that TCP only allows bytes to send and receive.
But it''s not a problem, in fact, it''s the beauty in it. But, it''s impossible to
determine client and server sided how many bytes you send without having a limit,
for instance ''4096''. So, what if I would want to sent a class, for instance:

public class Email
{
  string Mail { get; set;}
  string Recept { get; set; }
  string Sent { get; set; }
  
  public Email()
  {
    
  }

  public SetEmail(string Receptant, string Sender, string Message)
  {
       Mail = Message;
       Recept = Receptant;
       Sent = Sender;
   }
}



Basically, if I would use XML Serializer to serialize this class into a StringBuilder, than took the MemoryStream and made a byte[] out of it, it was possible for me to send a whole class, right? No, not at all. Since Mail, Recept, and Sent, are variabels, they can and will be changed. So assuming my message was a bit longer than 4000 characters, I would already have a problem. So my question is, if it''s possible to determine how big an incoming byte array is, how would I do that? Or, does anyone have any other suggestions?

解决方案

TCP by itself is only a transport layer. Your problem is, that you forgot to implement the layer above it: the application layer (see: http://en.wikipedia.org/wiki/Internet_protocol_suite[^]).
More precisely, you experienced it correctly, you can not relay only on the stream you can write/read in a TCP communication, you have to implement a protocol, your application protocol above it. All internet applications use some sort of binary or text based protocol, some are complicated, some are more simple. In general these protocols define message formats. In your case a simple one could be like this (A sender, B receiver):
A -> B : I will send you a message of 4541 bytes
B -> A : Ok, I am ready
A -> B : [send those 4541 bytes]
B -> A : I got it
(of course, this is "english", but you could use predefined structures serialized to byte stream - except for the actual content.)

Following articles will give you the highlights of your possibilities in c#.
A Complete TCP Server/Client Communication and RMI Framework in C# .NET - Implementation[^]
Design and Implementation of a High-performance TCP/IP Communications Library[^]


这篇关于通过TCP连接发送包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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