TCPListener / TCPClient 服务器-客户端写入/读取数据
在这里,我正在解决有关服务器和客户端如何在计算机上工作的理论问题。我知道所有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想编写面向回调的服务器代码,你可以找到MSDN的 Asynchronous Server Socket示例正是您正在寻找的内容。
这就是 TCP 的本质。标准化互联网协议分为以下几类:
如果您确实想要发送数据块,则可以使用 SCTP,但请注意,许多防火墙会丢弃 SCTP 数据包,因为它们不“常见”。我不知道是否可以在开放的互联网上可靠地路由 SCTP 数据包。
您可以将自己的内容包装成带有自己的标头的数据块,或者向系统添加其他“同步”机制。考虑一个 HTTP 服务器:它必须等到读取整个请求,例如:
直到服务器当看到 CRLFCRLF 序列时,它必须将部分读取的数据保留在缓冲区中。这些字节可能会在十几个或更多数据包中一次出现一个。或者,如果客户端在单个流中发送多个请求,则单个数据包中可能会出现十几个请求。
你只需要处理这个。
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.
That is the nature of TCP. The standardized Internet protocols fall into a few categories:
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:
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.