• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Server accept both connections but print messages only from one.

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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):

 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ServerFrameHandler  fixed While loop. It wasn't needed. So here is a valid code.Sorry




 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why there is no edit button ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everything works fine except that text from client 2 or  more doesn't show up at TextArea.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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);
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
           


New instaces is "Creating constructor in ServerFrameHandler"




 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then starts the run method if i am right ( im newbie so...)
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
everything*
Ouuh, sorry for those mistakes.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.  

 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now we know that text area changes when second client sends a message but it doesn't show up in this area.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the current contents of the classes you are working with?
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Sure, wait a moment.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Current Classes:

MainClass:



ServerFrame class:



ServerFrameHandler class:



Client class:




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




 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The posted code uses 3rd party packages: org.netbeans.lib.awtextra
Can you remove them and only use standard java se classes?
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i mean packages.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i remove AbsoluteLayout from libraries my program doesnt work.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i did it and removed absoluteLayout.
Here is the code for ServerFrame:

 
reply
    Bookmark Topic Watch Topic
  • New Topic