gpt4 book ai didi

c# TCP 套接字服务器不响应客户端

转载 作者:可可西里 更新时间:2023-11-01 02:56:50 24 4
gpt4 key购买 nike

我有一个使用 Mono 在 Ubuntu 12.04 下运行的 C# 服务器,代码如下:

while (true)
{
Console.WriteLine("Socket ready...");
using (Socket handlerSocket = listenerSocket.Accept())
{
Console.WriteLine("New request...");
BackgroundWorker newBackgroundWorker = new BackgroundWorker();
byte[] buffer = new byte[1024];
Console.WriteLine("Buffer allocated...");
handlerSocket.Receive(buffer);
//newBackgroundWorker.DoWork += (s, o) =>
//{
using (Socket Sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
Console.WriteLine("Buffer received...");
string request = Encoding.Unicode.GetString(buffer);
Console.WriteLine("New request from : {0} /t Request : {1}", ((IPEndPoint)handlerSocket.RemoteEndPoint).Address.ToString(), request);
if (request.Contains("$GetBattleList"))
{
//Chain of if's and else's continues
//The following else if is the only case being tested

else if (request.Contains("$GetZeroBasedProfile"))
{
Console.WriteLine("Sending profile...");
Sender.Connect(handlerSocket.RemoteEndPoint); //Fails here due to not being able to reach the client application, throws a timeout exception
Sender.SendFile(_profileLocation);
}
else Console.WriteLine("Invalid request. Ignoring...");
}
}
}

服务器能够接收客户端请求,但是无法响应,因为它每次都无法连接。我的代码中是否遗漏了某些内容,或者这里是否还有其他内容?

最佳答案

您声明的附加 Socket Sender 是不必要的。您从 listenerSocket.Accept() 收到的 handlerSocket 已经是您可以用来与客户端通信的套接字。

关于c# TCP 套接字服务器不响应客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26868533/

24 4 0
文章推荐: java - 类型不匹配 : cannot convert from Optional to BasketDTO