Hello. Im trying to make application which have server and to the server can connect many clients. So I just started doing it and I have problem. When I run my server it waits until client connects to server, so if I run client and server got connection, then they send each other a message and disconnecting both. Client can disconnect, I do not care about it now. Now I want to fix server. And I think that I must to tell server that he waits for my command to disconnect because now he disconnects without my command. Here is my server code:
Maybe I must to make it somehow that it disconnects on application close? Or do you have any other suggestions?Code:ServerSocket myServerSocket = new ServerSocket(9999);
System.out.println("Server is waiting for an incoming connection on host="
+ InetAddress.getLocalHost().getCanonicalHostName()
+ " port=" + myServerSocket.getLocalPort());
Socket skt = myServerSocket.accept();
BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
PrintStream myOutput = new PrintStream(skt.getOutputStream());
String buf = myInput.readLine();
if (buf != null) {
System.out.println("Server read: [" + buf + "]");
myOutput.print("got it");
}
//SERVER GOT MESSAGE FROM CLIENT
//AND CLOSE THE CONNECTION
skt.close();
System.out.println("Server is exiting!");
