java - Why the server stops running when I close the Client? -
i writing first client-server program in java using sockets. using eclipse ide. when testing communication between both programs (server , client) run first server using command prompt , run client in eclipse. works fine, can read , write socket, however, when close client program in eclipse, server program closes too. why happening? server supposed running in command prompt, not dependent on client.
also know if there possibility can run both programs in eclipse instead of opening server in command prompt first.
here code both programs:
server:
import java.io.bufferedreader; import java.io.inputstreamreader; import java.io.printwriter; import java.net.serversocket; import java.net.socket; public class serverprg { public static void main(string[] args) { system.out.println("server running....."); try { serversocket socketser = new serversocket(4444); socket clientsocket = socketser.accept(); printwriter out = new printwriter(clientsocket.getoutputstream(), true); bufferedreader in = (new bufferedreader (new inputstreamreader(clientsocket.getinputstream()))); bufferedreader stdin = (new bufferedreader (new inputstreamreader(system.in))); system.out.println("client: " + in.readline()); string input ; while((input = stdin.readline()) != null) { out.println(input); system.out.println("client: " + in.readline()); } } catch (exception e) {system.out.println("can't create serversocket. port being used " + e);} } //end main } //end public class
client:
import java.io.bufferedreader; import java.io.inputstreamreader; import java.io.printwriter; import java.net.socket; public class clientprg { public static void main(string[] args) { int portnumber = 4444; try { socket clientsocket = new socket("127.0.0.1", portnumber); printwriter out = new printwriter(clientsocket.getoutputstream(), true); bufferedreader in = new bufferedreader(new inputstreamreader(clientsocket.getinputstream())); bufferedreader stdin = new bufferedreader(new inputstreamreader(system.in)); string input; while ((input = stdin.readline()) != null) { out.println(input); system.out.println("server: " + in.readline()); } } catch(exception e) { system.out.println(e); system.out.println("can't connect server"); } } //end main } // end public class
your server lacks loop around accepting client sockets.
this means after client socket accepted, exit because there no flow control element have attempt accept second client socket.
a simple loop around accepting client sockets not want. because there 1 thread in solution, means while client being handled, other clients won't able accepted.
there many ways handle situation above. 1 of simplest create thread every accepted client handle client's communications. while simple, not scale well. large number of clients, thread count rise, , computers can handle many more network connections threads.
the scope of talking services scale far big address here; but, after familiar 1 thread per client processing, start looking @ java nio.
Comments
Post a Comment