Re: UDP broadcasts: bind exception on 192.168.0.255

O

Old and In the Way

Added comp.lang.java.programmer

I'm trying to create a DatagramSocket to send broadcasts, but I'm
getting a

java.net.BindException: Cannot assign requested address: Cannot bind

on the call

udpBcastSocket = new DatagramSocket( oport, InetAddress.getByName
(target) ) ;

Where 'target' is 192.168.0.255

In STFW, descriptions I've seen so far seem to indicate that
DatagramSockets can be used for UDP broadcasts. I have apparently
not yet found the right web page.

I've love to learn what I'm doing wrong.
 
C

Chris Uppal

Old said:
In STFW, descriptions I've seen so far seem to indicate that
DatagramSockets can be used for UDP broadcasts. I have apparently
not yet found the right web page.

The following test code works for me.

-- chris

========================
private void
run()
throws IOException
{
byte[] broadcast = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
InetAddress addr = InetAddress.getByAddress(broadcast);
DatagramPacket packet = new DatagramPacket(
new byte[BUFFER_SIZE],
BUFFER_SIZE,
addr,
PORT);
packet.setAddress(addr);

DatagramSocket socket = new DatagramSocket();

BufferedReader in = new BufferedReader(
new InputStreamReader(
System.in));
for (;;)
{
byte[] bytes = in.readLine().getBytes("UTF-8");
packet.setData(bytes);
socket.send(packet);
}
}

========================
 
E

EJP

Old said:
Where 'target' is 192.168.0.255

192.168.0.255 must be an IP address on the local machine, and it appears
that it isn't. You're generally better off omitting this parameter, or
specifying null.
 
B

Brandon McCombs

Old said:
Added comp.lang.java.programmer

I'm trying to create a DatagramSocket to send broadcasts, but I'm
getting a

java.net.BindException: Cannot assign requested address: Cannot bind

on the call

udpBcastSocket = new DatagramSocket( oport, InetAddress.getByName
(target) ) ;

Where 'target' is 192.168.0.255

In STFW, descriptions I've seen so far seem to indicate that
DatagramSockets can be used for UDP broadcasts. I have apparently
not yet found the right web page.

I've love to learn what I'm doing wrong.


Similar to what someone else said, you don't bind to the broadcast
address. You bind to a local address (192.168.0.1) in order to later
send the broadcast to the broadcast IP (192.168.0.255).
 
O

Old and In the Way

Just a quick note of thanks for the assistance from Chris, EJP, and
Brandon. I'm a little less clueless now.
 
A

Andrew Thompson

Mark said:
Multiposting?

Are your referring to the message on c.l.j.help?
That was a cross-post.

[ For unititiated readers, cross-posting (X-posting) is
considered much less problematic than multi-posting,
to the extent that people generally recommend X-posting
as an alternative to multi-posting. More here..
<http://www.cs.tut.fi/~jkorpela/usenet/xpost.html> ]

And as a note to the OP. It is generally not a good idea to
X-post between the c.l.j.help and c.l.j.programmer newsgroups.

c.l.j.help is a group mostly for those new to Java or
programming (or usenet), people who *reply* to that
group are strongly 'encouraged' to answer politely,
or not at all.

The c.l.j.programmer group has readers generally of a higher
technical expertise, but posters are also expected to have
more experience (not just at Java, but 'Netiquette' as well),
and are often treated rudely, or dismissively, if they fail to
meet the reader's expectations.

Andrew T.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top