?
Solved

Passing sockaddr_in structure won't work

Posted on 2003-10-23
10
Medium Priority
?
435 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
  • 4
  • 4
9 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 150 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

862 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