I am working on a chat application using java. One of the functionality that I want to implement is that beside client1 chat with client 2(which works fine), client1 will send secondary information (data from an Arduino) to client2. The idea that I have is listening to two ports on the server, one for the chat, the other for the secondary info, I used two ServerSockets and of course two accept() statements and they keep blocking. My question is how can I open two ports simultaneously in java? I started with something like this:
new Thread() {
public void run() {
while(true) {
try {
Socket client1 = server.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//handle client1
}
}}.start();
new Thread() {
public void run() {
while(true) {
try {
Socket client2 =arduinoServer.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//handle client1
}
}}.start();
But I got Exception in thread "pool-1-thread-1" java.lang.NullPointerException
Thanks.