Solved

Passing sockaddr_in structure won't work

Posted on 2003-10-23
10
422 Views
Last Modified: 2010-04-15
Hey,

I'm passing a sockadd_in struct to a function but when it gets there it's empty:

/*here it prints the address, if requested*/
remove(sockfd,serverAddr);

void remove(int socketfd, struct sockaddr_in serverAddr){

/*here it prints 0.0.0.0*/
printf("%s", inet_ntoa(serverAddr.sin_addr.s_addr));

}
0
Comment
Question by:bass20
[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
  • 4
  • 4
10 Comments
 
LVL 1

Author Comment

by:bass20
ID: 9609887
PS: I can't pass the address of the struct because I'll use sendto in function remove() and it needs to use the real address of the struct not the address of a pointer that's been passed. Hence, sendto's perror gives me invalid argument...
0
 
LVL 86

Expert Comment

by:jkr
ID: 9610009
I think you got the concept of pointers and adresses wrong - simply use

void remove(int socketfd, struct sockaddr_in* serverAddr){


printf("%s", inet_ntoa(serverAddr->sin_addr.s_addr));

sendto (socketfd, buf, len, flags, serverAddr, sizeof ( struct sockaddr_in));

}


0
 
LVL 1

Author Comment

by:bass20
ID: 9610338
In that case, when you do

sendto (socketfd, buf, len, flags, serverAddr, sizeof ( struct sockaddr_in));

you'll be passing sendto the address of serverAddr when it expects the structure itself
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 9611856
>>you'll be passing sendto the address of serverAddr when it expects the structure itself

No.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 9612727
jkr has guided you correctly

with regards to your misconception regarding pointers... read on

remove(sockfd,serverAddr); suppose your serverAddr is at memory location 1000


void remove(int socketfd, struct sockaddr_in serverAddr){  // what we get here is a copy of the struct at memory location 1000... we have no way to access the original struct now ... suppose this new struct is at memory ocation 2000

printf("%s", inet_ntoa(serverAddr.sin_addr.s_addr));        // you are printing value in the copy at 2000

sendto (socketfd, buf, len, flags, &serverAddr, sizeof ( struct sockaddr_in)); //here you are passing address 2000 and the local copy of the struct in this function will be accessible to sendto

}
===========================================================================
compare this with what jkr said

remove(sockfd,&serverAddr);   // you pass address 1000 to remove

void remove(int socketfd, struct sockaddr_in* serverAddr){ // we get address 1000 and we can access the original struct

printf("%s", inet_ntoa(serverAddr->sin_addr.s_addr)); // we print values from the struct at location 1000 (notice ->operators)

sendto (socketfd, buf, len, flags, serverAddr, sizeof ( struct sockaddr_in)); // here we pass address 1000 to sendto rather than address 2000 as in the previous case... thus sendto will use the original copy of the struct

}

as far as your original question is concerned, remove should get a copy of the original struct and should be able to print the values
against this background, analyze your program and you will be able to locate the error
if you astill have problems then,
1. try running through a debugger and try to trace the error
2. if that fails, post code here and we will try to check
0
 
LVL 1

Author Comment

by:bass20
ID: 9823671
"This old question needs to be finalized"

I'm quite aware of that, and you may notice that I closed two other questions that were awaiting an answer to be accepted; the reason I didn't on this one, is that I tried twice to do so and I couldn't get the Accept button on Mozilla; I had to clear all cache/cookies, log in and reload question twice to get the button.

I wasn't intending to leave the question open forever, if I didn't close it before, it was because I was unable to do so
0
 
LVL 86

Expert Comment

by:jkr
ID: 9824692
bass20 - I am sorry to say so, you have no clue how EE works.
0
 
LVL 1

Author Comment

by:bass20
ID: 9824933
I know that message was automatic, I posted an answer so none of the people that helped me would think that I didn't pay attention to their posts
0
 
LVL 86

Expert Comment

by:jkr
ID: 9952180
>>I know that message was automatic

Well, and you accepted a comment that baiscally said 'jkr has guided you correctly'...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

724 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