0

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.

achref
  • 1,150
  • 1
  • 12
  • 28
  • A solution with a [Selector](http://docs.oracle.com/javase/7/docs/api/java/nio/channels/Selector.html) will probably be the best. – Volune Sep 07 '14 at 01:48

1 Answers1

0

Of course they keep blocking. accept() is a blocking method.

You need two accept() loops, in two threads.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Didn't think of using threads, I am going to try this now, can you explain to me what is an accept() loop? you mean writing the accept() statement in an infinite loop so it never blocks, if it is the case, isn't it a bad approach to use infinite loops? – achref Sep 07 '14 at 01:51
  • No, I don't mean 'writing the accept() statement in an infinite loop so it never blocks'. It *does* block, and I already said so. You have to put it in a loop *so you can accept more than one connection.* I don't know where you got your strange theory about infinite loops from. – user207421 Sep 07 '14 at 02:09
  • Didn't really understood what you mean, I saw examples on the internet on how putting it in a loop, but there is only one accept statement, once I add another accept it blocks, for the theory, I read it in this book "Refactoring: Improving the Design of Existing Code" by Martin Fowler in which he discourage the usage of infinite loops. – achref Sep 07 '14 at 02:20
  • I edited my code to know if you meant something like it – achref Sep 07 '14 at 02:30
  • Could you possibly try reading what I wrote? 'Two accept loops in two threads'. Not two accept() calls in one loop in one thread. – user207421 Sep 07 '14 at 03:26