Solved

Socket: Allow Writing and Receiving on the same socket

Posted on 2007-04-06
9
280 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 107
eclipse buid path vs tomcat lib path 10 34
How  can  i  resolve  HTTP Status 404 -? 8 44
going to wrong jsp page 2 33
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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.

829 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