Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Java Datagram Broadcast Bug

Java Datagram Broadcast Bug
Thread Tools
mcomb
Guest
Status:
Reply With Quote
Oct 5, 2000, 10:44 PM
 
Hey, does anyone know how to send a datagram packet to the broadcast address on OSXb? The following code runs fine on Solaris, Linux, Windows, and OS 9, but results in a "java.io.IOException: Permission denied" error under OSX.

Thanks,
-Mike
--------

import java.net.*;
import java.io.*;

public class socktest
{
public static void main(String args[])
{
DatagramSocket socket;
DatagramPacket packet;
byte[] data={1,2,3,4};
try {
socket = new DatagramSocket();
packet = new DatagramPacket(data,data.length,InetAddress.getByN ame("255.255.255.255"),8300);
socket.send(packet);
}
catch (IOException e)
{
System.out.println("Error: " + e);
}
}
}
     
mcomb
Guest
Status:
Reply With Quote
Oct 5, 2000, 11:52 PM
 
Weird, this isn't just a java bug. I tried the same thing with perl and got a similar error. I am trying to do a udp broadcast for server discovery. Any ideas?
-Mike

---------
[mentally:/Users/mcomb] root# cat test2.pl
#!/usr/bin/perl
use Socket;
$MSG="1,2,3,4";
$HOSTNAME="255.255.255.255";
$PORTNO=8300;
socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "socket: $!";
$ipaddr = inet_aton($HOSTNAME);
$portaddr = sockaddr_in($PORTNO, $ipaddr);
send(SOCKET, $MSG, 0, $portaddr) == length($MSG) or die "cannot send to $HOSTNAME($PORTNO): $!";

[mentally:/Users/mcomb] root# perl test2.pl
cannot send to 255.255.255.255(8300): Permission denied at test2.pl line 9.


[This message has been edited by mcomb (edited 10-06-2000).]
     
mcomb
Guest
Status:
Reply With Quote
Oct 8, 2000, 11:19 PM
 
Well, apparently this is a know bug with the JVM. Not sure why it did not work from perl though.

-Mike
     
Fresh-Faced Recruit
Join Date: Jun 2007
Status: Offline
Reply With Quote
Jun 21, 2007, 01:04 PM
 
If you want send a broadcast packet

just do : socket.setBroadcast(true);

In Perl I don't know how ou can do
     
Fresh-Faced Recruit
Join Date: Jan 2009
Status: Offline
Reply With Quote
Jan 8, 2009, 12:12 PM
 
You have to understand the following:

Broadcast is not Multicast.

Broadcast:

A broadcast is also on the ethernet a broadcast,
that means, the ethernet datagram has not a MAC address,
but a broadcast address.

Well, who cares? We do.
Because: The NIC forewards the datadram as packet to the TCP/IP layer,
(and there to the designated port... the process owning the port gets
the datagram)

So, nothing to join, no group no ip, just listen to a port.

Multicast:
Ok, this is on the TCP/IP layer alone.
A client has to JOIN a group (The group ranges from 224.0.0.0 to 239.255.255.255).
Here you do tell the router to foreward all packets sent to the group (and port).
So, this is absolutely diffrent to broadcast, but one single code line to add.
(exact would be to, as you have to leave the group)

The following code is a client, listening... of course has to be surrounded with try, catch etc.


//----------------CLIENT
int port = 44444;


MulticastSocket socket = new MulticastSocket(port);

InetAddress group = InetAddress.getByName(grp);
// if it is a multicast group, we do have to join the group
// (so that the router will send the muticast packets.
if (group.isMulticastAddress())
{
System.out.println("Group is an Multicast group, therefore joining group...");
socket.joinGroup(group);
}
else
{
System.out.println("Group is not a broadcast group, therefore ignoring group IP, just listening on port "+port+" for any datagram!");
socket.setBroadcast(true);
}
//

DatagramPacket packet;
byte[] buf = new byte[256];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
.
.
.

///at the end, you may add, or care previosly to remember this is a multicast join:

try{
socket.leaveGroup(group);
}
catch(Exception ex)
{
// dont care....
}
socket.close();


//--- The server is simplier:

int port = 44444;
socket = new DatagramSocket(this.iPortBroadcast);
while (socket.isBound())
{
try
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
bout.add(.......... // here you do have to something
InetAddress ipGroup = InetAddress.getByName("255.255.255.255");
DatagramPacket packet = new DatagramPacket(bout.toByteArray(), bout.size(), ipGroup, this.iPortBroadcast);
socket.send(packet);
}....
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:14 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2