?
Solved

Closing Threads that create sockets

Posted on 2005-05-07
4
Medium Priority
?
197 Views
Last Modified: 2010-03-31
Hello,

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?

Cheers


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 () ;
            }
0
Comment
Question by:Cyart
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 375 total points
ID: 13950299
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)
0
 
LVL 15

Assisted Solution

by:aozarov
aozarov earned 375 total points
ID: 13951360
>> 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).
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13951377
>> 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: http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html ) [And If think in your case is not needed]
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14054395
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

807 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