关于 SocketAsyncEventArgs (SAEA) 的问题
我已经实现了异步套接字连接并使其与客户端成功通信。我当前遇到的问题是在独立于定义的协议的计时器上向客户端发送数据包时。
每次我调用 SAEA.AcceptSocket.SendAsync(...) 时,我都会收到异常: System.InvalidOperationException:“使用此 SocketAsyncEventArgs 实例已经在进行异步套接字操作。”
我做了一些测试,但似乎无法调用 SAEA.AcceptSocket.SendAsync(...)紧接着一个接着一个。这是 SAEA 的设计方式还是我在代码中做错了什么导致引发异常?
我花了一些时间在谷歌上搜索这个问题,但似乎找不到任何答案。如果有人可以帮助我或至少指出我正确的方向,我将不胜感激!
谢谢!
I've implemented an asynchronous socket connection and have it communicating with a client successfully. The problem I'm currently having is when sending packets to the client on a timer independent to the protocol that's defined.
Every time I call SAEA.AcceptSocket.SendAsync(...) I get the exception:
System.InvalidOperationException: "An asynchronous socket operation is already in progress using this SocketAsyncEventArgs instance."
I did some testing and cannot seem to call SAEA.AcceptSocket.SendAsync(...) immediately one after the other. Is this the way SAEA has been designed or am I be doing something wrong in my code that's causing the exception to be raised?
I've spent a bit of time googling this but can't seem to find any answers. If someone could help me or atleast point me in the right direction it'd be much appreciated!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每个 SocketAsyncEventArgs 实例只能挂起一个操作。您要么需要等待当前挂起的操作完成,要么只需在每次发送时使用 SocketAsyncEventArgs 的唯一实例。
Only a single operation can be pending per instance of
SocketAsyncEventArgs
. You either need to wait for the currently pending operation to complete OR simply use a unique instance ofSocketAsyncEventArgs
per send.每次发送消息时,它都会尝试发送或等待,直到超时或错误。
您可能会发生什么情况,您仍在处理上一条消息,并且尝试使用相同的 SocketAsyncEventArgs 再次发送。如果 SocketAsyncEventArgs 正在等待下一个进程,通过要求它转到另一个进程,您将收到此错误。
因此,如果侦听器是服务器,当客户端尝试发送时,我建议您创建一个新连接并为其创建一个新的 SocketAsyncEventArgs。
Each time you send the message over, it will try to send or wait till timeout or error.
What happen to u might be you are still processing the previous message and u tried to send again, using the same SocketAsyncEventArgs. If the SocketAsyncEventArgs is waiting for the next process, by asking it to go to another process, u will get this error.
So if the listener is a server, when the client tries to send, I would suggest u do a new connection and create a new SocketAsyncEventArgs for it.
我认为你必须首先调用 SAEA.AcceptSocket.EndAccept(...) 来结束异步连接过程或 SAEA.AcceptSocket.EndSend(...) 来结束之前的发送
i think you have to call SAEA.AcceptSocket.EndAccept(...) first to end the async connection process or SAEA.AcceptSocket.EndSend(...) to end the previous sending