Solved

Passing sockaddr_in structure won't work

Posted on 2003-10-23
10
415 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
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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now