Closing Threads that create sockets

Posted on 2005-05-07
Last Modified: 2010-03-31

Using the code below to create threads for creating server scokects for receiving and sending messages to the clients. I would like to use a button to terminate the server, but not sure if I have to stop the threads or close the sockets. What is the best way to stop the server? What code should I use?


while (true)
            Socket userSocket = serverSocket.accept();
            textOutput.append ("\nNew Connection Accepted") ;
            ServerReceivingThread receive = new ServerReceivingThread (userSocket, msgs_data_structure, textOutput);      // create a thread for this user
            ServerSendingThread send = new ServerSendingThread (userSocket, msgs_data_structure, receive, textOutput);      // create a thread for this user
            receive.start();      // start a thread for this user
            send.start () ;
Question by:Cyart
    LVL 86

    Accepted Solution

    You might find it difficult to terminate a thread blocked in a read, but try to do it by closing streams and then the socket(s)
    LVL 15

    Assisted Solution

    >> I would like to use a button to terminate the server
    Closing the serverSocket will cause exception while in accept.
    So you can use this (or use accept with a timeout and check a "isClosed" flag)  to get out
    from the serversocket thread.
    Closing the serversocket will not effect current client sockets (and their threads).
    For terminating the clients threads you can willl need to keep a recored of them [or their sockets] (put them in some sort of collection)
    and then close each socket (this will cause IOException in their read/write operation which can help you to bail out).
    LVL 15

    Expert Comment

    >> I have to stop the threads ..
    Calling stop on the thread should do the job a well but that should be used as a last resort (see why it got deprecated: ) [And If think in your case is not needed]
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now