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?
 
CEHJConnect With a Mentor Commented:
                 prServerSocket = new ServerSocket(9998, 0, InetAddress
                              .getByName("127.0.0.1"));
                  System.out.println("It DOES!");
0
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJCommented:
Try

prServerSocket = new ServerSocket( 9999);
0
 
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
 
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
All Courses

From novice to tech pro — start learning today.