n00b
n00b

Reputation: 6360

Question on SocketAsyncEventArgs (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!

Upvotes: 0

Views: 4832

Answers (3)

Len Holgate
Len Holgate

Reputation: 21644

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 of SocketAsyncEventArgs per send.

Upvotes: 2

C_Rance
C_Rance

Reputation: 661

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.

Upvotes: 1

TobiHeidi
TobiHeidi

Reputation: 1251

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

Upvotes: 0

Related Questions