?
Solved

C function for sending ASCII text as UDP packet

Posted on 2009-04-10
5
Medium Priority
?
393 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:davepusey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24116882
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.
0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24116911
This manual will be a great help for you!!

http://www.dmp.com.tw/tech/os-dos/DSock_Programmer_Manual.pdf
0
 
LVL 2

Author Comment

by:davepusey
ID: 24116970
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?
0
 
LVL 4

Accepted Solution

by:
ewest02 earned 2000 total points
ID: 24124642
basically

   fd = socket()      // create a socket
   sendto( fd, ...)            // send datagram to listening socket
   recvfrom( fd, ...)        // get response from server
   close( fd)           // close the socket

An excellent text for the fine details is  Stevens, Unix Network Programming
A quick overview can be had with
http://pages.cpsc.ucalgary.ca/~agopinat/441/files/UDPSocketProgramming.ppt
0
 
LVL 2

Author Comment

by:davepusey
ID: 24132995
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question