Link to home
Start Free TrialLog in
Avatar of InteractiveMind
InteractiveMindFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ServerSocket#close() without closing Socket?

Hi, I figured that it would be easier to ask here, then test it out myself... :-)

If I were to do the following:

   ServerSocket srv = new ServerSocket( port );
   Socket sock = srv.accept();
   srv.close();    // Or reinitialize srv.

Will that also close the Socket connection??

Ta.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of InteractiveMind

ASKER

Ah, that's a shame. lol.

Cheers  :-)
;-)
Avatar of aozarov
aozarov

I don't think that is correct.
Closing the serverSocket will not close its active connections. (it will stop listening and clear is pending connection).
See this example:

import java.net.*;
import java.io.*;

public class TestServerSocketClose
{
      public static void main(String st[]) throws Exception
      {
            ServerSocket server = new ServerSocket(1929);

            new Thread()
            {
                  public void run()
                  {
                        try
                        {
                              Thread.sleep(1000);
                              Socket client = new Socket("localhost", 1929);
                              DataInputStream clientIn = new DataInputStream(client.getInputStream());
                              DataOutputStream clientOut = new DataOutputStream(client.getOutputStream());
                              for (int i = 0; i < 10; i++)
                              {
                                    clientOut.writeChars("hello world\n");
                                    clientOut.flush();
                                    Thread.sleep(1000);
                              }

                              client.close();
                        }
                        catch (Exception ex)
                        {
                        }
                  }
            }.start();


            Socket socket = server.accept();
            DataInputStream in = new DataInputStream(socket.getInputStream());
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            String line = null;      
            boolean wasClosed = false;
            while ((line = in.readLine()) != null)
            {
                  System.out.println(line);
                  if (!wasClosed)
                  {
                        System.out.println("Closing serverSocket....");
                        server.close();
                        wasClosed = true;
                        System.out.println("ServerSocket isClosed -> " + server.isClosed());
                  }
            }
      }
}
Oh.  -_^

Well, in that case; thank you! lol. :-)

Please see: https://www.experts-exchange.com/questions/21409873/Cheers-Aozarov.html

Thank you.