5

I'm trying to make a multithreaded server/client app with java ! this code is for listen() method in a class of a package that named Bsocket (iserver.core.socket) :

 try {
     serverSocket = new ServerSocket(port);
 }catch(IOException e ){
     ui.log(e.toString());//*
 }
while (true){
    try{
        clienSocket = serverSocket.accept();
        ui.log("Incomming Connection.");//*
        new connectionHandler(clienSocket, ui);
    }catch(IOException e ){
        ui.log(e.toString());
    }
}

ui.log("Incomming Connection."); is a method in below of main class of Bgui (iserver.core.ui).Bgui is a jframe that contain a textarea and something else ! the problem is when the accept methods executed , the ui.log did not works ! whats wrong here ?

bizzr3
  • 1,925
  • 4
  • 24
  • 37
  • The Question is fully informed !!! but some info's is optinal ! you like -1 huh ? :) ok ! look at `dty` answer! – bizzr3 Mar 26 '12 at 12:49
  • 1
    You should have a look at this [tutorial about SwingWorkers](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) – assylias Mar 26 '12 at 12:50
  • Heh, I undid the -1 once I figured out that you didn't know about how UI systems worked. I got confused by your talking about log output and jframes. – Gray Mar 26 '12 at 13:12
  • See also this [example](http://stackoverflow.com/a/3245805/230513). – trashgod Mar 26 '12 at 14:14

3 Answers3

8

You will need to launch your server on a seperate thread since .accept is a blocking call. You might want to do something like so:

(new Runnable() {
    @Override
    public void run()
    {
         try {
              serverSocket = new ServerSocket(port);
          }catch(IOException e ){
              ui.log(e.toString());//*
          }
         while (true){
             try{
                 clienSocket = serverSocket.accept();
                 ui.log("Incomming Connection.");//*
                 new connectionHandler(clienSocket, ui);
             }catch(IOException e ){
                 ui.log(e.toString());
             }
         }
    }
}).start();

NOTE: This code is not tested, but it should give you an idea of what you need to do.

npinti
  • 51,780
  • 5
  • 72
  • 96
7

Socket.accept() blocks until there's an incoming connection to receive (see the documentation). You shouldn't be making any blocking calls from your UI thread - otherwise it will... you know... block!

dty
  • 18,795
  • 6
  • 56
  • 82
5

You need to separate UI threads from your own network service threads. accept() is blocking (obviously) and it freezes your application until you get a new client, and freezes again waiting for more clients.

logoff
  • 3,347
  • 5
  • 41
  • 58
  • can you tell me more about separating this layers ? – bizzr3 Mar 26 '12 at 12:52
  • 1
    this is a short question with a really large response. you need to learn some concepts about concurrency. it is very important developing UI applications. Oracle provides some interesting [tutorials related to concurrency in Swing applications](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/). – logoff Mar 26 '12 at 12:57
  • 2
    To be fair, it doesn't "freeze your application", it just blocks the single thread that calls it. All other threads are free to do their own thing. – dty Mar 26 '12 at 13:18
  • @dty your response is true. the application freezes as a consequence of blocking the UI thread with socket.accept() method. – logoff Mar 26 '12 at 14:27