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

Socket: Allow Writing and Receiving on the same socket

Hi experts,

After accept the socket Socket socket = serverSocket.accept() in a object
How can I achieve the followings simutaneously?
1) non-stop monitoring socket for incoming message, received message have to pass to outer object
2) allow other objects to send message?

It seems to me that something called "callback" interface has to be implemented, I am looking for sample codes of this...  and explanations...
4 Solutions
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
>>1) non-stop monitoring socket for incoming message, received message have to pass to outer object

All you need is

a. a separately threaded loop
b. a method on the outer object to call with the message as a parameter

>>2) allow other objects to send message?

An interface to the socket's output stream allowing the message to be passed as a parameter
alenwongAuthor Commented:
shivaspk: don't sure if there's any implementation based on Socket and ServerSocket

CEHJ: it would be grateful if you could provide example...  
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Mayank SAssociate Director - Product EngineeringCommented:
Have a look at this to get an idea of a sample multi-threaded server:


In the run () method, you could loop and receive values:

while ( running )
  String s = br.readLine () ;
  System.out.println ( s ) ;

For sending, you can provide a method like:

public void send ( String s )
  writer.println ( s ) ;
alenwongAuthor Commented:
mayankeagle: don't sure if this segment allows only sequenial read and write, that means a read write write or write write read is not possible....  

      BufferedReader reader = new BufferedReader ( new InputStreamReader ( s.getInputStream () ) ) ;
      PrintWriter writer = new PrintWriter ( s.getOutputStream (), true ) ;
      while ( true )
        // use 'reader' to read commands from the client
        // use 'writer' to send response to the client
Mayank SAssociate Director - Product EngineeringCommented:
You need to change it slightly. the ServerThread will have:

class ServerThread implements Runnable
  private Socket s ;
  private BufferedReader reader ;
  private PrintWriter writer ;
  public ServerThread ( Socket s )
    this.s = s ;
    // initialize reader and writer here, as in the run () method
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

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