Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServerSocket crash with Thread

 
Jedrzej Pow
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

I write out a simple messaging client / server. I have already written a console application, which works fine but I wanted to write desktop application.
And there is a problem because when I create a connection to the program crashes(freeze) and I could only determine what portion of code that causes. I can say that the same code in the console is working properly.

Sorry for my english.

console code:


desktop application:


Please indicate the error and any instructions.
 
Farhad Rahmati
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it looks fine, but you don't have to run it with Thread.

Try it out of thread and runnable interface

I will work
 
Jedrzej Pow
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Already been written without the Thread and Runnable, and it did not work. The program also crashes.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that to "crash" means to exit abruptly, so the program's no longer running. To "freeze" means to stop working, so that the program won't respond to input anymore. Your program is freezing, not crashing!

Anyway: accept() waits for a new connection to come in before returning, right? Well, you're using invokeLater() to call accept() directly on the event-processing thread, which is also responsible for painting the screen and responding to mouse clicks. As long as accept() is waiting for a connection, your GUI will be frozen. Never call any method that will take a long time to run on the event thread, or it will freeze your GUI.

Even worse, you're running it on the event thread many, many, many times! invokeLater() returns immediately, so your while loop is adding calls to accept() onto the event thread at the rate of many thousands per second! Finally, note that you don't need to -- in fact, can't! -- create a new ServerSocket for each accept() call; you should create just one, and call accept on it in a loop, once for each client.

Anyway, to fix your problem: don't call accept() on the event loop! It looks like you already know how to define a Runnable and create a thread. Do that -- create a separate thread just for calling accept() and servicing clients. Create the ServerSocket outside of that loop.
 
Jedrzej Pow
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I followed your advice and my application is not freezing. But now I do not get the message that the new client has connected.
1. I do not know how to find outside the thread that the client initiated the connection. Is there any way to find out.
2. Maybe I made sth wrong in the thread initialization and jTextArea1 doesn't get message.

New code :


Thread:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have all the right code here, but it's not necessarily in the right place. The line that updates the textarea should execute on the event thread, so you should use SwingUtilities.invokeLater() for that. On the other hand, the code where you've asked "//HOW IT CAN BE CHECKED", that should move onto the network thread. The best way to know about a client connecting is just to put the code right after accept() returns, right?

Now, actually, the way this is often done is you have one thread that calls accept() in a loop; whenever accept() returns, yet another thread is created to service that one client (passing the Socket as a constructor argument to the new thread.) That way, the accept() loop can call accept() again right away while that first client is being processed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic