Solved

SocketChannel

Posted on 2003-11-07
5
204 Views
Last Modified: 2010-03-31
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
Comment
Question by:skyblue01
  • 3
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 150 total points
ID: 9700317
 Yes, you can use the same socket if you want to.
0
 

Author Comment

by:skyblue01
ID: 9700362
Thanks girionis.   And I'd assume that this won't cause problems for the connection states, would it?  
0
 
LVL 35

Expert Comment

by:girionis
ID: 9700398
 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
 
LVL 35

Expert Comment

by:girionis
ID: 9700405
 Just make sure that no other component outside the thread on the client has access to the socket (input and output streams).
0
 

Author Comment

by:skyblue01
ID: 9700514
k
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now