A
ales
Hi !
I have a thread waiting for incoming connections which then processes
data in its own way.
I want to stop and start thread manually using start() and close()
methods. The problem is, I don't know how to stop it in the middle of
accept() call.
The only way I can get out from accept is to connect localy to the same
port.
Any better idea ? Can I simulate IOException inside accept() ?
import java.net.* ;
import java.io.*;
public class MyServer extends Thread
{
private ServerSocket server = null ;
private boolean done = false ;
private int port = 0 ;
public MyServer(int port) throws Exception
{
this.port = port ;
this.server = new ServerSocket(port) ; // Starts server
}
public void run()
{
this.done = false ;
while (!this.done) {
Socket socket = this.server.accept(); // I want to kill this
if (!this.done) {
...processing socket
}
}
}
public void close()
{
this.done = true ;
try {
// This works but is lawzy
Socket s = new Socket("localhost", port) ;
} catch (Exception e) {}
}
}
I have a thread waiting for incoming connections which then processes
data in its own way.
I want to stop and start thread manually using start() and close()
methods. The problem is, I don't know how to stop it in the middle of
accept() call.
The only way I can get out from accept is to connect localy to the same
port.
Any better idea ? Can I simulate IOException inside accept() ?
import java.net.* ;
import java.io.*;
public class MyServer extends Thread
{
private ServerSocket server = null ;
private boolean done = false ;
private int port = 0 ;
public MyServer(int port) throws Exception
{
this.port = port ;
this.server = new ServerSocket(port) ; // Starts server
}
public void run()
{
this.done = false ;
while (!this.done) {
Socket socket = this.server.accept(); // I want to kill this
if (!this.done) {
...processing socket
}
}
}
public void close()
{
this.done = true ;
try {
// This works but is lawzy
Socket s = new Socket("localhost", port) ;
} catch (Exception e) {}
}
}