Solved

Socket: Allow Writing and Receiving on the same socket

Posted on 2007-04-06
9
276 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle 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:mayankeagle
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 39
scoreUp challenge 14 48
bunnyEars challenge 6 68
json format text only 4 65
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 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

13 Experts available now in Live!

Get 1:1 Help Now