Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swing & ServerSocket & Thread

Status
Not open for further replies.

SValmont2000

Technical User
Nov 26, 2001
5
CA
Hi, I wrote a small Swing GUI to control my ServerSocket. I have the JFrame class as my main class, display the Frame and then start a new thread that creates the ServerSocket and has an infinite loop with accept() to wait for an incoming connection and then starts a new thread for every connection. Anyways, if the ServerSocket Thread is started and is in accept mode, my GUI is kind of frozen (it's waiting for the thread to end, i seems to me) Anyone has an idea for a workaround??? Thanks in advance.

S. V.
 
Okay,

When you do an accept(), the thread blocks until there's a connection.

However, you've done the accept() in the GUI thread, which you should NEVER block. This is why the GUI isn't responding.

What you need to do is create another thread in which to do the accept(). Since this thread will mostly be waiting for a connection, it won't take up much CPU time.

The only other way of doing it would be to use the nio package with Java 1.4, which is currently in beta. This allows proper asynchronous i/o.
 
I think I pretty much already did what you suggested. Mhm, strange. To recapitulate:
1. Create the GUI
2. If the Button is pressed, create a new object of my class ServermainThread, and run that thread.
This class starts a serversocket and goes into accept() mode then creates for every connection a new thread to handle that.

What is my problem? I already looked into the nio classes, it kind of worked but was extremely slow and chewed up an insane amount of mem. Any further suggestions???

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top