I am making a simple telnet implementation in Java from ground up.
I have already made a simple socket connection between client and server work. My problem is just that the whole server application freezes when it is waiting for a connection – even though i am running it in a seperate thread. Is there any (preferably) simple way to get around this?
Thread starter snippet:
worker slave = new worker();
Thread slaveThread = new Thread(slave);
slaveThread.run();
Thread snippet:
public class worker implements Runnable{
public void run()
{
try
{
ServerSocket srv = new ServerSocket(1337);
System.out.println("Thread is running!");
Socket clientSocket = srv.accept();
System.out.println("Connection made.");
}catch (IOException e){
System.out.println("Failed.");
}
Thanks in advance!
Java Newbie
Although Thread implements Runnable, you aren’t supposed to call
run(). You should call Thread.start(), which callsrun()in the new thread. If you callrun()directly, the current thread is the one that executes it, not the Thread you created.