套接字缓冲区接收到的数据 [英] Socket buffers the data it receives

查看:125
本文介绍了套接字缓冲区接收到的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端.NET应用程序和服务器.NET应用程序,通过接口进行连接。

I have a client .NET application and a server .NET application, connected through sockets.

客户端发送的每500毫秒20左右字符的字符串。

The client sends a string of 20 or so characters every 500 milliseconds.

在我的本地开发机器,这个完美的作品,但一旦在客户端和服务器上的两个不同的服务器,服务器不会立即当它发送接收的字符串。客户端仍然发送完美,我使用Wireshark证实了这一点。我还证实,该服务器的确实收到每500毫秒的字符串。

On my local development machine, this works perfectly, but once the client and the server are on two different servers, the server is not receiving the string immediately when it's sent. The client still sends perfectly, I've confirmed this with Wireshark. I have also confirmed that the the server does receive the strings every 500 milliseconds.

的问题是,我的服务器的应用正在等待消息,实际上只收到每20秒左右的消息 - 再收到来自这20秒内的含量

The problem is that my server application that is waiting for the message only actually receives the message every 20 seconds or so - and then it receives all the content from those 20 seconds.

我使用异步套接字出于某种原因,回调只是不调用超过每20秒一次。

I use asynchronous sockets and for some reason the callback is just not invoked more than once every 20 seconds.

AcceptCallback 它建立连接和呼叫 BeginReceive

handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);

这工作我的本地机器上,但我的生产服务器上的ReadCallback不会立即发生。

This works on my local machine, but on my production server the ReadCallback doesn't happen immediately.

的缓冲区大小设置为1024。我也试图将其设置为10,这使得有多少数据会从插座中一次读取一次的ReadCallback是有区别的调用,但在这里,这不是真正的问题。一旦调用ReadCallback,其余工作正常。

The BufferSize is set to 1024. I also tried setting it to 10. It makes a difference in how much data it will read from the socket at one time once the ReadCallback is invoked, but that's not really the problem here. Once it invokes ReadCallback, the rest works fine.

我使用的是微软的异步服务器套接字示例所以你可以看到有什么我ReadCallback方法是什么样子。

I'm using Microsofts Asynchronous Server Socket Example so you can see there what my ReadCallback method looks like.

我可以马上得到BeginReceive回调当数据到达服务器

-

更新

这已经解决了。这是因为该服务器有一个单一的处理器和单核。加入另一个核心后,问题马上解决。 ReadCallback当呼叫经过到服务器现在马上打电话。

This has been solved. It was because the server had a a single processor and single core. After adding another core, the problem was instantly solved. ReadCallback is now called immediately when the call goes through to the server.

三江源所有您的建议!

推荐答案

通过OP的要求,复制我的的评论/回答在这里。

By the request of OP, duplicating my "comment/answer" here.

我的猜测是,问题出现了,因为单核机器上的线程调度。这是一个老问题,在超线程/多核心处理器的现代几乎绝迹。当线程在程序的执行过程中被产生,它需要预定时间来运行。

My guess was, the problem appeared because of thread scheduling on a single-core machine. This is an old problem, almost extinct in the modern age of hyper-threading/multi-core processors. When a thread is spawned in the course of execution of the program, it needs scheduled time to run.

在单核机中,如果一个线程继续没有明确把控制权交给OS调度器来执行(通过等待互斥/信号或致电休眠),执行任何其他线程(在同一进程中的和具有较低优先级的)可以被无限期地由调度推迟。因此,在所描述的情况,异步网络线程(最有可能的)只饥饿执行时间 - 正从时间仅件时间

On a single-core machine, if one thread continues to execute without explicitly passing control to OS scheduler (by waiting for mutex/signal or by calling Sleep), the execution of any other thread (in the same process and with lower priority) may be postponed indefinitely by the scheduler. Hence, in the case described, the asynchronous network thread was (most likely) just starved for execution time - getting only pieces from time to time.

添加第二CPU /核心显然,固定,通过提供一个并行调度环境

Adding second CPU/core, obviously, fixed that by providing a parallel scheduling environment.

这篇关于套接字缓冲区接收到的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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