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.
LVL 25
InteractiveMindAsked:
Who is Participating?
 
CEHJCommented:
Yes it will ;-)
0
 
InteractiveMindAuthor Commented:
Ah, that's a shame. lol.

Cheers  :-)
0
 
CEHJCommented:
;-)
0
 
aozarovCommented:
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());
                  }
            }
      }
}
0
 
InteractiveMindAuthor Commented:
Oh.  -_^

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

Please see: http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21409873.html

Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.