For a java application:Why isn’t there possible to start two different threads in the same main???
One thread that is listening form incoming connections to a port and another thread that listens for other connections to a totally different port???
Something like:
public static void main(String[] args) {
// TODO Auto-generated method stub
ClientThread clienThread= new ClientThread();
new Thread(clienThread).start();
ThreadPooledServer server = new ThreadPooledServer(6000);
new Thread(server).start();
}
Where ClientThread() and ThreadPooledServer() are two different threads!
Question:Why doesn’t java let me do that?
public class Start {
Socket socket;
private String serverIpAddress="127.0.0.1";
static Thread cThread;
public static void main(String[] args) {
// TODO Auto-generated method stub
ClientThread clienThread= new ClientThread();
new Thread(clienThread).start();
ThreadPooledServer server = new ThreadPooledServer(6000);
new Thread(server).start();
}
}
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, 6100);
System.out.println("s-a creat");
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
} catch (IOException e) {
System.err
.println("Couldn't get I/O for the connection to host");
}
}
}
public class ThreadPooledServer implements Runnable {
protected int serverPort =6000;
public static String SERVERIP = "127.0.0.1";
protected ServerSocket serverSocket = null;
protected boolean isStopped = false;
protected Thread runningThread = null;
public static int clientconnection = 0;
Vector<WorkerRunnable> workerList = null;
protected ExecutorService threadPool =
Executors.newFixedThreadPool(5);
public ThreadPooledServer(int port) {
this.serverPort = port;
workerList = new Vector<WorkerRunnable>();
}
public void run() {
synchronized (this) {
this.runningThread = Thread.currentThread();
}
openServerSocket();
while (!isStopped()) {
Socket clientSocket = null;
try {
System.out.println("Serverul asteapta clienti spre conectare");
clientSocket = this.serverSocket.accept();
clientconnection++;
System.out.println("Serverul a acceptat clientul cu numarul:"
+ clientconnection);
// Log.d("Server:","s-a acceptat un nou client");
} catch (IOException e) {
if (isStopped()) {
System.out.println("Server Stopped.");
return;
}
throw new RuntimeException("Error accepting client connection",
e);
}
// creare thread pt noul client conectat
//WorkerRunnable workerRunnable = new WorkerRunnable(clientSocket);
//Thread workerThread = this.threadPool.execute(new WorkerRunnable(clientSocket));
//workerThread.start();
this.threadPool.execute(new WorkerRunnable(clientSocket));
//this.workerList.add(workerRunnable);
}
this.threadPool.shutdown();
System.out.println("Server Stopped.");
}
private synchronized boolean isStopped() {
return this.isStopped;
}
public synchronized void stop() {
this.isStopped = true;
try {
this.serverSocket.close();
// stop thread-uri workers
for (int i = 0; i < this.workerList.size(); i++) {
WorkerRunnable worker = this.workerList.elementAt(i);
worker.stop();
}
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
private void openServerSocket() {
try {
InetSocketAddress serverAddr = new InetSocketAddress(SERVERIP,
serverPort);
serverSocket = new ServerSocket();
serverSocket.bind(serverAddr);
} catch (IOException e) {
throw new RuntimeException("Cannot open port 6000", e);
}
}
}
public class WorkerRunnable implements Runnable {
protected Socket clientSocket = null;
Scanner s = null;
String longitude;
String latitude;
boolean stop = false;
public WorkerRunnable(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public void run() {
try {
System.out.println("O noua conexiune acceptata"
+ clientSocket.getInetAddress() + "cu portul"
+ clientSocket.getPort());
Scanner is = new Scanner(new DataInputStream(
this.clientSocket.getInputStream()));
while (!stop) {
while (is.hasNext()) {
try {
longitude = is.next();
latitude = is.next();
System.out.println(longitude);
} catch (Exception e) {
e.printStackTrace();
}
}
}
is.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stop() {
this.stop = true;
}
This is all I have!!!
EDIT:I moved the ClientThread code into a different class and I succeded to run my code,but now I can’t get to create two sockets.
I mean it creates one but for the second one I get:
“Couldn’t get I/O for the connection to host”
I assume that is coming from here:
catch (IOException e) {
System.err
.println("Couldn't get I/O for the
connection to host");
}
Does someone knows why??
This has nothing to do with threads or sockets. The error you are getting is described here. Without seeing your code I’d say that the classes you are trying to instantiate are inner classes of Start. You don’t have an instance of Start because you’re in its static main() method.
try something like: