• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

SocketChannel

My client is sending a message into the byte buffer to the SocketChannel sc.   Right after it sends it to the server, it starts a background thread for receiving any incoming messages.   In the run() method of the Client class which extends Thread, I'm using this same SocketChannel sc (field variable) to read bytes of data into the buffer to get the message.   Is this legal or do I have to create a new SocketChannel in run() designed specifically for this message retrieving purpose?   Please let me know.   This is part of the simple chat program where the client sends a message to the server which the server redirects it back to all the connected clients.   Thanks.  
0
skyblue01
Asked:
skyblue01
  • 3
  • 2
1 Solution
 
girionisCommented:
 Yes, you can use the same socket if you want to.
0
 
skyblue01Author Commented:
Thanks girionis.   And I'd assume that this won't cause problems for the connection states, would it?  
0
 
girionisCommented:
 No, the client shouldn't have problems and since you are using the nio API then you can use Selectors on the server to manage several simultaneous socket connections to one thread.
 
0
 
girionisCommented:
 Just make sure that no other component outside the thread on the client has access to the socket (input and output streams).
0
 
skyblue01Author Commented:
k
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now