LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2005, 07:11 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
socket dont get/set values (c problem)


Hi i have used in the past many times sockets

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){
 
Old 09-16-2005, 09:27 AM   #2
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
omg i am searching for this thing all the night... i want so sleep
 
Old 09-16-2005, 11:12 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.

Last edited by paulsm4; 09-16-2005 at 11:39 AM.
 
Old 09-16-2005, 03:19 PM   #4
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
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?
 
Old 09-16-2005, 04:25 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Code:
int
main(int argc, char *argv[])
{
  int n = -1, sock = 0, fromlen = 0;

  /* SAMPLE OUTPUT:
   * sizeof qdisc_stats: 40, sizeof qdisc_stats[0]: 4
   */
  printf ("sizeof qdisc_stats: %d, sizeof qdisc_stats[0]: %d\n",
     sizeof qdisc_stats, sizeof qdisc_stats[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":

Code:
#define MAX_STRING 4
typedef struct htb
{
  char parameter[MAX_STRING];
} 
  htb;
IMHO ...
 
Old 09-18-2005, 06:17 AM   #6
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
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.
 
Old 09-20-2005, 04:51 AM   #7
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
thx a lot but if i receive a pointer how i can calculate the correct size to feed the function correctly?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem displaying Bash array values. shinobi59 Programming 3 01-17-2006 05:45 PM
Dont know how to set-up... Dark_Sniper* Linux - Networking 2 05-24-2005 08:43 PM
what would happen if set the SO_SENDBUG for udp socket to 0? ringerxyz Programming 0 02-20-2005 02:16 AM
Dont know how to set IP info valsus Linux - Networking 3 04-14-2004 03:47 PM
How to set SQL to UNIX socket Skunk_Face Linux - General 1 01-19-2004 06:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration