multithreading - Java threaded socket connection timeouts -
i have make simultaneous tcp socket connections every x seconds multiple machines, in order status update packet.
i use callable thread class, creates future task connects each machine, sends query packet, , receives reply returned main thread creates callable objects.
my socket connection class :
public class clientconnect implements callable<string> { connection con = null; statement st = null; resultset rs = null; string hostipp, hostnamee; clientconnect(string hostname, string hostip) { hostnamee=hostname; hostipp = hostip; } @override public string call() throws exception { return getdata(); } private string getdata() { socket = new socket(); socketaddress sa = null; printwriter out = null; bufferedreader in = null; try { sa = new inetsocketaddress(inetaddress.getbyname(hostipp), 2223); } catch (unknownhostexception e1) { e1.printstacktrace(); } try { so.connect(sa, 10000); out = new printwriter(so.getoutputstream(), true); out.println("\1idc_update\1"); in = new bufferedreader(new inputstreamreader(so.getinputstream())); string [] response = in.readline().split("\1"); out.close();in.close();so.close(); = null; try{ integer.parseint(response[2]); } catch(numberformatexception e) { system.out.println("number format exception"); return hostnamee + "|-1" ; } return hostnamee + "|" + response[2]; } catch (ioexception e) { try { if(out!=null)out.close(); if(in!=null)in.close(); so.close();so = null; return hostnamee + "|-1" ; } catch (ioexception e1) { // todo auto-generated catch block return hostnamee + "|-1" ; } } } } and way create pool of threads in main class :
private void startthreadpool() { executorservice pool = executors.newfixedthreadpool(30); list<future<string>> list = new arraylist<future<string>>(); (map.entry<string, string> entry : pc_nameip.entryset()) { callable<string> worker = new clientconnect(entry.getkey(),entry.getvalue()); future<string> submit = pool.submit(worker); list.add(submit); } (future<string> future : list) { try { string threadresult; threadresult = future.get(); //........ process data here!..........// } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } } } the pc_nameip map contains (hostname, hostip) values , every entry create clientconnect thread object.
my problem when list of machines contains lets 10 pcs (which of them not alive), lot of timeout exceptions (in alive pcs) though timeout limit set 10 seconds.
if force list contain single working pc, have no problem. timeouts pretty random, no clue what's causing them.
all machines in local network, remote servers written (in c/c++) , been working in setup more 2 years without problems.
am missing or os network restriction problem? testing code on windows xp sp3. in advance!
update:
after creating 2 new server machines, , keeping 1 getting lot of timeouts, have following results :
for 100 thread runs on 20 minutes : new_server1 : 99 successful connections/ 1 timeouts new_server2 : 94 successful connections/ 6 timeouts old_server : 57 successful connections/ 43 timeouts other info : - experienced jre crash (exception_access_violation (0xc0000005)) once , had restart application. - noticed while app running network connection struggling browsing internet. have no idea if expected think having @ max 15 threads not much.
so, fisrt of old servers had kind of problem. no idea was, since new servers created same os image.
secondly, although timeout percentage has dropped dramatically, still think uncommon 1 timeout in small lan ours. server's application part problem.
finally point of view that, apart old server's problem (i still cannot beleive lost time that!), there must either server app bug, or jdk related bug (since experienced jre crash).
p.s. use eclipse ide , jre latest.
if of above ring bells you, please comment. thank you.
-----edit-----
could printwriter and/or bufferedreader not thread safe????!!!?
----new edit 09 sep 2013----
after re-reading comments , @gray , comment :
when run multiple servers first couple work , rest of them timeout? might interesting put small sleep in fork loop (like 10 or 100ms) see if works way.
i rearanged tree list of hosts/ip's , got strange results. seems if alive host placed on top of tree list, being first start socket connection, has no problem connecting , receiving packets without delay or timeout.
on contrary, if alive host placed @ bottom of list, several dead hosts before it, takes long connect , previous timeout of 10 secs failed connect. after changing timeout 60 seconds (thanks @ejp) realised no timeouts occuring!
it takes long connect (more 20 seconds in occasions). blobking new socket connections, , isn't hosts or network busy respond.
i have debug data here, if take : http://pastebin.com/2m8jdwkl
you check availability before connect socket. there answer provides kind of hackish workaround https://stackoverflow.com/a/10145643/1809463
process p1 = java.lang.runtime.getruntime().exec("ping -c 1 " + ip); int returnval = p1.waitfor(); boolean reachable = (returnval==0); by jayunit100
it should work on unix , windows, since ping common program.
Comments
Post a Comment