LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-27-2005, 12:54 PM   #1
Stack Overflow
LQ Newbie
 
Registered: Jan 2004
Location: USA
Distribution: SuSE 9.3 Pro
Posts: 13

Rep: Reputation: 0
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
 
Old 01-27-2005, 01:06 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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;
}
 
Old 01-27-2005, 01:16 PM   #3
Stack Overflow
LQ Newbie
 
Registered: Jan 2004
Location: USA
Distribution: SuSE 9.3 Pro
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks Matir,

That did it. I need to be more observant of what I'm dereferencing in the future.


- Stack Overflow
 
Old 01-27-2005, 03:48 PM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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.
 
Old 01-27-2005, 05:11 PM   #5
Stack Overflow
LQ Newbie
 
Registered: Jan 2004
Location: USA
Distribution: SuSE 9.3 Pro
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
  


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
accessing functions using strings in runtime shyam_d_sundar Programming 3 05-18-2005 08:02 AM
error while accessing math functions in kernel modules dypgrp Programming 0 01-19-2005 09:12 AM
accessing functions in kernel modules starbuck8968 Programming 5 12-14-2004 09:42 AM
c to GCChange of member functions in iostream from GCC296 to Gcc 3.2.2 Basiltp Programming 0 11-29-2004 03:19 PM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

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

All times are GMT -5. The time now is 07:42 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