Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

check Receiver

Posted on 2003-03-08
12
Medium Priority
?
225 Views
Last Modified: 2012-05-04

I have clients and server and i use metods for sending and receiving.
if the receiver did not reecive a message, it shoud do other different functions . How can i made the receiver return boolen to check if receive or not . Should i check the buffer or should i use a spcial exceptions ???  
0
Comment
Question by:Marmar
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8093757
Simply set a boolean flag when you do get as message. Have an accessor method that can be called at any time, such as:

public boolean getMessageReceived() {
  return messageReceived;
}
0
 
LVL 2

Expert Comment

by:karlika
ID: 8093896
I would not recommend exceptions because they always have some overhead and are usually far from the ideal way you want your code to work.

Provided the receiver knows when a message should be received (eg directly after a request) you may wait until some timeout and then insert your code after the timeout-statement.

This is a solution for only very simple and not too timecritical actions. For asynchronous actions simply start a new thread that handles what you want to do after a timeout.

If you do not know when a message is to be received I would recommend to check the buffer if available.

Marko
0
 

Author Comment

by:Marmar
ID: 8097199
If you do not know when a message is to be received I would recommend to check the buffer if available.

>How can I ckeck the buffer?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Expert Comment

by:pronane
ID: 8098254
The easiest way to do this is:

public boolean hasText()
     {
          boolean hastext = false;
          try
          {
               hastext = (dis.available() > 0);
          }
         
          catch (IOException e)
          {
               System.out.println(e);
               System.exit(1);
          }
          return hastext;
     }

where dis has been initialised as
private DataInputStream dis;
dis = new DataInputStream(sock.getInputStream());
   
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8102078
I fail to see why such complex solutions are being offered for such a simple problem! Any socket receiving a message will block until it reads something. As soon as it does, it can set a boolean flag to tell you it *has* received something. If that has a proper accessor method (a public 'get' method) it can be called from anywhere.
0
 

Author Comment

by:Marmar
ID: 8109332
The follwing code working with DataInputStream..
public boolean hasText()
    {
         boolean hastext = false;
         try
         {
              hastext = (dis.available() > 0);
         }
         
         catch (IOException e)
         {
              System.out.println(e);
              System.exit(1);
         }
         return hastext;
    }

where dis has been initialised as
private DataInputStream dis;
dis = new DataInputStream(sock.getInputStream());

But I'm using datagramSocket, so can i modify this code to work with DatagramSocket?


   
0
 
LVL 1

Expert Comment

by:pronane
ID: 8111347
Yes you can modify it so long has available is a method in datagram socket.
0
 
LVL 1

Expert Comment

by:pronane
ID: 8111397
here is the api, Im not fully sure but you can try it with datagramsocket, you could pass the datagram socket throught the datainputstream ( I think )

I have done it using BufferedReader in = new BufferedReader(new InputStreamReadr)
then i pass the sock.getInputStream which has been declared as  a socket that takes buffered reader as the input stream.

http://java.sun.com/products/jdk/1.2/docs/api/java/net/DatagramSocket.html
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 8111543
What are you intending to do with this datagram socket? Presumably read from it? Why do you need to call this method to determine if there is anything to read beforehand anyway? Why don't you inform the other end that something *has been read* instead?
0
 

Expert Comment

by:CleanupPing
ID: 9058847
Marmar:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9693307
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to CEHJ

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

564 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