Asynchronous Sockets

  • Thread starter Thread starter Andrew Ducker
  • Start date Start date
A

Andrew Ducker

I have the following code, which throws an error of "An invalid argument
was supplied" when I run it.

Can anyone tell me the stupid mistake I'm obviously making?

Socket listener = new Socket(*snipped*);
listener.Bind(localEndPoint);
while (true)
{
AsyncCallback MyCallback = new AsyncCallback(ConnectionMade);
listener.BeginAccept(MyCallback,listener);
}

public void ConnectionMade( IAsyncResult ar)
{
MessageBox.Show("Got an attempt to connect!");
}

The error happens on the line BeginAccept.

Any suggestions?

Andy D
 
Hi Andrew,

Thank you for posting here. Regarding on the issue, we're
finding proper resource to assist you and we will update as soon as posible.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
with no warranties, and confers no rights.)
 
Andrew said:
I have the following code, which throws an error of "An invalid argument
was supplied" when I run it.

Can anyone tell me the stupid mistake I'm obviously making?

Socket listener = new Socket(*snipped*);
listener.Bind(localEndPoint);
while (true)
{
AsyncCallback MyCallback = new AsyncCallback(ConnectionMade);
listener.BeginAccept(MyCallback,listener);
}

public void ConnectionMade( IAsyncResult ar)
{
MessageBox.Show("Got an attempt to connect!");
}

The error happens on the line BeginAccept.

1. you don't call listener.Listen() before BeginAccept
2. you spin with the while(true){}-loop. BeginAccept is
asynchronous, so it doesn't block.

bye
Rob
 
Back
Top