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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-27-2005, 12:54 PM
|
#1
|
|
LQ Newbie
Registered: Jan 2004
Location: USA
Distribution: SuSE 9.3 Pro
Posts: 13
Rep:
|
Accessing member functions of sockaddr_in
Hello,
I am working on a small project in Linux, written in the C language, that allows communication to and from a server. So far I have my concept in mind, and source code close to flawless. Though, I have a small problem. To reduce the size of code used in main, I created a structure to allow the handles of clients, sockets, and other information store in different areas. Though, to make my job easier, I want to pass the address of a sockaddr_in and access its members inside a function.
Though, when I do, it gives an error as the following: " request for member 'sin_family' in something not a structure or union"
Here is a code sample:
Code:
void setInfo(struct sockaddr_in *sock) {
memset(sock, 0, sizeof(*sock));
*(sock.sin_family) = AF_INET;
*(sock.sin_port) = htons(port);
*(sock.sin_addr.s_addr) = INADDR_ANY;
}
int main() {
struct sockaddr_in server;
setInfo(&server);
return 0;
}
Compiled with GCC 3.4.3 | Slackware 10.0
At this moment, I'm clueless of why it won't accept the indirection (*) operator. Any help would be appreciated.
- Stack Overflow
|
|
|
|
01-27-2005, 01:06 PM
|
#2
|
|
Moderator
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,505
Rep: 
|
I believe it should be:
Code:
void setInfo(struct sockaddr_in *sock) {
memset(sock, 0, sizeof(*sock));
(*sock).sin_family = AF_INET;
(*sock).sin_port = htons(port);
(*sock).sin_addr.s_addr = INADDR_ANY;
}
You want to dereference sock, not sock.sin_family... sock.sin_family does not exist.
Alternatively, use struct pointer syntax:
Code:
void setInfo(struct sockaddr_in *sock) {
memset(sock, 0, sizeof(*sock));
sock->sin_family = AF_INET;
sock->sin_port = htons(port);
sock->sin_addr.s_addr = INADDR_ANY;
}
|
|
|
|
01-27-2005, 01:16 PM
|
#3
|
|
LQ Newbie
Registered: Jan 2004
Location: USA
Distribution: SuSE 9.3 Pro
Posts: 13
Original Poster
Rep:
|
Thanks Matir,
That did it. I need to be more observant of what I'm dereferencing in the future.
- Stack Overflow
|
|
|
|
01-27-2005, 03:48 PM
|
#4
|
|
Moderator
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,505
Rep: 
|
No problem. A couple of questions, out of curiousity:
1. What are you working on, if I can ask?
2. Which of the two methods do you choose to use? I generally use the second just because it looks cleaner, though the first is more explicit.
|
|
|
|
01-27-2005, 05:11 PM
|
#5
|
|
LQ Newbie
Registered: Jan 2004
Location: USA
Distribution: SuSE 9.3 Pro
Posts: 13
Original Poster
Rep:
|
Hi,
Quote:
|
1. What are you working on, if I can ask?
|
I just started learning UNIX Network Programming a few months ago, say, about 2. I have been programming in the C language for a good 4 years. Though, sockets, and other functions are kind of new to me. I occasionally have a problem in my source code, as trivial as this one might I add. To answer this question, I'm just trying to make a test echo server. Though, I am adding a hash table to store and organize all the clients that connect. So I can look them up, remove them, add them, and other fun stuff. It's just to test my C ability.
Quote:
|
2. Which of the two methods do you choose to use? I generally use the second just because it looks cleaner, though the first is more explicit.
|
I chose the first, as of now. Since I am passing a few other pointers, for instance integers, I wanted to keep the dereference style the same. Since none of the variables I pass are truely pointers, but rather memory addresses to local variables, I didn't want to get into a habit of using the pointer to struct shorthand (->) in this type of situation. I admit, the second method looks cleaner, though I am looking for more of an explict look.
I've been working on this project for some time now, though now I am developing it under the Linux environment. I've spent a while allowing executable options. And also able to read in a configuration file and parse it. Though, I spent the last few hours cleaning up all the compiler warnings and errors for GCC 3.4.3 since it's a bit more picky than usual, esp. with the -Wall, -pedantic, -ansi, and -std=c89 flags. Though, I managed to make the build perfectly clean. I am considering releasing the source code, but first I need to figure out why all connections never make it through to the server. No matter how many clients I write.
Hope this answers your questions,
- Stack Overflow
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:40 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|