gpt4 book ai didi

c# - C#异步服务器套接字的奇怪性能

转载 作者:行者123 更新时间:2023-11-30 17:22:18 25 4
gpt4 key购买 nike

我正在使用 C# 开发 Web 服务器,并让它在异步套接字调用上运行。奇怪的是,出于某种原因,当您开始加载页面时,第三个请求是浏览器无法连接的地方。它只是一直说“正在连接...”并且永远不会停止。如果我停下来。然后刷新,它会再次加载,但如果我在那之后再尝试一次,它会执行不会再次加载的操作。它会在那个循环中继续。我不太确定是什么让它这样做。

代码是从几个示例和我的一些旧代码中拼凑而成的。任何杂项提示也会有所帮助。

这是我处理一切的小 Listener 类

( pastied here 。认为这样阅读可能更容易)

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using irek.Request;
using irek.Configuration;
namespace irek.Server
{
public class Listener
{
private int port;
private Socket server;
private Byte[] data = new Byte[2048];
static ManualResetEvent allDone = new ManualResetEvent(false);
public Config config;

public Listener(Config cfg)
{
port = int.Parse(cfg.Get("port"));
config = cfg;
ServicePointManager.DefaultConnectionLimit = 20;
}

public void Run()
{
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, port);
server.Bind(iep);

Console.WriteLine("Server Initialized.");
server.Listen(5);
Console.WriteLine("Listening...");
while (true)
{
allDone.Reset();
server.BeginAccept(new AsyncCallback(AcceptCon), server);
allDone.WaitOne();
}

}

private void AcceptCon(IAsyncResult iar)
{
allDone.Set();
Socket s = (Socket)iar.AsyncState;
Socket s2 = s.EndAccept(iar);
SocketStateObject state = new SocketStateObject();
state.workSocket = s2;
s2.BeginReceive(state.buffer, 0, SocketStateObject.BUFFER_SIZE, 0, new AsyncCallback(Read), state);
}

private void Read(IAsyncResult iar)
{
try
{
SocketStateObject state = (SocketStateObject)iar.AsyncState;
Socket s = state.workSocket;

int read = s.EndReceive(iar);

if (read > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, read));

SocketStateObject nextState = new SocketStateObject();
nextState.workSocket = s;
s.BeginReceive(state.buffer, 0, SocketStateObject.BUFFER_SIZE, 0, new AsyncCallback(Read), nextState);
}
if (state.sb.Length > 1)
{
string requestString = state.sb.ToString();
// HANDLE REQUEST HERE
byte[] answer = RequestHandler.Handle(requestString, ref config);
// Temporary response
/*
string resp = "<h1>It Works!</h1>";
string head = "HTTP/1.1 200 OK\r\nContent-Type: text/html;\r\nServer: irek\r\nContent-Length:"+resp.Length+"\r\n\r\n";
byte[] answer = Encoding.ASCII.GetBytes(head+resp);
// end temp.
*/
state.workSocket.BeginSend(answer, 0, answer.Length, SocketFlags.None, new AsyncCallback(Send), s);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
return;
}
}

private void Send(IAsyncResult iar)
{
try
{
SocketStateObject state = (SocketStateObject)iar.AsyncState;
int sent = state.workSocket.EndSend(iar);
state.workSocket.Shutdown(SocketShutdown.Both);
state.workSocket.Close();
}
catch (Exception)
{

}
return;
}
}
}

还有我的 SocketStateObject:

public class SocketStateObject
{
public Socket workSocket = null;
public const int BUFFER_SIZE = 1024;
public byte[] buffer = new byte[BUFFER_SIZE];
public StringBuilder sb = new StringBuilder();
}

** 编辑 **

我已经根据 Chris Taylor 的一些建议更新了代码。

最佳答案

快速查看代码,我怀疑您可能会停止查询 AsyncReads,因为 s.Available 返回 0,我指的是以下代码

if (read > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, read));
if (s.Available > 0)
{
s.BeginReceive(state.buffer, 0, SocketStateObject.BUFFER_SIZE, 0, new AsyncCallback(Read), state);
return;
}
}

为了确认,把上面的改成下面的

if (read > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, read));

SocketStateObject nextState = new SocketStateObject();
nextState.workSocket = s;
s.BeginReceive(state.buffer, 0, SocketStateObject.BUFFER_SIZE, 0, new AsyncCallback(Read), nextState);
}

这不是代码的完整更正,但它会确认这是否是问题所在。您需要确保正确关闭套接字等。

更新我还注意到您在调用 BeginSend 时将套接字作为状态发送。

 state.workSocket.BeginSend(answer, 0, answer.Length, SocketFlags.None, new AsyncCallback(Send), state.workSocket); 

但是,您的回调 Send 正在将 AsyncState 转换为 SocketStateObject

SocketStateObject state = (SocketStateObject)iar.AsyncState; 

这将引发 InvalidCastExceptions,您只是通过添加空的 catch 来隐藏它。我相信其他人会同意,这是一种非常糟糕的做法,因为它有空捕获,它隐藏了太多您可以用来调试问题的信息。

关于c# - C#异步服务器套接字的奇怪性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2793063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com