Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Socket: Allow Writing and Receiving on the same socket

Posted on 2007-04-06
9
Medium Priority
?
285 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 500 total points
ID: 18865139
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 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
Independent Software Vendors: 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 500 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 500 total points
ID: 18868558
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
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…
This video teaches viewers about errors in exception handling.
Suggested Courses

688 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