Results 1 to 2 of 2
  1. #1
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Server socket won't wait

    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:
    Java 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!");
    Maybe I must to make it somehow that it disconnects on application close? Or do you have any other suggestions?
    Last edited by Daslee; 03-16-2012 at 10:58 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: Server socket won't wait

    Use a loop in the server to keep it from exiting until you give it a command to exit the loop.

Similar Threads

  1. non-blocking SSL socket server
    By e_scape in forum Networking
    Replies: 0
    Last Post: 04-12-2011, 05:18 PM
  2. events on a server socket
    By newbiejava in forum New To Java
    Replies: 13
    Last Post: 08-03-2010, 08:24 AM
  3. Server Socket
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 09-05-2009, 06:08 AM
  4. Replies: 4
    Last Post: 02-02-2009, 04:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •