Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

Socket exception in BeginConnect

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
linuxfedora@yahoo.com.hk - 03 Sep 2007 09:38 GMT
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)
Peter Duniho - 03 Sep 2007 18:32 GMT
> 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
linuxfedora@yahoo.com.hk - 05 Sep 2007 10:56 GMT
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.
Peter Duniho - 05 Sep 2007 18:06 GMT
> The error detail:
>
[quoted text clipped - 8 lines]
>
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.