In java network programming, is there a way to keep the Server side open even when the Client side shuts down? -
suppose have simple echo client/server pair in java. understand it, once 1 side of socket breaks whole connection gone.
but if want server can stay alive, if client side dies. want able resume broken connection.
echo server :
import java.net.socket; import java.net.serversocket; public class echoserver { public static void main(string[] args) throws exception { // create socket int port = 4444; serversocket serversocket = new serversocket(port); system.err.println("started server on port " + port); // repeatedly wait connections, , process while (true) { // "blocking" call waits until connection requested socket clientsocket = serversocket.accept(); system.err.println("accepted connection client"); // open io streams in in = new in (clientsocket); out out = new out(clientsocket); // waits data , reads in until connection dies // readline() blocks until server receives new line client string s; while ((s = in.readline()) != null) { out.println(s); } // close io streams, socket system.err.println("closing connection client"); out.close(); in.close(); clientsocket.close(); } } } any tips appreciated
what you're missing concept of 'session'. think position of server, when bits arrive 'on wire'. these? tcp/ip, there information present on wire, namely:
- src addr
- src port
- dest addr
- dest port
- and message payload itself
- and other stuff (like sequence counters, ensure 'packets' not jumbled during transmission)
the operating system of server uses src/dest addr/port information decide if 'a conversation in progress' or not. considers dest port (since message made machine itself, through internet , firewall) decide if java program listening on 'dest port'. however, uses entire src-addr/src-port/dest-addr/dest-port try , deliver payload program in order sender sent them (which might not order in arrived, because of intervening inter-net). notice single 'message' might split multiple packets. operating system's tcp/ip stack doing fair bit of work you.
however, notice in order perform function on behalf, operating system has devote amount of resources 'tracking state' of tcp/ip session. @ least, each set of port/addr src/dest, there needs counter of last well-received 'packet', buffer space hold packets until program ready consume them, , forth.
now question facing tcp/ip stack implementor "how long should 'hang on' state for"? 10 seconds enough? 20 minutes? @ point, after not hearing client while, session must 'timeout'. if there more packets send in sequence, , client starts sending them again, server has able "sorry, you're sending me packet 234 of previous message, because didn't hear while, threw out packets 1-233. start again?".
so in sense, there's no way prevent client 'disconnecting'. sure, can keep listening on socket, in case client recovers , sends more data. , client need way 'pick left off'.
in http, that's achieved using 'session cookie' - long unique string server gave client, , client re-sends each fresh request (whether happens on same tcp-level session or not). 'application level session' has lifetime longer of tcp session.
since you're writing application, , sounds have degree of control on client , server each other (the 'protocol'), have more options how agree on session, how deal things if client 'goes away' (timeout session), , how both sides recover , 'pick left off' (re-establish session). don't forget authentication you!
as others have said, depends great deal on you're trying achieve.
Comments
Post a Comment