Have a client application that is supposed to connect to a given server ip and port.
When server is listening, all is fine, but if server is unreachable/off the socket.connect call hangs forever, even when server starts listening after the call to connect is done.
I run the client code in separate thread:
// Create socket and connect to the server's IP address and port
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ep); //Never returns if cable is disconnected or server away!!!
return socket;
It does not throw any exceptions or anything, just hangs.
I can see two approaches here:
Hardware reboot using watchdog, dirty
Kill the thread and start again, have not tried this but can see issues with memory etc in the end if this is an underlying bug/issue.
I have many threads that listen on my device and they all work without any problem:
Telnet
WEB
UDP
Remote flash
I only have this issue when I try to connect out from my device. This is basic socket TCP/IP function and it me that this would be an issue!!
Hope anyone can help me out, this is a very important aspect of my product and need to work…
A full system restart is not a good way to solve this, my system is supposed to do many things and if it keeps restarts, every minute if the server is away other functions will suffer.
Thanks for the workaround tips will try them out.
Anyhow it looks like this problem has hang around for a while and it baffles me that this issue is not fixed ASAP. Socket.Connect should have a working timeout property in my mind…
ExecutionConstraint worked just fine, easy and quick. Not have time to do long time testing though. Have tried a number of connect disconnect scenarios and remove ethernet cable from EMX all handled like expected.
Thanks all but still, this is a bug
For now hardcoded a 10 sec interval, not sure how aggressive I should be.
Thanks for the help, here are my connect code:
private Socket ConnectSocket(String server, Int32 port)
{
try
{
IPAddress hostIP = IPAddress.Parse(server);
IPEndPoint ep = new IPEndPoint(hostIP, port);
// Create socket and connect to the server's IP address and port
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
ExecutionConstraint.Install(10000, 0);
tryConnect = true;
socket.Connect(ep);
ExecutionConstraint.Install(-1, 0);
return socket;
}
catch
{
return null;
}
}
@ andre.m I was a bit quick to say ok to this fix. Every approximate 30min I get a full system crash!!
This was what I was afraid of because the underlying socket.connect code fails I guess it does not get cleaned up correct so eventually the system crashes.
Any comments/thoughts are appreciated.
@ andre.m but the other solution was basically the same as using ExecutionConstraint but make a thread handler myself. I will not be to assertive but I guess a enough calls to socket.Connect without a listener in the other end will result in a system crash regardless of how you kill it when it does not return. Will see if I get time to try the other solution also…
I suspect there are some underlying resource issue when socket.Connect does not return but that is only speculation on my end.
But like I wrote earlier that is a bad fix, during system boot other functions of my system will suffer. While waiting for a valid server we need to buffer some data and monitor inputs…
In some installations we have a local server that we can wait for connections from, but if the unit is standalone behind firewalls and what not we need it to connect out. If someone forget to disable this feature if not in use the system will suffer if it reboots every 10sec.
I will try your approach with the thread and timer, what timeout are you using if you dont mind me asking?
Sad it takes so long to fix stuff, especially things like this, I would think socket.connect is a critical functions to any Ethernet enabled device these days…
I had same problems on EMX with 4.1 and Im fix it with opening connection in other thread and in new thread check for timeout and then close it… Im post somewhere on old forum snippet how to do it…
if this is a bug, are there anyone who know what default timeout value should be? And is there anyway to change this timeout? (except for 2 ways from this link) because as I l know, socket.connect has only 1 param (Endpoint)
MS said that it is fixed in 4.3, I am wondering that if it is fixed like default 15 secs, (4.2 hangs forever).
In case user want to reduce that value, how do they set it? :))))
So 4.3 may go back with our current solutions, use a wrapper class or another thread to reduce time out value. :)))
Hi all, been on summer holyday so have not been able to try the wrapper solution.
The ExecutionConstraint solution crashes every 30-40min so I hope the thread wrapper works better.
Are there any ETA from GHI for 4.3 support, cant fins any release date? (Q3, Q4, 2014)
Gus, have seen that but it does not give any hint of date/month or quarter (quote: GHI Electronics will hold on releasing NETMF 4.3 for some time) so thats the reason for me asking. I also have understood it that the problem in this thread are solved in 4.3?
It also looks that 4.3 has better support for webservices, have not been looking at that yet but may need to in a couple of months.