wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Problem: (Simple program) server doesn't display/read message from client!? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Problem: (Simple program) server doesn Watch "Problem: (Simple program) server doesn New topic
Author

Problem: (Simple program) server doesn't display/read message from client!?

Saleh Feek
Greenhorn

Joined: Sep 19, 2012
Posts: 14
Hi guys

I wrote a simple server and client GUI classes,..

Up to now I reached to a stage that supposed to work correctly, But the problem is that the server doesn't display received text from client.

server GUI:
- Run the server.
- You provide it the port number (you are free to choose say 4444).
- Then click "Connect".
- The server will wait for client connection.

Client GUI:
- Run the client.
- You provide it the "local host" it is supposed to be loop back address and it is 127.0.0.1
- And the port that you chose for the server (say 4444).
- Click "set".
- Type text in the text field labelled "text to send" .
- Click "send".

Now it is supposed that text will be displayed on the server JTextArea, but nothing happen.

What I missed. I think the problem is in the method "processClient()" of server class.

This is the code of both classes, the complete code that you can copy and run:

Server:



Client:
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

My guess is that in the server,

reads all characters up to and including a line-ending character before it returns.

And my guess is that the client sends data without a line-ending character.

The result of this is that the server would wait indefinitely for a line-ending character which never arrives.
Saleh Feek
Greenhorn

Joined: Sep 19, 2012
Posts: 14
Paul Clapham wrote:My guess is that in the server,

reads all characters up to and including a line-ending character before it returns.

And my guess is that the client sends data without a line-ending character.

The result of this is that the server would wait indefinitely for a line-ending character which never arrives.


Thank you Paul Clapham for trying the program and take time to see what is the problem :)
I was trying to detect the source of the problem, and I discovered the problem before I read your appreciated reply.

I will put the solution so that other readers interested in "networking+GUI" take attention to such a problem

The problem is a GUI problem rather than logic or networking problem. So the logic is good but the way GUI respond to it make the problem to occur. The problem is that the button "connect" in the server class is responsible for triggering the method "run()", the method run has a while loop that will never end; while(true), the run method is responsible for a waking server, and it in turn contains the method processClient that has an infinite loop.

Ok,, The button "connect" when clicked will be frozen till event finish processing, hence the whole server GUI become frozen till events finished, and that will not happen as long as the server is running.

I made different strategy
- Avoid the looping work from GUI, and move it to the constructor.
- This enforce me to set the port number for the server on creation of the server that is in the constructor. So I set it to 4444

For beginners "like me" I will put the working code:

The Server:



TheClient:


I know it is not a good client server application because it need more improvements, at the meantime it is one-way communication. client can send to server but not the other way.

To run Server:
server GUI:
- Run the server. (just run)

To run the client:
- Run the client.
- You provide it the "local host" it is supposed to be loop back address and it is 127.0.0.1
- And the port 4444 (You can change it before working in the server class code)
- Click "set".
- Type text in the text field labelled "text to send" .
- Click "send".


Thank you




Saleh Feek
Greenhorn

Joined: Sep 19, 2012
Posts: 14
Based on the previous reply (my reply). I have a question

How to make a button invoke a method that has a loop, and prevent the GUI from freezing that way. Because I have some ideas that require setting at run time and keep it work without freezing.

Thank you



Saleh Feek
Greenhorn

Joined: Sep 19, 2012
Posts: 14
One thing I need to fix. I want to make the server GUI instance close and make all operations closed. I tried many code tricks but that doesn't stop the server from running. I will put the code for the server, you can copy and run. It is running correctly but cannot be stopped running behind the scenes (after the GUI disappear due close button).

Here is the working code for the server; I also provide the other required classes to interact with client. The problem is in the server class alone.


If you want to try the server. it requires the client class:


The server connector class:



To run things:

- Run the Server class. (don't press "send" - because it is not connected to any client yet).
- Run the ServerConnector class
- input 127.0.0.1
- click "set"
- start chat with yourself.

Now when close the client and serverconnector, related things stop running (what expected), but when closing the server it is continue running behind the scenes.

Thank you.
 
GeeCON Prague 2014
 
subject: Problem: (Simple program) server doesn't display/read message from client!?