gpt4 book ai didi

c# - C#:远程主机上的套接字ReceiveAsync和SendAsync出现问题

转载 作者:行者123 更新时间:2023-12-03 12:05:32 25 4
gpt4 key购买 nike

首先,我想说的是,带有服务器/客户端的代码在我的PC上可以正常工作(客户端和服务器都在同一台PC上运行)

当我在专用服务器上托管服务器时,问题就开始了。第二次我尝试与客户端发送或接收时,客户端将花费很多时间来完成操作(谈论接收1个字节的数据需要花费超过1个小时的时间!)

**我可以通过在每次发送/接收命令后简单地断开并重新连接套接字来避免此问题

我不明白为什么如果我要在同一连接 session 中执行多于1个发送/接收操作的时间,那么为什么要花这么多时间。

这是一个例子:

连接服务器:

    RC_Server = new SocketConnection_Client(RC_IpEndPoint);

现在客户端已连接到服务器,并且我可以正常开始发送/接收。

我第一次尝试发送或接收它时,它的工作原理非常完美且快速(无论缓冲区长度如何)。
    RC_Server.sendCompleted = false;
RC_Server.Send(buffer); //in this case buffer size is 1024
while (!RC_Server.sendCompleted) { } //here thread is waiting for the send to complete

现在,如果我想开始与服务器的接收 session
        RC_Server.receiveCompleted = false;
RC_Server.Receive(buffer.Length); //in this case byffer needs to get 4 bytes from server
while (!RC_Server.receiveCompleted) { } // <<- Here it will take VERY long time for the operation to complete
buffer = RC_Server.buffer;

第二个接收操作永远不会到达socket_receiveCompleted_Completed事件。

但是..如果我将在第一个发送/接收 session 后关闭与服务器的连接,然后打开与服务器的新连接,然后执行下一个发送/接收 session ,
那么一切都会完美地进行。 (并多次重复此方法以进行多次发送/接收)

再次,在我的工作站中不会发生此问题(如果我在同一台PC上运行服务器和客户端)。仅当我将服务器托管在远程主机上时,它才会发生。

这是我的套接字类的一部分:
    class SocketConnection_Client
{
public string errorMsg = string.Empty;
public bool receiveCompleted = false;
public bool sendCompleted = false;
int bytesReceived = 0;
public byte[] buffer;
Socket socket;

public SocketConnection_Client(IPEndPoint ipEP)
{
socket = new Socket(ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Connect(ipEP);
}
catch (Exception ex)
{
errorMsg = ex.Message;
socket.Dispose();
socket = null;
}
}

public void Send(byte[] sBuffer)
{
buffer = null;
if (socket == null)
{
sendCompleted = true;
return;
}
sendCompleted = false;

SocketAsyncEventArgs socket_sendCompleted = new SocketAsyncEventArgs();
socket_sendCompleted.SetBuffer(sBuffer, 0, sBuffer.Length);
socket_sendCompleted.Completed += socket_sendCompleted_Completed;
socket.SendAsync(socket_sendCompleted);
}

void socket_sendCompleted_Completed(object sender, SocketAsyncEventArgs e)
{
sendCompleted = true;
}

public void Receive(int bLength)
{
bytesReceived = 0;
buffer = null;
if (socket == null)
{
receiveCompleted = true;
return;
}
receiveCompleted = false;

buffer = new byte[bLength];
SocketAsyncEventArgs socket_receiveCompleted = new SocketAsyncEventArgs();
socket_receiveCompleted.SetBuffer(buffer, 0, buffer.Length);
socket_receiveCompleted.Completed += socket_receiveCompleted_Completed;
socket.ReceiveAsync(socket_receiveCompleted);
}

private void Receive_Continue()
{
SocketAsyncEventArgs socket_receiveCompleted = new SocketAsyncEventArgs();
socket_receiveCompleted.SetBuffer(buffer, bytesReceived, buffer.Length - bytesReceived);
socket_receiveCompleted.Completed += socket_receiveCompleted_Completed;
socket.ReceiveAsync(socket_receiveCompleted);
}

void socket_receiveCompleted_Completed(object sender, SocketAsyncEventArgs e)
{
bytesReceived += e.BytesTransferred;
if (e.BytesTransferred == 0 && bytesReceived == 0)
{
socket.Dispose();
socket = null;
receiveCompleted = true;
throw new Exception("Server Disconnected");
}
else if (bytesReceived != buffer.Length)
Receive_Continue();
else
receiveCompleted = true;
}
}

感激。
谢谢。

最佳答案

您将忽略ReceiveAsync的返回值。请注意the documentation-有两种可能的行为,您必须检查返回值以区分它们:

Returns true if the I/O operation is pending. The SocketAsyncEventArgs.Completed event on the e parameter will be raised upon completion of the operation.

Returns false if the I/O operation completed synchronously. In this case, the SocketAsyncEventArgs.Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.

关于c# - C#:远程主机上的套接字ReceiveAsync和SendAsync出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18387860/

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