sockets - How to add heartbeat messaging on top of this Java code( for KnockKnockClient/Server)? -
i'm studying following basic java socket code( source ). it's knock-knock-joke client/server app.
in client, set socket usual:
try { kksocket = new socket("localhost", 4444); out = new printwriter(kksocket.getoutputstream(), true); in = new bufferedreader(new inputstreamreader(kksocket.getinputstream())); } catch( unknownhostexception uhe ){ /*...more error catching */
and later, read , write server:
bufferedreader stdin = new bufferedreader(new inputstreamreader(system.in)); string fromserver; string fromuser; while ((fromserver = in.readline()) != null) { system.out.println("server: " + fromserver); if (fromserver.equals("bye.")) break; fromuser = stdin.readline(); if (fromuser != null){ system.out.println("client: " + fromuser); out.println(fromuser); }
and on server, have corresponding code, joke punch-line.
knockknockprotocol kkp = new knockknockprotocol(); outputline = kkp.processinput(null); out.println(outputline); while ((inputline = in.readline()) != null) { outputline = kkp.processinput(inputline); out.println(outputline); if (outputline.equals("bye.")) break;
i want attach heartbeat whole thing, print out console whenever detects other side died. because happens if kill other side exception - 1 below:
so if running both knockknockclient , knockknockserver, shut down knockknockserver, should happen on client see outputted:
>the system has detected knockknockserver aborted
i'm looking tips. far i've been trying run daemon thread periodially creates new connections other side. i'm confused condition check for(but think it's boolean
value?). right approach? found out online there's library called jgroups multicast networking - better way? i'm looking tips.
my server-code far(sorry it's messy)
&
thanks
but exception getting this! it's telling other side died. catch exception , print console, "the system has detected knockknockserver aborted".
you using tcp connection , tcp has built-in harthbeat (keepalive) mechanism you. set setkeepalive() on socket. being said - possible control keepalive frequency per each connection, not know how in java.
Comments
Post a Comment