Solved

Socket: Allow Writing and Receiving on the same socket

Posted on 2007-04-06
9
283 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

630 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