2

I have to create an application having a GUI. my application has to work as a server. When it starts, it has to accept all the incoming connection and write the output in a JTextArea. my problem is where I have to create the ServerSocket ss = new ServerSocket(port_number) and the method ss.accept in the way I can accept connections. I tried to create in the main constructor of my gui but being ServerSocket anI/O request the gui stucks.some idea to resolve the solution?

I create in the constructor of my GUI:

SwingUtilities.invokeLater(new Runnable(){public void run(){connection();}});

where connection() is the method where I create the serversocket and accepts call

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mazzy
  • 13,354
  • 43
  • 126
  • 207
  • You should separate the functionality of the server from the gui. Gui operations need to run in something called the `EventDispatchThread` any server-like operations or other work should be done in a separate thread(s) – Hunter McMillen Jan 15 '12 at 16:53
  • 2
    @Mazzy with same suggestion as in your last question, use SwingWokrer – mKorbel Jan 15 '12 at 16:55
  • 1
    Exactly -- you should already know the answer to this! – Hovercraft Full Of Eels Jan 15 '12 at 16:56
  • Unfortunately I don't have the idea how to use SwingWorker.I tried to read the api but I don't understand how use process,done,ecc... – Mazzy Jan 15 '12 at 16:58
  • 1
    @Mazzy: but you should already know that it's necessary to solve this type of problem, so why waste your time and ours asking this question? Instead you should be focusing on getting SwingWorkers to work, and failing that, asking about SwingWorkers here. – Hovercraft Full Of Eels Jan 15 '12 at 17:03
  • @Mazzy that big mistake opening Connection wrapped inside invokeLater, don9t do that, this is example fror SwingWorker, please to check http://stackoverflow.com/a/6186188/714968, there are four separated thread, if you have problem with SwingWorkers logics – mKorbel Jan 15 '12 at 17:14
  • For reference, there's a complete example [here](http://stackoverflow.com/a/3245805/230513). – trashgod Jan 15 '12 at 17:18
  • Possible duplicate of [Socket using in a swing applet](http://stackoverflow.com/questions/3244400/socket-using-in-a-swing-applet) – trashgod Jan 15 '12 at 17:19
  • 1
    `I don't have the idea how to use SwingWorker` - See the tutorial on [Concurrency](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) for working examples. – camickr Jan 15 '12 at 17:21

2 Answers2

3

You should create a separate thread to wait/handle the network connections.

When a new connection comes in read the data and pass them to the EDT to update the GUI.

This way the GUI will be responsive.

You should read about MVC Pattern threads. If you Google there is an abundance of articles to study

UPDATE:

Your code here is wrong.

SwingUtilities.invokeLater(new Runnable(){public void run(){connection();}});

You are handling the connection from the EDT thread.
You should use this to update the GUI and not to call the network I/O code.

Cratylus
  • 52,998
  • 69
  • 209
  • 339
3

The IO logic must be in (at least one) separate background thread. Each time something must be printed to the text area from one of those background threads, they should do it using SwingUtilities.invokeLater(), to make sure that the Swing components are only accessed from the event dispatch thread.

That said, I don't think it's a very good idea to have a GUI for a server. Why don't you simply write in a log file, and use any text editor to see what the server received. Or write the GUI of the server as a client of this server?

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • +1 for the logging suggestion. I recently used a log4j appender which was capable of outputting the log messages in a text area. You want something similar here. Your application should be able to run without UI, and if needed/wanted, show the UI with the messages. Although a simple `tail` on the console would be as efficient – Robin Jan 15 '12 at 17:10