I'm facing a problem of stop the socket.connect() function. Sometimes the physical link between my local machine and the remote machine might be good, but due to some reason, the remote endpoint cannot be accessed, maybe a firewall or the port on the remote machine is closed. In such cases, the socket.connection() function will be stuck there and waits for an infinite long time...Even the firewall is disabled later, the function will still stuck there forever.
So I tried to find a way to stop the socket.connect() when faces the above situations.
The thing is I'm using a .net micro framework in which I dont have timeout mechanism or task or socket.beginconnect();
I'm trying making the socket.connect() itself a thread and tried to abort() it after 2 seconds if (!thread.join(2000)). However, I dont quite understand the abort() function and i've heard its an unwise way to do so and it does not work afterall.
Also I'd like the socket keep trying until it succeed in connecting.
Now i dont know what to do about it? can anyone help? thx a lot. it's a netduino plus board
main function {
remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP_add), 30000);
while (true){ m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//Set up socket m_socket.Connect(remoteEndPoint); myThread.Start(); if (!myThread.Join(2000)) { Debug.Print(myThread.ThreadState.ToString()); myThread.abort();
m_socket.close(); }
else
{break;} } } private static void socket_connect() { m_socket.Connect(remoteEndPoint);//Connect to remote device }
I'm also have the following code by creating a new thread each time.
while (true) { try { m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//reset up socket myThread = new System.Threading.Thread(new System.Threading.ThreadStart(socket_connect)); myThread.Start(); if (!myThread.Join(2000)) { throw new SocketException(SocketError.AccessDenied); } } catch (Exception ex) {
m_socket.Close();
Debug.GC(true); } } } private static void socket_connect() { m_socket.Connect(remoteEndPoint);//Connect to remote device }
The above code will allow the GC to deal with the useless threads instead of using abort(). This code will run like 10 mins before it stops running anymore. And I've checked the memory, it still have more than 45M. Can anyone tell me why this happens?