Socket exception in BeginConnect

L

linuxfedora

Sometime, when i try to use the following function to connect to a
socket, it will come out the error as below, but it seems the
arugments are correct, ip and port are correct, then what would be the
cause of that?thanks

public void Connect(string ip, int port)
{
this.ip = ip;
this.port = port;
try
{
handle.BeginConnect(ip, port, new
AsyncCallback(OnConnectCallBack), null);
}
catch (SocketException e)
{
ThrowError(e);
}
catch (Exception e)
{
ThrowError(e);
}
}


©ó System.Net.Sockets.Socket.DoMultipleAddressConnectCallback(Object
result, MultipleAddressConnectAsyncResult context)
©ó System.Net.Sockets.Socket.BeginConnect(String host, Int32 port,
AsyncCallback requestCallback, Object state)
 
P

Peter Duniho

Sometime, when i try to use the following function to connect to a
socket, it will come out the error as below, but it seems the
arugments are correct, ip and port are correct, then what would be the
cause of that?thanks

And what is the error? You posted what appears to be a partial stack
trace, but that's not the actual error, never mind does it provide
nearly enough information to advise you on what might be wrong.

Pete
 
L

linuxfedora

The error detail:

When i catch the SocketException, it throws:
ErrorCode: 10022
SocketErrorCode: InvalidArgument
StackTrace: at
System.Net.Sockets.Socket.DoMultipleAddressConnectCallback(Object
result, MultipleAddressConnectAsyncResult context)\r\n ©ó
System.Net.Sockets.Socket.BeginConnect(String host, Int32 port,
AsyncCallback requestCallback, Object state)\r\n

ip and port is exisiting when it is caught.
 
P

Peter Duniho

The error detail:

When i catch the SocketException, it throws:
ErrorCode: 10022
SocketErrorCode: InvalidArgument
StackTrace: at
System.Net.Sockets.Socket.DoMultipleAddressConnectCallback(Object
result, MultipleAddressConnectAsyncResult context)\r\n ?
System.Net.Sockets.Socket.BeginConnect(String host, Int32 port,
AsyncCallback requestCallback, Object state)\r\n

ip and port is exisiting when it is caught.

Well, I still have no idea.

The only things that seem a little unusual about your call to
BeginConnect() are that you use the new operator to create the delegate
instance, and that you pass null for the "state" parameter.

I don't actually think that there's anything wrong with the way you
initialize the delegate, but just in case you might try just using the
method name directly, rather than bothering with the AsyncCallback
instantiation. The C# compiler should handle the details for you.

As far as the "state" parameter goes, I suppose that if the class that
contains the Socket instance is the one in which the callback method
exists, this isn't strictly necessary. Even if you did need for "state"
to be a valid value in your own code, I don't know why the .NET code
would require it.

Of course, the biggest puzzler is that your original post implies that
this code does work some of the time.

If I had to guess, I'd say that for some reason the Socket instance
"handle" is invalid for some reason, possibly because it's been disposed
already. But why that would happen before you've even connected the
socket, I can't say.

I think you will need to post a concise-but-complete example of code
that reliable reproduces the problem for anyone here to provide better
information about what's wrong. The information you've provided so far
is just too vague to allow useful commentary.

For what it's worth, I found this link, with the source code for an
implementation of the Socket class. Whether it's current, I have no
idea, but you might get some insight looking at it (or you might not...I
didn't):
http://www.koders.com/csharp/fid6FA73953FA5EBB3DC897A92D3902EFAB164F66D2.aspx#L6088

Sorry...

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top