Link to home
Start Free TrialLog in
Avatar of Dave Pusey
Dave Pusey

asked on

C function for sending ASCII text as UDP packet

I'm trying to write a C function that accepts an ASCII string as it's input parameter and broadcasts that text over the network as a UDP packet.

I have no idea where to start with this.

Here's a brief example of what i mean...
int main( int argc, const char* argv[] )
{
	SendTextViaUdpBroadcast("EXAMPLE_TEXT_TO_BE_SENT");
}
 
void SendTextViaUdpBroadcast ( TextIn )
{
	// Here the contents of TextIn are send as ascii in a udp packet as a broadcast (to ip 255.255.255.255)
}

Open in new window

Avatar of mfhorizon
mfhorizon

You want to set the EnableBroadcast property on the UdpClient instance
set to true. Once you do that, you want to send your request to the IP
address of 255.255.255.255. From the documentation for the EnableBroadcast
property on the UdpClient class:
Broadcasting is limited to a specific subnet. You can broadcast to your
local subnet by sending a packet to 255.255.255.255; or, you can use the
directed broadcast address, which is the network portion of an Internet
Protocol (IP) address with all bits set in the host portion. For example, if
your IP address is 192.168.1.40 (a Class C address, with the network portion
as the first three octets, and the host portion is the last octet), your
directed broadcast address is 192.168.1.255.
Avatar of Dave Pusey

ASKER

Maybe i should clarify... This is C on linux (debian/ubuntu).

How do i open the socket in the first place?

How do i send the packet?

How do i close the socket afterwards?
ASKER CERTIFIED SOLUTION
Avatar of ewest02
ewest02
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'd already figured it out by the time you'd posted, but i'll give you the points anyway seeing as it's what i came up with.