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.
My problem that i have for first time is that the recv seems to not set the correct values to the parameters
Code:
extern union qdisc qdisc_stats[10];
............
if ((n=recvfrom(sock,qdisc_stats,sizeof(qdisc_stats[10]),0,(struct sockaddr *) &client, &client_len)) <0){
After this call the struct has the same values... like not ever getting the values
I send the values from another pc
extern union qdisc reconfigure_qdisc[10];
printf("I send for example %s \n",reconfigure_qdisc[0].htbstats.rate);
if (sendto(sock,reconfigure_qdisc,sizeof(reconfigure_qdisc), 0,(struct sockaddr *) &server, server_len) <0){
Try creating your own dummy "recvmsg()" and see what happens:
cut/pasted from your example code (in a separate thread):
Code:
#include <stdio.h>
#include <string.h>
/*
* Here's the struct we're trying to read into
*/
typedef struct htb
{
char *parameter;
}
htb;
union qdisc
{
int i;
struct htb htb;
}
qdisc_stats[10];
/*
* Here's a substitute for the actual "read" procedure
*/
int
recvfrom2 (int s, void *buf, size_t len, int flags, void *from, size_t *fromlen)
{
strcpy ((char *)buf, "ABC");
return 4; /* 'A', 'B', 'C' and '\0' */
}
/*
* And here's our test driver
*/
int
main(int argc, char *argv[])
{
int n = -1, sock = 0, fromlen = 0;
/* Initialize the data */
memset (&qdisc_stats, sizeof qdisc_stats, 0xaa);
qdisc_stats[0].htb.parameter="foo";
printf ("Before: %s %d\n",
qdisc_stats[0].htb.parameter, qdisc_stats[0].i);
/* Attempt read */
n= recvfrom2 (sock, qdisc_stats, sizeof(qdisc_stats[10]), 0, 0, (size_t *)&fromlen);
printf ("After: n= %d; %s %d\n",
n, qdisc_stats[0].htb.parameter, qdisc_stats[0].i);
return 0;
}
I've cut and pasted the "qdisc_stats" data definition from another thread you posted.
If you run this under a debugger, you'll see that it should probably *crash*.
Please consider at least *borrowing* a copy of K&R from a library until I can get you a copy. Please?
PS:
"sizeof (qdisk_stats[10])" for a 10-element array is syntactically legal, it won't crash ... but it's
extremely poor form. Stylistically, it would be *much* better to write "sizeof (qdisk_stats[0])".
And to declare "main()" either "int" (the standard definition), or "void" (if you really don't want
to return anything. But I think you should always return at least a "0").
IMHO.
PPS:
The goal here is to compare how you *meant* to pass your struct to recvfrom() (presumably, you mean to pass the address of some "parameter" string) with the buffer that you *actually* passed into recvfrom().
K&R would be of help. The tutorial on arrays and pointers would be of help. a debugger like gdb (Linux) or Visual Studio (Windows) would be of *enormous* help.
PPS:
One final question: how big can your parameter strings be? And where are you allocating the space for them? Again: K&R discusses all of these issues.
Thx paul basically i am very tired
i have changed the line to the send to
if (sendto(sock,reconfigure_qdisc,sizeof(reconfigure_qdisc[0]), 0,(struct sockaddr *) &server, server_len) <0){
and now it works
The line before was
if (sendto(sock,reconfigure_qdisc,sizeof(reconfigure_qdisc), 0,(struct sockaddr *) &server, server_len) <0){
"sizeof qdisc_stats[0]" gives the size of one "qdisk" element (which happens to be a union of "struct htb" and "int", which happens to be 4 bytes). "sizeof qdisc_stats", on the other hand, gives the size of the whole thing (which happens to be 10 "qdisc" elements, with no padding and no extra space). One is "4", the other is "40".
Now: what's an "htb"? It's a struct that contains a pointer to a string. *Not* a string, but a *pointer* to a string. I would humbly suggest this might be "better":
Originally posted by alaios Thx paul basically i am very tired
i have changed the line to the send to
if (sendto(sock,reconfigure_qdisc,sizeof(reconfigure_qdisc[0]), 0,(struct sockaddr *) &server, server_len) <0){
and now it works
The line before was
if (sendto(sock,reconfigure_qdisc,sizeof(reconfigure_qdisc), 0,(struct sockaddr *) &server, server_len) <0){
Can u explain me whats the difference?
The difference is in the use of the name of an array. The sizeof() an array[0] will be the size of one element, the sizeof() the name of the array will the size of its pointer. Confusing, but logical, in a K&R way.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.