[Logo]
Forums Register Login
Server accept both connections but print messages only from one.
So, my simple server without frame takes messages from both clients but this server with frame take messages only from one. It gives no errors and even when i close 1 client , the server still doesn't take messages from another one.
Can anyone explain what to do? I think it's something wrong with threads but im new in those things.


MainClass of Server which runs everything:


ServerFrame class:


ServerFrameHandler (Thread):

ServerFrameHandler  fixed While loop. It wasn't needed. So here is a valid code.Sorry




 

Arek Wróbel wrote:ServerFrameHandler  fixed While loop. It wasn't needed. So here is a valid code.Sorry






NEVERMIND IT DOES NOT FIXE. REVERT GUYS TO 1 POST!
Why there is no edit button ?

Swing isn't thread safe. You can't just change swing components in any thread. It may work, but it is not guaranteed to do so. Take a look at the SwingWorker and SwingUltilities classes to get the EDT (event dispatching thread) to make changes to Swing components.

Henry
 

Henry Wong wrote:
Swing isn't thread safe. You can't just change swing components in any thread. It may work, but it is not guaranteed to do so. Take a look at the SwingWorker and SwingUltilities classes to get the EDT (event dispatching thread) to make changes to Swing components.

Henry


Ok, thank you for information. I will look into it.
 

Arek Wróbel wrote:Why there is no edit button ?



Due to abuse, mostly by spammers, editing requires trust on the ranch. This can be done by earning enough cows. Or if you like, on request for a specific post, a moderator can enable it for you.

Henry
How are you trying to debug the code?  I don't see enough print statements to show where the code is executing and what the values of variables are as the code executes.
 

Norm Radder wrote:How are you trying to debug the code?  I don't see enough print statements to show where the code is executing and what the values of variables are as the code executes.


I didn't try to debug it.
Everything works fine except that text from client 2 or  more doesn't show up at TextArea.
 

I didn't try to debug it.

I think you should.
Debugging code that does not work is a very frequent part of a programmer's job.  It is a necessary skill.

I use print statements that show where the code is executing and what the values of variables are as the code executes.  Knowing what is happening when the code executes helps you find the problem so it can be fixed.


Does the server handle more than one client?  What does the server do while handling a client?  How does it get ready for the next client?
 

Norm Radder wrote:

I didn't try to debug it.

I think you should.
Debugging code that does not work is a very frequent part of a programmer's job.  It is a necessary skill.

I use print statements that show where the code is executing and what the values of variables are as the code executes.  Knowing what is happening when the code executes helps you find the problem so it can be fixed.


Stupid question but how to add those print statetmens, what code? I dont understand , sorry...
 

how to add those print statetmens,


The code has some already at lines 118 and 120.  Add more of the same in each method to show where the execution flow goes.

If the print statement does not print anything you know that the code was not executed.
 

Norm Radder wrote:

I didn't try to debug it.

I think you should.
Debugging code that does not work is a very frequent part of a programmer's job.  It is a necessary skill.

I use print statements that show where the code is executing and what the values of variables are as the code executes.  Knowing what is happening when the code executes helps you find the problem so it can be fixed.


Does the server handle more than one client?  What does the server do while handling a client?  How does it get ready for the next client?




Server handle more than one client , and when the client sends a message it print's "Message delivered from client" but text doesnt show up in text area from more than first client.
Here is a code for that:

What is printed on the console when the program is executed?  
Where does the second client send a message that the server does not receive?
 

Norm Radder wrote:What is printed on the console when the program is executed?  
Where does the second client send a message that the server does not receive?


When the program is executed it prints:
"Server waiting for connection"
When someone connect it prints:
"Server waiting for connection
Connection accepted from Socket[addr=/127.0.0.1,port=52492,localport=5000]
Server waiting for connection"
When another one connect it prints:
"Server waiting for connection
Connection accepted from Socket[addr=/127.0.0.1,port=52492,localport=5000]
Server waiting for connection
Connection accepted from Socket[addr=/127.0.0.1,port=52493,localport=5000]
Server waiting for connection"

The client sends messages through Scanner and save message to String then DataOutputStream sends it to the server where is recieved and printed to chatArea's textfield using append method.
OK that's a start.  What happens after this is printed:


Connection accepted from ...


What does the new instance of ServerFrameHandler do?  Add some print statements so you can see.
 

Norm Radder wrote:OK that's a start.  What happens after this is printed:


Connection accepted from ...


The object is created and method is invoked.


After that a new Thread is created in class SwingServerFrameHandler:



Code for handleServer():


 

Arek Wróbel wrote:

Norm Radder wrote:OK that's a start.  What happens after this is printed:


Connection accepted from ...


The object is created and method is invoked.


After that a new Thread is created in class SwingServerFrameHandler:



Code for handleServer():



Im sorry there should be ServerFrameHandler ServerFrameHandler = new ServerFrameHandler(clientSocket, chat_Field);
instead of
SwingServerFrameHandler swingServerFrameHandler = new SwingServerFrameHandler(clientSocket, chat_Field);
           


New instaces is "Creating constructor in ServerFrameHandler"




Then starts the run method if i am right ( im newbie so...)
 

Then starts the run method if i am right


Add some print statements to show where the execution is doing.  If values are read from a stream, print them.

No need to copy and paste another copy of the code.  One is enough.  What is needed is a copy of the console containing what was printed so we can see where the code was executing.

And if nothing was printed, we know that the execution hung or was blocked before it got to that print statement.
 

Norm Radder wrote:

Then starts the run method if i am right


Add some print statements to show where the execution is doing.  If values are read from a stream, print them.

No need to copy and paste another copy of the code.  One is enough.  What is needed is a copy of the console containing what was printed so we can see where the code was executing.

And if nothing was printed, we know that the execution hung or was blocked before it got to that print statement.



run:
Server waiting for connection
New client connected.Connection accepted from Socket[addr=/127.0.0.1,port=52542,localport=5000]
Creating constructor in ServerFrameHandler
Server waiting for connection
Message delivered from client
Actual value of msg_To_Read is Hey Norm! This is message from first client and it will be printed in text area.
New client connected.Connection accepted from Socket[addr=/127.0.0.1,port=52543,localport=5000]
Creating constructor in ServerFrameHandler
Server waiting for connection
Message delivered from client
Actual value of msg_To_Read is Hey again! This is message from second client and it looks like everyone is ok but it doesn't show up in text area.
everything*
Ouuh, sorry for those mistakes.
Updated now shows what is actual value of ChatArea where i want to text show up.

run:
Server waiting for connection
New client connected.Connection accepted from Socket[addr=/127.0.0.1,port=52551,localport=5000]
Creating constructor in ServerFrameHandler
Server waiting for connection
Message delivered from client
Actual value of msg_To_Read is Hey Norm! This is message from first client and it will be printed in text area.
Actual value of chat_Field is :Hey Norm! This is message from first client and it will be printed in text area.

New client connected.Connection accepted from Socket[addr=/127.0.0.1,port=52553,localport=5000]
Creating constructor in ServerFrameHandler
Server waiting for connection
Message delivered from client
Actual value of msg_To_Read is This is message from second client and it looks like everyone is ok but it doesn't show up in text area.
Actual value of chat_Field is :This is message from second client and it looks like everyone is ok but it doesn't show up in text area.  

Now we know that text area changes when second client sends a message but it doesn't show up in this area.
Updated Pointers, now show where the method that accept input from clients starts:


run:
Server waiting for connection
New client connected.Connection accepted from Socket[addr=/127.0.0.1,port=52567,localport=5000]
Creating constructor in ServerFrameHandler
Server waiting for connection
Method handleInputMessages starts here
Message delivered from client
Actual value of msg_To_Read is Hey Norm! This is message from first client and it will be printed in text area.
Actual value of chat_Field is :Hey Norm! This is message from first client and it will be printed in text area.  

Method handleInputMessages starts here
New client connected.Connection accepted from Socket[addr=/127.0.0.1,port=52568,localport=5000]
Creating constructor in ServerFrameHandler
Server waiting for connection
Method handleInputMessages starts here
Message delivered from client
Actual value of msg_To_Read is This is message from second client and it looks like everyone is ok but it doesn't show up in text area.  
Actual value of chat_Field is :This is message from second client and it looks like everyone is ok but it doesn't show up in text area.  

Method handleInputMessages starts here
 

Norm Radder wrote:

Then starts the run method if i am right


Add some print statements to show where the execution is doing.  If values are read from a stream, print them.

No need to copy and paste another copy of the code.  One is enough.  What is needed is a copy of the console containing what was printed so we can see where the code was executing.

And if nothing was printed, we know that the execution hung or was blocked before it got to that print statement.



run:
Server waiting for connection
New client connected.Connection accepted from Socket[addr=/127.0.0.1,port=52571,localport=5000]
Creating constructor in ServerFrameHandler
Server waiting for connection
Method handleInputMessages starts here
Message delivered from client
Actual value of msg_To_Read is This is message from first client and it will be printed in text area.
Actual value of chat_Field is :This is message from first client and it will be printed in text area.  

Method handleInputMessages ends here
Method handleInputMessages starts here
New client connected.Connection accepted from Socket[addr=/127.0.0.1,port=52572,localport=5000]
Creating constructor in ServerFrameHandler
Server waiting for connection
Method handleInputMessages starts here
Message delivered from client
Actual value of msg_To_Read is This is message from second client and it looks like everyone is ok but it doesn't show up in text area.  
Actual value of chat_Field is :This is message from second client and it looks like everyone is ok but it doesn't show up in text area.  

Method handleInputMessages ends here
Method handleInputMessages starts here
 

know that text area changes when second client sends a message but it doesn't show up in this area.


Sorry, I don't understand that.  
"text area" changes - what is that?
"in this area" - what does that mean?
 

Norm Radder wrote:

know that text area changes when second client sends a message but it doesn't show up in this area.


Sorry, I don't understand that.  
"text area" changes - what is that?
"in this area" - what does that mean?



Text area is the place where text from clients should show up but it doesnt show in there from second and more clients.
Does this describe what happens when the code executes?
The server receives the text from the client
the server attempts to show that text in a textarea
the text does NOT show in the textarea
 

Norm Radder wrote:Does this describe what happens when the code executes?
The server receives the text from the client
the server attempts to show that text in a textarea
the text does NOT show in the textarea


Yes!
Can you post the current contents of the classes you are working with?
 

Norm Radder wrote:Can you post the current contents of the classes you are working with?


Sure, wait a moment.
Current Classes:

MainClass:



ServerFrame class:



ServerFrameHandler class:



Client class:




ClientHandler class:   I don't know if it's needed for working.




The posted code uses 3rd party packages: org.netbeans.lib.awtextra
Can you remove them and only use standard java se classes?
 

Norm Radder wrote:The posted code uses 3rd party packages: org.netbeans.lib.awtextra
Can you remove them and only use standard java se classes?


Hmm i didn't add any libraries, can you tell me how to do that?
i mean packages.
When i remove AbsoluteLayout from libraries my program doesnt work.
Ok i did it and removed absoluteLayout.
Here is the code for ServerFrame:


This thread has been viewed 927 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 11, 2018 04:27:11.