UDP Broadcast under PB4 (Beta 7)

Just starting out? Need help? Post your questions and find answers here.
Charlie
New User
New User
Posts: 3
Joined: Sun Feb 27, 2005 11:45 am

UDP Broadcast under PB4 (Beta 7)

Post by Charlie »

with PB3.94 and UDP userlib from PureFan i can send limited broadcast message to "255.255.255.255" without to know my own ip.
with PB4 (Beta 7) i only can send a directed broadcast ex. 192.168.0.255, but to do so i must know my own ip on lan.

how i can send a limited broadcast message without to know my own ip?
because i use it to find out my own ip on lan by send a coded broadcast message and other global message.
the solution should working on windows and linux also.
Sebe
Enthusiast
Enthusiast
Posts: 160
Joined: Sun Dec 19, 2004 10:55 pm
Location: Munich
Contact:

Post by Sebe »

Pseudocode

Code: Select all

For a = 1 to 255

  SendUDP(MESSAGE$, "192.168.9." + str(a))

Next
Could be a little slow though :wink:

You could also try to send to 192.168.0.0 because [FIRST 3 IP NUMBERs].0 is the broadcasting number afaik :?
Charlie
New User
New User
Posts: 3
Joined: Sun Feb 27, 2005 11:45 am

Post by Charlie »

you don't know from stuff i mean, right?

with 192.168.x.255 you already send an udp broadcast message, but its a directed broadcast.
what i want is to send limited broadcast message where ip 255.255.255.255 as target.

when i use

Code: Select all

ConnectionID.l = OpenNetworkConnection("255.255.255.255", 5000, #PB_Network_UDP)
ConnectionID give me zero number as result, and zero mean that connection is failed and can not established.

so, any other way to send limited broadcast? or should i back to PBv3.94 with PureFan UDP userlib.
real
User
User
Posts: 49
Joined: Fri Oct 08, 2004 5:17 am

Post by real »

Hi Charlie,

take a look into your thread in the german forum... I posted a brand new userlib (and the smallest one ever, I think :lol: ) for PB4 with only 1 function - SendUDPBroadcast(Port, Message$). It enables you to send messages via UDP to IP 255.255.255.255.

If I think about the problem with OpenNetworkConnection:
Sending messages to broadcasts and multicasts requires the use of a generic sendto() function without need to connect to a server. You MUST NOT connect to a broadcast-IP, because a wide range of "servers" get the message and handle it.

For interested users in this forum: It's up to you to try the lib - download it on http://www.cornycountry.de! At the moment it's commented in german, but in a few minutes it will be available with english comments, too.
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

Does this really need to be a lib ?
real
User
User
Posts: 49
Joined: Fri Oct 08, 2004 5:17 am

Post by real »

No - that's why the lib is that kind of small. :roll: The reason for creating a library is simply to write one, because I didn't know how to do... Charlie mentioned (in the german forum) the UPDlib for PB 3.94 and I wanted to make a try with an interesting background. If you look into the source you should know what to bundle into a working udp-broadcast-FUNCTION (for example):
1. you have to create a socket_()
2. you have to setsockopt_() to allow broadcasts
3. you have to set the destination IP to broadcast IP
4. you have to sendto_() a message to the built socket
5. you have to closesocket_() the socket.

If you don't like my lib (I take it personal :wink:), use this procedure to send a udp broadcast:

Code: Select all

Procedure.l SendUDPBroadcastL (port, message.s)
  Protected sock,one,bytes,remote.sockaddr_in
  
  sock = SOCKET_(#AF_INET,#SOCK_DGRAM,#IPPROTO_IP)
  If ( sock = -1) : ProcedureReturn 1 : EndIf
  one = 1
  If (setsockopt_(sock,#SOL_SOCKET,#SO_BROADCAST,@one,SizeOf(one)) = -1)
    ProcedureReturn 1
  EndIf
  remote\sin_family = #AF_INET
  remote\sin_port = htons_(port)
  remote\sin_addr = #INADDR_BROADCAST
  if (sendto_(sock,message,Len(message),0,@remote,SizeOf(remote)) = -1)
    ProcedureReturn 1
  EndIf
  closesocket_(sock)
  ProcedureReturn 0
EndProcedure
Greets
René
Post Reply