我们有一个用 C# 编写的异步套接字服务器。(在 Windows Web Server 2008 上运行)
它可以完美地工作,直到由于未知原因停止接受新连接为止。
我们平均有大约 200 个并发连接,但是我们会记录创建的连接和删除的连接的计数。这些数字最高可达 10,000,最低可达 1000,然后就停止了!它有时可以运行大约 8 小时,然后才会停止,或者可以运行大约半小时,目前它运行大约一个小时,然后我们让另一个应用程序在无法连接时自动将其备份(不完全是这样)理想的)。
看起来我们并没有耗尽套接字,因为我们正确关闭了它们,我们还记录了所有错误,并且在停止之前没有任何反应。
我们可以弄清楚这一点。有人知道可能会发生什么吗?
我可以粘贴代码,但它通常只是您在各处看到的相同的旧异步开始接受/发送代码。
我们一直坚持在SocketAsyncEventArgs对象上使用缓冲区.
使用旧的套接字方法,我们将转换我们的状态对象,如下所示:
clientState cs = (clientState)asyncResult.AsyncState;
Run Code Online (Sandbox Code Playgroud)
但是,3.5框架是不同的.
有从字符串到客户端的字符串,我们似乎无法弄清楚缓冲区如何工作,所以我们可以在找到char3时处理整个字符串.
目前代码:
private void ProcessReceive(SocketAsyncEventArgs e)
{
string content = string.Empty;
// Check if the remote host closed the connection.
if (e.BytesTransferred > 0)
{
if (e.SocketError == SocketError.Success)
{
Socket s = e.UserToken as Socket;
//asyncResult.AsyncState;
Int32 bytesTransferred = e.BytesTransferred;
// Get the message received from the listener.
content += Encoding.ASCII.GetString(
e.Buffer, e.Offset, bytesTransferred);
if (content.IndexOf(Convert.ToString((char)3)) > -1)
{
e.BufferList = null;
// Increment the count of the total bytes receive by …
Run Code Online (Sandbox Code Playgroud)