What's the difference between Datagram.read and Datagram.receive?

G

Gordon Beaton

What's the difference between Datagram.read and Datagram.receive?

Exactly what class is Datagram?

DatagramSocket has a receive() method that receives a DatagramPacket.
It has no read() method.

DatagramChannel has read(), write(), send() and receive().

read() and write() can only be used with connected sockets, and only
receive from (or send to) the socket's peer. That's why there is no
way to specify a destination address with write(), or obtain the
sender address with read().

send() and receive() are used with unconnected sockets, and can send
to any destination or receive from any source.

Please post beginner questions to c.l.j.help, or at least make an
effort to read your API documentation before posting here.

/gordon
 
A

Andrew Thompson

Please post beginner questions to c.l.j.help, or at least make an
effort to read your API documentation before posting here.

To the OP. Please don't scrimp on the 'read your API
documentation' if you are posting to c.l.j.help!

(I suspect you actually meant that, but I thought it
needed reinforcing.)

Posting to c.l.j.help is no excuse for not using the
(freely available, comprehensive and rather explicit)
API documentation. Or a search engine, or Java texts..

'Smart questions' are much more welcome on any of the
Java groups (that I have observed).

The only difference between c.l.j.p/h is that on c.l.j.help,
the people mentioning these things might take more time to
ensure it is said in a polite/gentle* way (*or as polite/gentle
as is practical).
 
?

-

Gordon said:
Exactly what class is Datagram?

DatagramSocket has a receive() method that receives a DatagramPacket.
It has no read() method.

DatagramChannel has read(), write(), send() and receive().

read() and write() can only be used with connected sockets, and only
receive from (or send to) the socket's peer. That's why there is no
way to specify a destination address with write(), or obtain the
sender address with read().

send() and receive() are used with unconnected sockets, and can send
to any destination or receive from any source.

Please post beginner questions to c.l.j.help, or at least make an
effort to read your API documentation before posting here.

/gordon

I have read the API but my comprehension of what's written didn't help.
(If I hadn't read the API, I wouldn't know of such methods)

Sorry.
 
G

Gordon Beaton

I have read the API but my comprehension of what's written didn't help.

This is copied from the summary at the top of the DatagramChannel
class description:

"A datagram channel need not be connected in order for the send and
receive methods to be used."

"A datagram channel must be connected in order to use the read and
write methods, since those methods do not accept or return socket
addresses."

Now honestly, did those two sentences really not answer your original
question? I can hardly claim that my own explanation actually adds
anything of value to this one.

/gordon
 
?

-

Gordon said:
Now honestly, did those two sentences really not answer your original
question? I can hardly claim that my own explanation actually adds
anything of value to this one.

/gordon

Valid OPs for DatagramChannel are SelectionKey.OP_READ and
SelectionKey.OP_WRITE.

So how do i actually know the DatagramChannel.connect(..)'s method has
finished connecting? If I remembered correctly, using

while (!isConnected()) {
// do nothing
}

in another thread goes on forever.
 
G

Gordon Beaton

Valid OPs for DatagramChannel are SelectionKey.OP_READ and
SelectionKey.OP_WRITE.

So how do i actually know the DatagramChannel.connect(..)'s method
has finished connecting?

Presumably it has finished when it returns. UDP is not a connection
oriented protocol, and connect() does not involve any actual
communication with the peer.

All connect() does is (locally) setup a peer address so that you don't
need to provide a recipient address with each outgoing packet, and to
get the security checking out of the way so it doesn't need to be done
for each packet you send.

/gordon
 
R

Roedy Green

Please post beginner questions to c.l.j.help, or at least make an
effort to read your API documentation before posting here.

You are setting the bar too high. Newbies do not attempt datagrams.

I suspect some of the old timers are beginning to think of
comp.lang.java.programmer as a sort of exclusive club, and they enjoy
hazing newcomers attempting to keep them out.

Yet surely in your hearts you want to see Java succeed. That will only
happen if many people code in Java. You want avoid doing anything that
will put them off.

Traffic is WAY down from years past. There is far less need now to be
strict.
 
R

Roedy Green

Now honestly, did those two sentences really not answer your original
question? I can hardly claim that my own explanation actually adds
anything of value to this one.

He is more likely to have been reading up on DatagramSocket and
DatagramPacket which have been around longer than nio.

For a someone new coming in, it is not obvious there are two totally
different datagram schemes.

It does no good to tell someone they SHOULD understand something just
because you do. The fact remains they DON'T. Even if the docs read to
you like poetry, to them they are Greek. Bawling the guy out won't
make him any smarter. It will just discourage him.

The bitchy superiority going on in this group toward newbies
is counterproductive to Java succeeding.

If you want to chew someone out who REPEATEDLY refuses to even read
docs, or who lamely demands you do their homework, fine, blast them.
Until you provide a pointer to the relevant docs, you can't very well
accuse them of refusing to read. They have no clue what to look for.
They don't have the vocabulary to look. They have no idea if what
they are looking for even exists.

People post stuff here they would never say face to face.

Imagine someone at a help desk at a university handing out advice like
that given here putting down newbies.
 
R

Raymond DeCampo

Roedy said:
He is more likely to have been reading up on DatagramSocket and
DatagramPacket which have been around longer than nio.

For a someone new coming in, it is not obvious there are two totally
different datagram schemes.

It does no good to tell someone they SHOULD understand something just
because you do. The fact remains they DON'T. Even if the docs read to
you like poetry, to them they are Greek. Bawling the guy out won't
make him any smarter. It will just discourage him.

The bitchy superiority going on in this group toward newbies
is counterproductive to Java succeeding.

If you want to chew someone out who REPEATEDLY refuses to even read
docs, or who lamely demands you do their homework, fine, blast them.
Until you provide a pointer to the relevant docs, you can't very well
accuse them of refusing to read. They have no clue what to look for.
They don't have the vocabulary to look. They have no idea if what
they are looking for even exists.

People post stuff here they would never say face to face.

Imagine someone at a help desk at a university handing out advice like
that given here putting down newbies.

People looking for free advice shouldn't complain if they get a bit of
the "teach a man to fish" treatment at the same time.

Oops, wait, they weren't complaining, you were. How does this affect
you again?

Ray
 
G

Gordon Beaton

He is more likely to have been reading up on DatagramSocket and
DatagramPacket which have been around longer than nio.

Extremely doubtful, since the methods he mentioned were common only to
DatagramChannel (AFAIK). His later followup confirmed that, which you
would have seen had you bothered to read the entire thread before
accusing me of attitude problems.
For a someone new coming in, it is not obvious there are two totally
different datagram schemes.

In another post in this thread you claimed that "newbies do not
attempt datagrams.". Yet datagrams it is. Newbie, or not?
It does no good to tell someone they SHOULD understand something
just because you do. The fact remains they DON'T. Even if the docs
read to you like poetry, to them they are Greek. Bawling the guy out
won't make him any smarter. It will just discourage him.

The documentation was simple and concise in this case, and my own
response added nothing (other than to point out that the answer could
have been found in the documentation).

If you've read the documentation and don't understand, then you need
to mention that in your question in order to get responses that
address the right issues, or you risk being pointed right back at the
documentation.
The bitchy superiority going on in this group toward newbies
is counterproductive to Java succeeding.

Regardless of whether Java fails or succeeds it will do so on its own
merits (with some help from Sun), not on the advice (or lack thereof)
handed out on Usenet, a relatively obscure place in the larger scheme
of things (and more so for each year).

I did not "bawl him out", "chew him out" or "blast him", as you imply.
I gave him the answer he was looking for, and told him where he could
have found it himself had he made the effort. That will help him and
others down the road who might find this thread.

What did you do?

/gordon
 
G

Gordon Beaton

You are setting the bar too high. Newbies do not attempt datagrams.

Since his question was about datagrams, I guess it's safe to assume
that he was not a newbie.

So "make an effort to read the API documentation" was by no means an
inappropriate suggestion (and actually I agree with Andrew here, that
even newbies need to make that effort).

If you're going to criticise me, at least try to get your logic
straight.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top