Runnable interface for stopping blocking method

Hi All,

All i want is to be able to stop the listening for incomming connections whenever i run the stop method.
But somehow I dont even get there.

Main.java
package globalchat;
public class Main {
    public Main() {
    }
    public static void main(String[] args) {
        ChatServer lChatServer = new ChatServer();
        lChatServer.Start();
        lChatServer.Stop();
    }    
}

ChatServer.java
package globalchat;

import java.io.*;
import java.net.*;
public class ChatServer implements Runnable
{
    private ServerSocket prServerSocket = null;

    public ChatServer()
    {
    }
   
    public void Start()
    {
        Thread th = new Thread(this);
        th.start();
    }
    public void Stop()
    {
        try
        {
            prServerSocket.close();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }

    public void run()
    {
        System.out.println("why does not the code run past this point?");
        try
        {
            prServerSocket = new ServerSocket( 9999, 0, InetAddress.getByName( "127.0.0.1" ) );
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        while ( true )
        {
            try
            {
                Socket lSocket = prServerSocket.accept();
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}


In the run method which is ran on thread start It never comes into the try block. for some reason it simply ignores that whole section.
WHat am I doing wrong?
LVL 2
DaFouAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
DO you get an exception thrown?
0
DaFouAuthor Commented:
no, not in the run method.

but once in the Stop method a null reference exception is thrown on this line
prServerSocket.close();

this is logical becuase the prServerSocket never gets set in the run method.
0
CEHJCommented:
Try

prServerSocket = new ServerSocket( 9999);
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

DaFouAuthor Commented:
u sure?

I must be able to set the IP address on which it is listening and also this code:
prServerSocket = new ServerSocket( 9999, 0, InetAddress.getByName( "127.0.0.1" ) );

runs perfectly if I do not place it inside the run method
0
CEHJCommented:
Try making the Thread an instance variable
0
DaFouAuthor Commented:
i tried your suggestion. made no difference what so ever. please copy paste my code into your IDE. this problem is really weird.
0
DaFouAuthor Commented:
instance variable as in
private Thread prThread = null; in the ChatServer class

and

prThread = new Thread(this);
prThread.start();

in the Start method?/ if so  it also did not make any difference.
0
CEHJCommented:
                 prServerSocket = new ServerSocket(9998, 0, InetAddress
                              .getByName("127.0.0.1"));
                  System.out.println("It DOES!");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DaFouAuthor Commented:
i think you are right, i think i am running the Stop method before the run method gets a chance to actually do something.
0
CEHJCommented:
:-)

Try sleeping the main thread for a couple of ticks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.