?
Solved

check Receiver

Posted on 2003-03-08
12
Medium Priority
?
221 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
[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
  • 3
  • 3
  • 2
  • +3
12 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
Industry Leaders: 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 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

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!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 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