?
Solved

ServerSocket#close() without closing Socket?

Posted on 2005-05-01
5
Medium Priority
?
326 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:InteractiveMind
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 13904501
Yes it will ;-)
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13904507
Ah, that's a shame. lol.

Cheers  :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13904513
;-)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13911411
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
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13911482
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question