ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I was just wondering how I would go about making a UDP packet and sending it out, I know C and C++ well and I know unix well enough to learn how if need be. Can anyone help me out here, I am trying to learn about computers and heres something I don't know about. Heck I would like to know how to make a packet period, not just UDP.
A "packet" is kind of a loose term for a set of data. About the easiest way to create a "packet" is to create a struct. To send it out via UDP, you should look into sockets.
Just to give you an idea of where to start looking, here's a quick pseudo-code example:
Code:
typedef struct _packet
{
int iSomeValue;
int iSomeOtherValue;
} Packet;
int main()
{
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SOCKADDR_IN addr;
Packet p;
// Setup a port to bind to
addr.sin_family = AF_INET;
addr.sin_addr.S_un.S_addr = INADDR_ANY;
addr.sin_port = htons(9998);
bind(sock, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN));
p.iSomeValue = 0;
p.iSomeOtherValue = 1;
// Define an address to send to
addr.sin_addr.S_un.S_addr = inet_addr("192.168.1.167");
addr.sin_port = htons(1812);
// Send our packet to 192.168.1.167:1812
sendto(sock, (char*)(void*)&packet, sizeof(Packet), (SOCKADDR*)&addr, sizeof(SOCKADDR_IN));
return 0;
}
Note: Some of the above code was taken from a Windows app I wrote, so the constants might be a bit different. (For example, you probably need to use sockaddr_in rather than SOCKADDR_IN)
yes I meant creating your own packets including the headers. Where would I find documentation for all the functions that where called in that code. I get the feeling that when it comes to alot of that stuff its one of those situations in programing where it would take to long to go into all the function calls and you just have to take them ob faith.
I know it may be taboo to recommend Microsoft references here, but you can also take a look at the Winsock documentation at MSDN. Winsock is derived from Berkley sockets, so it is pretty close to the same calls you would use on Unix. Not everything is exactly the same, but it's usually easy enough to figure out the differences.
ok nother quick question while this thread is open how do you start a chargen service in lunix or unix? open a new one on say port 15000 or something like that.
i think you have alot of learning to do b4 you create your own packets, have a look here first http://www.ecst.csuchico.edu/~beej/guide/net/ and learn about the sockets interface. a great book is unix network programming vol I by W Richard Stevens. after reading that you'll have a solid grasp of whats going on and can move onto advanced stuff like raw sockets(that is how you create your own packets).
dood that sight is what I have been looking for a long time seriously it rocks big time. Anyway, so now my question is: when you call the send(...) functions I take it you would use a struct that would be pointed to by *msg, but in that struct if I wanted to send something like SSDP packet or something like it. Like here:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.