Solved

Socket: Allow Writing and Receiving on the same socket

Posted on 2007-04-06
9
282 Views
Last Modified: 2010-03-31
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...
0
Comment
Question by:alenwong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 125 total points
ID: 18865139
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
ID: 18865320
>>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
0
 

Author Comment

by:alenwong
ID: 18865343
shivaspk: don't sure if there's any implementation based on Socket and ServerSocket

CEHJ: it would be grateful if you could provide example...  
0
Technology Partners: 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!

 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 125 total points
ID: 18865708
Have a look at this to get an idea of a sample multi-threaded server:

http://www.experts-exchange.com/Programming/Languages/Java/Q_21757720.html#16084277

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 ) ;
}
0
 

Author Comment

by:alenwong
ID: 18865762
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....  

    try
    {
      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
      }
    }
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18866197
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
    ..
  }
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 125 total points
ID: 18868558
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 615
Desingning Refactoring existing code 2 45
What is the use of Forwarding Class in java 1 53
Java Inheritance super keyword use 8 72
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

734 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