Socket.Connect Throws and Error I can't catch - c#

I apologize if this has been answered on SO. I looked but didn't find it. My situation is my customer can connect to their TCP server either by being on the local network, or coming in remotely via a Verizon modem. (I am using .NET 3.5 - C#)
When I connect from either of those, things are fine. But what if I am accidentally on the "wrong" network (ex: I am at Starbucks and logged onto their wireless network. I forget, thinking I am on the Verizon card, so, now I try to log in) The server refuses the connection. Fine. But my Try/Catch is not catching anything... it simply exits the routine (if I am calling it through threadpool) or it hangs if I call it directly.
Here is the code (it fails on: m_clientSocket.Connect(ipEnd) ):
tcpConnectionObject passedTCPObject = (tcpConnectionObject) obj;
if (checkNetStat(passedTCPObject) == false)
{
updateStatus(false);
return;
}
try
{
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(passedTCPObject.ipString);
int iPortNo = System.Convert.ToInt16(passedTCPObject.portString);
IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo);
m_clientSocket.Connect(ipEnd);
if (m_clientSocket.Connected)
{
updateStatus(true);
//Wait for data asynchronously
WaitForData();
}
}
catch (SocketException e)
{
updateStatus(false);
return;
}
catch (Exception e)
{
updateStatus(false);
return;
}
updateStatus(true);
return ;
I call this from a Winform using the threadpool:
tcpConnectionObject passObject = new tcpConnectionObject(serverIP, serverPORT);
ThreadPool.QueueUserWorkItem(new WaitCallback(SRVR_CONNECT.connectToTCP), passObject);
This is called in the form's SHOW() method. When I do it like this, my form shows, but the status is never changed (which it should be by any result) If I put a debugger on it, and start stepping through the execution, I get to the Socket.Connect() and the debugger simply exits the mthod, and my form shows. I never hit any of my catches.
I have tried every error that Socket can generate, but it never catches anything. When I run my debugger, as soon as I hit that line, it exits my method, but there is no error that is catch-able.

If you really are waiting asynchronously (as you state in your comment), then you may be exiting the try/catch block before you actually get the exception.

It exits your method because you have a return statement inside of your catch block. You are telling it to return explicitly.

OK, I think Peter was on the right track. it seems like the debugger was simply skipping over my Connect line and moving on to the next line while waiting for the Socket.Connect() to finish (if that makes sense?)
Here's a change in the code that works... I move the packet creation, and the callback definition out of "WaitForData" and use the beginConnect method instead. Then I can set a timeout, which lets me know it fails -- so the code is changed to this:
try
{
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Cet the remote IP address
IPAddress ip = IPAddress.Parse(passedTCPObject.ipString);
int iPortNo = System.Convert.ToInt16(passedTCPObject.portString);
// Create the end point
ipEnd = new IPEndPoint(ip, iPortNo);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketPacket theSocPkt = new SocketPacket();
if (m_pfnCallBack == null)
{
m_pfnCallBack = new AsyncCallback(OnDataReceived);
}
// Give It 5 Seconds to connect
IAsyncResult result = socket.BeginConnect(ipEnd, m_pfnCallBack, theSocPkt);
bool success = result.AsyncWaitHandle.WaitOne(5000, true);
if (!success)
{
socket.Close();
throw new ApplicationException("Server Refused Connection");
}
// Success
updateStatus(true);
}
catch (SocketException e)
{
updateStatus(false);
return;
}
catch (Exception e)
{
updateStatus(false);
return;
}

Related

Socket reuse not working

I have got the following two functions to start and stop a 'local server' (Socket listener).
public String startServer(Int32 port, Int32 maximumPendingConnections, ref String errorMsg) {
try {
// Creates one SocketPermission object for access restrictions
permission = new SocketPermission(
NetworkAccess.Accept, // Allowed to accept connections
TransportType.Tcp, // Defines transport types
"", // The IP addresses of local host
SocketPermission.AllPorts // Specifies all ports
);
// Listening Socket object
sListener = null;
// Ensures the code to have permission to access a Socket
permission.Demand();
// Resolves a host name to an IPHostEntry instance
IPHostEntry ipHost = Dns.GetHostEntry("");
// Gets first IP address associated with a localhost
ipAddr = ipHost.AddressList[0];
// Creates a network endpoint
ipEndPoint = new IPEndPoint(ipAddr, port);
// Create one Socket object to listen the incoming connection
sListener = new Socket(
ipAddr.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp
);
// Associates a Socket with a local endpoint
sListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); //newly added as an answer
sListener.Bind(ipEndPoint);
sListener.Listen(maximumPendingConnections);
// Begins an asynchronous operation to accept an attempt
AsyncCallback aCallback = new AsyncCallback(AcceptCallback);
sListener.BeginAccept(aCallback, sListener);
} catch (Exception e) {
//ErrorHandling
}
return ipAddr.ToString();
}
Stop Connection:
public void stopServer(ref String errorMsg) {
try {
sListener.Shutdown(SocketShutdown.Both);
sListener.Disconnect(true);
sListener.Close();
sListener.Dispose();
} catch (Exception e) {
//Errorhandling
}
}
I have found on SO that you cannot reuse a socket, however if you set sListener.Disconnect(true); it should be able to reuse it. Besides I'm creating a new socket every time on starting. What am I missing here?
It gives the error back that every socket can only be used once. It gives an error on sListener.Bind(ipEndPoint);
#Edit 14:41 - 16-12-2015
I have found that if I add the following line of code before the sListener.Bind(ipEndPoint); It works;
sListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
If you want to actually to reuse the socket, don't do Shutdown, Close and Dispose, since these calls return the resources used by the socket to the OS. Basically, your socket handler becomes invalid and the work done by Disconnect(true) is futile then.
(You also don't need to do Shutdown and Close at the same time. Just Close will do the trick.)
The Connected property will always return false because socket is in listening state, not connected. Unfortunately, is not possible to "unlisten" the socket so you have to close it and create new socket.
Another issue is: the sequence Shutdown, Disconnect, Close and Dispose looks like kicking the dead body.

Catch an AsyncCallback

As you can see in the code below I'm calling connect with an IP and port.
s.EndConnect(ar) is throwing ObjectDisposedException if the server is not running. Now if this happens I want to wait 5 seconds and retry. The problem is catching it in the ConnectCallback and calling itself doesn't work since the object IAsyncResult ar is disposed? I'm not storing the IP and port globally.
Now obviously I can store them globally and fix it that way, but I'm wondering if there's something else to fix this. Since I don't need the IP and port anywhere else storing them globally seems unnecessary.
Socket s;
public ClientSettings()
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void Connect(string IP, int port)
{
/* Simple connection method */
try
{
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);
s.BeginConnect(ep, new AsyncCallback(ConnectCallback), s);
}
catch { }
}
void ConnectCallback(IAsyncResult ar)
{
s.EndConnect(ar);
connected = true;
byte[] buffer = new byte[8192];
s.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), buffer);
}
You're using outdated APIs. Use await with Socket and the problem goes away at much better code quality.
Or, use synchronous IO. This is appropriate if there will only be a few dozen connections at a time. This results in even easier code.
Alternatively, make IP and port instance variables. No need for globals.

How to reconnect to a socket gracefully

I have a following method that connects to an end point when my program starts
ChannelSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var remoteIpAddress = IPAddress.Parse(ChannelIp);
ChannelEndPoint = new IPEndPoint(remoteIpAddress, ChannelPort);
ChannelSocket.Connect(ChannelEndPoint);
I also have a timer that is set to trigger every 60 seconds to call CheckConnectivity, that attempts to send an arbitrary byte array to the end point to make sure that the connection is still alive, and if the send fails, it will attempt to reconnect.
public bool CheckConnectivity(bool isReconnect)
{
if (ChannelSocket != null)
{
var blockingState = ChannelSocket.Blocking;
try
{
var tmp = new byte[] { 0 };
ChannelSocket.Blocking = false;
ChannelSocket.Send(tmp);
}
catch (SocketException e)
{
try
{
ReconnectChannel();
}
catch (Exception ex)
{
return false;
}
}
}
else
{
ConnectivityLog.Warn(string.Format("{0}:{1} is null!", ChannelIp, ChannelPort));
return false;
}
return true;
}
private void ReconnectChannel()
{
try
{
ChannelSocket.Shutdown(SocketShutdown.Both);
ChannelSocket.Disconnect(true);
ChannelSocket.Close();
}
catch (Exception ex)
{
ConnectivityLog.Error(ex);
}
ChannelSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var remoteIpAddress = IPAddress.Parse(ChannelIp);
ChannelEndPoint = new IPEndPoint(remoteIpAddress, ChannelPort);
ChannelSocket.Connect(ChannelEndPoint);
Thread.Sleep(1000);
if (ChannelSocket.Connected)
{
ConnectivityLog.Info(string.Format("{0}:{1} is reconnected!", ChannelIp, ChannelPort));
}
else
{
ConnectivityLog.Warn(string.Format("{0}:{1} failed to reconnect!", ChannelIp, ChannelPort));
}
}
So how I'd test the above, is to physically unplug the LAN cable from my ethernet device, allowing my code to attempt to reconnect (which fails obviously) and reconnect back the LAN cable.
However, even after reconnecting the LAN cable (able to ping), ChannelSocket.Connect(ChannelEndPoint) in my Reconnect method always throws this error
No connection could be made because the target machine actively refused it 192.168.168.160:4001
If I were to restart my whole application, it connects successfully. How can I tweak my reconnect method such that I don't have to restart my application to reconnect back to my Ethernet device?
If an application closes a TCP/IP port, the protocol dictates that the port stays in TIME_WAIT state for a certain duration (default of 240 seconds on a windows machine).
See following for references -
http://en.wikipedia.org/wiki/Transmission_Control_Protocol
http://support.microsoft.com/kb/137984
http://www.pctools.com/guides/registry/detail/878/
What this means for your scenario - is that you cannot expect to close (willingly or unwillingly) and re-open a port within a short period of time (even several seconds). Despite some registry tweaks which you'd find on internet.. the port will be un-available for any app on windows, for a minimum of 30 seconds. (Again, default is 240 seconds)
Your options - here are limited...
From the documentation at http://msdn.microsoft.com/en-us/library/4xzx2d41(v=vs.110).aspx -
"If the socket has been previously disconnected, then you cannot use this (Connect) method to restore the connection. Use one of the asynchronous BeginConnect methods to reconnect. This is a limitation of the underlying provider."
The reason why documentation suggests that BeginConnect must be used is what I mentioned above.. It simply doesn't expect to be able to establish the connection right away.. and hence the only option is to make the call asynchronously, and while you wait for the connection to get established in several minutes, do expect and plan for it to fail. Essentially, likely not an ideal option.
If the long wait and uncertainty is not acceptable, then your other option is to somehow negotiate a different port between the client and server. (For example, in theory you could use UDP, which is connectionless, to negotiate the new TCP port you'd re-establish the connection on). Communication using UDP, in theory of course, itself is not guaranteed by design. But should work most of the times (Today, networking in typical org is not that flaky / unreliable). Subjective to scenario / opinion, perhaps better than option 1, but more work and smaller but finite chance of not working.
As suggested in one of the comments, this is where application layer protocols like http and http services have an advantage. Use them, instead of low level sockets, if you can.
If acceptable, this is the best option to go with.
(PS - FYI - For HTTP, there is a lot of special handling built into OS, including windows - For example, there is a dedicated driver Http.sys, specially for dealing with multiple apps trying to listen on same port 80 etc.. The details here are a topic for another time.. point is, there is lots of goodness and hard work done for you, when it comes to HTTP)
Maybe you should switch to a higher abstraction class, which better deals with all these nifty little details?
I'm going to use for these network connections the TcpListener and TcpClient classes. The usage of these classes is quite easy:
The client side:
public void GetInformationAsync(IPAddress ipAddress)
{
_Log.Info("Start retrieving informations from address " + ipAddress + ".");
var tcpClient = new TcpClient();
tcpClient.BeginConnect(ipAddress, _PortNumber, OnTcpClientConnected, tcpClient);
}
private void OnTcpClientConnected(IAsyncResult asyncResult)
{
try
{
using (var tcpClient = (TcpClient)asyncResult.AsyncState)
{
tcpClient.EndConnect(asyncResult);
var ipAddress = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address;
var stream = tcpClient.GetStream();
stream.ReadTimeout = 5000;
_Log.Debug("Connection established to " + ipAddress + ".");
var formatter = new BinaryFormatter();
var information = (MyInformation)formatter.Deserialize(stream);
_Log.Info("Successfully retrieved information from address " + ipAddress + ".");
InformationAvailable.FireEvent(this, new InformationEventArgs(information));
}
}
catch (Exception ex)
{
_Log.Error("Error in retrieving informations.", ex);
return;
}
}
The server side:
public void Start()
{
ThrowIfDisposed();
if (_TcpServer != null;)
_TcpServer.Stop();
_TcpServer = new TcpListener(IPAddress.Any, _PortNumber);
_TcpServer.Start();
_TcpServer.BeginAcceptTcpClient(OnClientConnected, _TcpServer);
_Log.Info("Start listening for incoming connections on " + _TcpServer.LocalEndpoint + ".");
}
private void OnClientConnected(IAsyncResult asyncResult)
{
var tcpServer = (TcpListener)asyncResult.AsyncState;
IPAddress address = IPAddress.None;
try
{
if (tcpServer.Server != null
&& tcpServer.Server.IsBound)
tcpServer.BeginAcceptTcpClient(OnClientConnected, tcpServer);
using (var client = tcpServer.EndAcceptTcpClient(asyncResult))
{
address = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
_Log.Debug("Client connected from address " + address + ".");
var formatter = new BinaryFormatter();
var informations = new MyInformation()
{
// Initialize properties with desired values.
};
var stream = client.GetStream();
formatter.Serialize(stream, description);
_Log.Debug("Sucessfully serialized information into network stream.");
}
}
catch (ObjectDisposedException)
{
// This normally happens, when the server will be stopped
// and their exists no other reliable way to check this state
// before calling EndAcceptTcpClient().
}
catch (Exception ex)
{
_Log.Error(String.Format("Cannot send instance information to {0}.", address), ex);
}
}
This code works and doesn't make any problems with a lost connection on the client side. If you have a lost connection on the server side you have to re-establish the listener, but that's another story.
In ReconnectChannel just dispose the ChannelSocket object.
try
{
`//ChannelSocket.Shutdown(SocketShutdown.Both);
//ChannelSocket.Disconnect(true);
//ChannelSocket.Close();
ChannelSocket.Dispose();`
}
This is working for me. Let me know if it doesn't work for you.

How do I test my socket exception handling code?

I have the following code:
try
{
mainSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, serverPort);
mainSocket.Bind(ipEndPoint);
mainSocket.Listen(MAX_CONNECTIONS);
mainSocket.BeginAccept(new AsyncCallback(serverEndAccept), mainSocket);
OnNetworkEvents eventArgs =
new OnNetworkEvents(true, "Listening for Connection");
OnUpdateNetworkStatusMessage(this, eventArgs);
}
catch (SocketException e)
{
// add code here
}
catch (ObjectDisposedException e)
{
// add code here
}
How do I test the code's SocketException given the server is listening successfully all of the time?
Do not test against the live network. Mock the socket and test against a mock that throws a SocketException.
you could add something like this for testing:
#if (UNITTEST)
throw new SocketException();
#endif
Then in your unit test compile just define that variable.
Otherwise do something to force an exception. Like have an invalid config setting that won't let it connect for use with your unit test code.
Unplug your network cable or shut off your wireless (assuming you're testing against a remote server).
Manually throw a SocketException from inside your try block.
Like
throw new SocketException("Testing");

udpclient.close doesn't always close the socket?

I'm using the code below, it seem sometime the socket is not released
How did I found that?
by using process explorer(from sysinternal),
proprieties on the application
then going into TCP/IP tab.
I can see the port being used by typing "netstat -a" into a console
My problem is, after a while (like 5 weeks) there is like 40 port used by the application while it should be zero.
Anyone know why it does that?
public void Connect()
{
try {
// Resolve server address
IPHostEntry hostadd = Dns.GetHostEntry(TimeServer);
IPEndPoint EPhost = new IPEndPoint(hostadd.AddressList[0], 123);
//Connect the time server
UdpClient TimeSocket = new UdpClient();
TimeSocket.Connect(EPhost);
TimeSocket.Send(SNTPData, SNTPData.Length);
SNTPData = TimeSocket.Receive(ref EPhost);
TimeSocket.Close();
if (!IsResponseValid())
{
throw new Exception("Invalid response from " + TimeServer);
}
} catch(SocketException e)
{
throw new Exception(e.Message);
}
}
You're blocking forever on:
SNTPData = TimeSocket.Receive(ref EPhost);
If the socket never receives a packet it will sit there waiting until the process dies.
You'll need to close the socket by calling TimeSocket.Close() on a different thread or by setting a timeout on the receive using SetSocketOption.

Categories