为什么 SendAsync 不在我的 SocketAsyncEventArgs 上引发 Completed 事件,即使我的服务器套接字已经收到所有字节?
Why does SendAsync not raise the Completed Event on my SocketAsyncEventArgs even though my Server-Socket has already received all bytes?
所以我有一个非常简单的设置:等待客户端连接的服务器和连接到服务器并发送消息的客户端。服务器随后应再次发回相同的消息。但是当我尝试使用 client.SendAsync(sendArgs) 发送消息时,它实际上并没有调用 sendArgs 对象上的 Completed 事件,即使我可以看到我的 Server-Socket已经收到所有字节。那么为什么 sendArgs.Completed 在我的 Client-Socket 没有被提升 ,但是 receiveArgs.Completed 在我的 Server-Socket class?
使用的命名空间:System,System.Net,System.Net,System.Text
到目前为止,这是我的代码:
客户
public class SocketClient
{
public IPEndPoint Endpoint { get; }
private Socket client;
public SocketClient(IPAddress address, int port)
{
Endpoint = new IPEndPoint(address, port);
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void ConnectToServer(IPAddress serverAddress, int serverPort)
{
client.Bind(Endpoint);
try
{
client.Connect(serverAddress, serverPort);
}
catch (SocketException)
{
return;
}
}
public void SendMessage(string message)
{
SocketAsyncEventArgs sendArgs = new SocketAsyncEventArgs();
byte[] buffer = Encoding.ASCII.GetBytes(message);
sendArgs.SetBuffer(buffer, 0, buffer.Length);
sendArgs.Completed += SendArgs_Completed;
client.SendAsync(sendArgs);
}
private void SendArgs_Completed(object sender, SocketAsyncEventArgs e)
{
SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs();
byte[] buffer = new byte[1024];
receiveArgs.SetBuffer(buffer, 0, buffer.Length);
receiveArgs.Completed += ReceiveArgs_Completed;
client.ReceiveAsync(receiveArgs);
}
private void ReceiveArgs_Completed(object sender, SocketAsyncEventArgs e)
{
string message = Encoding.ASCII.GetString(e.Buffer);
Console.WriteLine(message);
}
}
服务器
public class SocketServer
{
public IPEndPoint Endpoint { get; }
private Socket server;
public SocketServer(IPAddress address, int port)
{
Endpoint = new IPEndPoint(address, port);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void Start()
{
server.Bind(Endpoint);
server.Listen(10);
SocketAsyncEventArgs acceptArgs = new SocketAsyncEventArgs();
acceptArgs.Completed += AcceptArgs_Completed;
server.AcceptAsync(acceptArgs);
}
private void AcceptArgs_Completed(object sender, SocketAsyncEventArgs e)
{
Socket connectionSocket = e.AcceptSocket;
SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs();
byte[] buffer = new byte[1024];
receiveArgs.SetBuffer(buffer, 0, buffer.Length);
receiveArgs.Completed += ReceiveArgs_Completed;
connectionSocket.ReceiveAsync(receiveArgs);
e.AcceptSocket = null;
server.AcceptAsync(e);
}
private void ReceiveArgs_Completed(object sender, SocketAsyncEventArgs e)
{
Socket connectionSocket = (Socket)sender;
SocketAsyncEventArgs sendArgs = new SocketAsyncEventArgs();
sendArgs.SetBuffer(e.Buffer, 0, e.Buffer.Length);
connectionSocket.SendAsync(sendArgs);
connectionSocket.ReceiveAsync(e);
}
}
编辑:我修复了它。我缺少的是检查 SendAsync 方法是同步完成还是异步完成。如果它同步完成,我需要自己调用该事件
bool completedAsync = client.SendAsync(sendArgs);
if(!completedAsync)
{
SendArgs_Completed(this, sendArgs);
}
所以我有一个非常简单的设置:等待客户端连接的服务器和连接到服务器并发送消息的客户端。服务器随后应再次发回相同的消息。但是当我尝试使用 client.SendAsync(sendArgs) 发送消息时,它实际上并没有调用 sendArgs 对象上的 Completed 事件,即使我可以看到我的 Server-Socket已经收到所有字节。那么为什么 sendArgs.Completed 在我的 Client-Socket 没有被提升 ,但是 receiveArgs.Completed 在我的 Server-Socket class?
使用的命名空间:System,System.Net,System.Net,System.Text
到目前为止,这是我的代码:
客户
public class SocketClient
{
public IPEndPoint Endpoint { get; }
private Socket client;
public SocketClient(IPAddress address, int port)
{
Endpoint = new IPEndPoint(address, port);
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void ConnectToServer(IPAddress serverAddress, int serverPort)
{
client.Bind(Endpoint);
try
{
client.Connect(serverAddress, serverPort);
}
catch (SocketException)
{
return;
}
}
public void SendMessage(string message)
{
SocketAsyncEventArgs sendArgs = new SocketAsyncEventArgs();
byte[] buffer = Encoding.ASCII.GetBytes(message);
sendArgs.SetBuffer(buffer, 0, buffer.Length);
sendArgs.Completed += SendArgs_Completed;
client.SendAsync(sendArgs);
}
private void SendArgs_Completed(object sender, SocketAsyncEventArgs e)
{
SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs();
byte[] buffer = new byte[1024];
receiveArgs.SetBuffer(buffer, 0, buffer.Length);
receiveArgs.Completed += ReceiveArgs_Completed;
client.ReceiveAsync(receiveArgs);
}
private void ReceiveArgs_Completed(object sender, SocketAsyncEventArgs e)
{
string message = Encoding.ASCII.GetString(e.Buffer);
Console.WriteLine(message);
}
}
服务器
public class SocketServer
{
public IPEndPoint Endpoint { get; }
private Socket server;
public SocketServer(IPAddress address, int port)
{
Endpoint = new IPEndPoint(address, port);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void Start()
{
server.Bind(Endpoint);
server.Listen(10);
SocketAsyncEventArgs acceptArgs = new SocketAsyncEventArgs();
acceptArgs.Completed += AcceptArgs_Completed;
server.AcceptAsync(acceptArgs);
}
private void AcceptArgs_Completed(object sender, SocketAsyncEventArgs e)
{
Socket connectionSocket = e.AcceptSocket;
SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs();
byte[] buffer = new byte[1024];
receiveArgs.SetBuffer(buffer, 0, buffer.Length);
receiveArgs.Completed += ReceiveArgs_Completed;
connectionSocket.ReceiveAsync(receiveArgs);
e.AcceptSocket = null;
server.AcceptAsync(e);
}
private void ReceiveArgs_Completed(object sender, SocketAsyncEventArgs e)
{
Socket connectionSocket = (Socket)sender;
SocketAsyncEventArgs sendArgs = new SocketAsyncEventArgs();
sendArgs.SetBuffer(e.Buffer, 0, e.Buffer.Length);
connectionSocket.SendAsync(sendArgs);
connectionSocket.ReceiveAsync(e);
}
}
编辑:我修复了它。我缺少的是检查 SendAsync 方法是同步完成还是异步完成。如果它同步完成,我需要自己调用该事件
bool completedAsync = client.SendAsync(sendArgs);
if(!completedAsync)
{
SendArgs_Completed(this, sendArgs);
}