TCPListener / TCPClient 服务器-客户端写入/读取数据

发布于 2024-12-21 15:58:49 字数 784 浏览 1 评论 0原文

在这里,我正在解决有关服务器和客户端如何在计算机上工作的理论问题。我知道所有 NET 进程,但我缺少一些涉及代码的内容。我无法找到与此相关的内容。

我在 Visual C# 2008 中进行编码,我在 2 个不同的项目中使用常规 TCPClient / TCPListener:

Project1(客户端)

Project2(服务器)

我的问题可能很简单:

1->关于服务器如何接收数据,事件处理程序是可能的吗? 在我的第一个服务器代码中,我曾经做过这个循环:

while (true)
{

if (NetworkStream.DataAvailable)
    {
        //stuff
    }

Thread.Sleep(200);
}

我遇到这是一种控制来自服务器的传入数据的糟糕方法。但服务器始终准备好接收数据。

我的问题:有什么类似...? ->

AcceptTcpClient();

我想要一个处理程序,等待发生某些事情,在本例中是一个特定的套接字数据接收。

2-> 通用网络 I/O 方法。

问题是(除了我是菜鸟之外)是如何处理多个数据写入。 如果我使用字节数组发送大量数据,则发送更多数据可能会中断。所有数据都已连接,接收时出现错误。我想处理发送和接收的多个写入。

这可能吗?

Here I am troubleshooting a theoretical problem about HOW servers and clients are working on machines. I know all NET Processes, but I am missing something referring to code. I was unable to find something related about this.

I code in Visual C# 2008, i use regular TCPClient / TCPListener with 2 different projects:

Project1 (Client)

Project2 (Server)

My issues are maybe so simple:

1-> About how server receives data, event handlers are possible?
In my first server codes i used to make this loop:

while (true)
{

if (NetworkStream.DataAvailable)
    {
        //stuff
    }

Thread.Sleep(200);
}

I encounter this as a crap way to control the incoming data from a server. BUT server is always ready to receive data.

My question: There is anything like...? ->

AcceptTcpClient();

I want a handler that waits until something happen, in this case a specific socket data receiving.

2-> General networking I/O methods.

The problem is (beside I'm a noob) is how to handle multiple data writing.
If I use to send a lot of data in a byte array, the sending can break if I send more data. All data got joined and errors occurs when receiving. I want to handle multiple writes to send and receive.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

波浪屿的海角声 2024-12-28 15:58:49

关于服务器如何接收数据,事件处理程序是可能的吗?

如果你想编写面向回调的服务器代码,你可以找到MSDN的 Asynchronous Server Socket示例正是您正在寻找的内容。

...如果我发送更多数据,发送可能会中断。所有数据都已连接,接收时出现错误。

这就是 TCP 的本质。标准化互联网协议分为以下几类:

             block oriented    stream oriented
reliable          SCTP              TCP
unreliable         UDP              ---

如果您确实想要发送数据块,则可以使用 SCTP,但请注意,许多防火墙会丢弃 SCTP 数据包,因为它们不“常见”。我不知道是否可以在开放的互联网上可靠地路由 SCTP 数据包。

您可以将自己的内容包装成带有自己的标头的数据块,或者向系统添加其他“同步”机制。考虑一个 HTTP 服务器:它必须等到读取整个请求,例如:

GET /index.html HTTP/1.1␍␊
Host: www.example.com␍␊
␍␊

直到服务器当看到 CRLFCRLF 序列时,它必须将部分读取的数据保留在缓冲区中。这些字节可能会在十几个或更多数据包中一次出现一个。或者,如果客户端在单个流中发送多个请求,则单个数据包中可能会出现十几个请求。

你只需要处理这个。

About how server receives data, event handlers are possible?

If you want to write call-back oriented server code, you may find MSDN's Asynchronous Server Socket Example exactly what you're looking for.

... the sending can break if I send more data. All data got joined and errors occurs when receiving.

That is the nature of TCP. The standardized Internet protocols fall into a few categories:

             block oriented    stream oriented
reliable          SCTP              TCP
unreliable         UDP              ---

If you really want to send blocks of data, you can use SCTP, but be aware that many firewalls DROP SCTP packets because they aren't "usual". I don't know if you can reliably route SCTP packets across the open Internet.

You can wrap your own content into blocks of data with your own headers or add other "synchronization" mechanisms to your system. Consider an HTTP server: it must wait until it reads an entire request like:

GET /index.html HTTP/1.1␍␊
Host: www.example.com␍␊
␍␊

Until the server sees the CRLFCRLF sequence, it must keep the partially-read data in a buffer. The bytes might come in one at a time in a dozen or more packets. Or, if the client is sending multiple requests in a single stream, a dozen requests might come in a single packet.

You just have to handle this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文