| September 19, 2008 12:40 PM PDT | |
Challenge
Determine IPEndPoint visibility for a mobilized .NET* application. This is first question that we need to ask before transferring data to or from a remote system: "Is the service on my target machine visible?" This means knowing not only the IP address of the machine, but the port number on which a server is listening. For a Web service, this port would generally be either 80 for unencrypted traffic or 443 for SSL traffic.
Solution
One way to determine the visibility of a service is simply to attempt a connection to it. If it works, we can see it. If we get an error, we can use the error code to find out why we can't see it. Using the Connect() method of the Sockets class, it might look like this:
{
s.Connect(ipEndPoint); bEndPointVisible = true;
//If no exception is thrown, we have a connection
bEndPointVisible = true;
}
catch(SocketException se)
{
bEndPointVisible = false;
//figure out what the problem was
switch ( se.ErrorCode )
{
case 10165:
//...
}
}
|
Using the error codes for the Socket.Connect() method, we can determine what the problem is:
This method has the following advantages:
This item is part of a series of items that present sample implementations to obtain network-connection information using the classes found in the .NET Framework:
| Error Code | Meaning |
| WSANOTINITIALISED | A successful WSAStartup call must occur before using this function. |
| WSAENETDOWN | The network subsystem has failed. |
| WSAEADDRINUSE | The socket's local address is already in use and the socket was not marked to allow address reuse with SO_REUSEADDR. This error usually occurs when executing bind, but could be delayed until this function if the bind was to a partially wildcard address (involving ADDR_ANY) and if a specific address needs to be committed at the time of this function. |
| WSAEINTR | The blocking Windows Socket 1.1 call was canceled through WSACancelBlockingCall. |
| WSAEINPROGRESS | A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. |
| WSAEALREADY | A non-blocking connect call is in progress on the specified socket. Note: In order to preserve backward compatibility, this error is reported as WSAEINVAL to Windows Sockets 1.1 applicat ions that link to either Winsock.dll or Wsock32.dll. |
| WSAEADDRNOTAVAIL | The remote address is not a valid address (such as ADDR_ANY). |
| WSAEAFNOSUPPORT | Addresses in the specified family cannot be used with this socket. |
| WSAECONNREFUSED | The attempt to connect was forcefully rejected. |
| WSAEFAULT | The name or the namelen parameter is not a valid part of the user address space, the namelen parameter is too small, or the name parameter contains incorrect address format for the associated address family. |
| WSAEINVAL | The parameter s is a listening socket. |
| WSAEISCONN | The socket is already connected (connection-oriented sockets only). |
| WSAENETUNREACH | The network cannot be reached from this host at this time. |
| WSAENOBUFS | No buffer space is available. The socket cannot be connected. |
| WSAENOTSOCK | The descriptor is not a socket. |
| WSAETIMEDOUT | Attempt to connect timed out without establishing a connection. |
| WSAEWOULDBLOCK | The socket is marked as non-blocking and the connection cannot be completed immediately. |
| WSAEACCES | Attempt to connect datagram socket to broadcast address failed because setsockopt option SO_BROADCAST is not enabled. |
This method has the following advantages:
- If the connection is successful, the socket is ready for use
- The SocketExecption.ErrorCode property can give useful information about the cause of the error. If the server is available, the call can return almost immediately; but if the server is not available, the call can take up to 20 seconds before it times out and throws an exception. For this reason, it may be best to use the asynchronous Socket.BeginConnect() call; but even so, it will take up to 20 seconds in order to verify that the service is not visible.
This item is part of a series of items that present sample implementations to obtain network-connection information using the classes found in the .NET Framework:
Source
For more complete information about compiler optimizations, see our Optimization Notice.
Comments (0) 
Trackbacks (0)
Leave a comment 
To obtain technical support, please go to Software Support.
