PDA

View Full Version : Accessing member functions of sockaddr_in



Stack Overflow
01-27-2005, 12:32 PM
Hello,

I am working on a small project in Linux 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:
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

Stack Overflow
01-27-2005, 01:18 PM
Scratch that,

Just got word that I shouldn't dereference sock.sin_family because sock.sin_family does not exist. Rather I should dereference sock:
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;
}Alternatively, I could use the struct pointer syntax (->). Though, is that legal in this case?


- Stack Overflow

edugarcia
01-27-2005, 03:17 PM
Yeah, it's legal. Why shouldn't be, there's no excepcion here, I guess


p->field == (*p).field

curlious
01-27-2005, 08:05 PM
Just a comment I like your seperation into routines. When I had time I was exploring the idea of wrapping different functionality in C++ but school started and I just did a very linear walk through. Now my interests have wanderd (Eiffel) but nice looking code.
What type of networking app are you creating. Have you explored threads yet?

Stack Overflow
01-28-2005, 11:00 AM
Well,

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. I have not checked out threads as of yet, though maybe I will.

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 yesterday 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 -g, -O2, -Wall, -pedantic, -ansi, and -std=c89 flags. Though, I managed to make the build perfectly clean.

Right now, I need to figure out why all connections never make it through to the server. No matter how many clients I write.


- Stack Overflow

Sebastiani
01-28-2005, 12:52 PM
>> Right now, I need to figure out why all connections never make it through to the server.

did you remember to bind() and listen()? or, do you have a firewall running?

Stack Overflow
01-28-2005, 12:58 PM
Yes,

I remembered to bind() and listen(), though I found the problem. I had a loop that ran through all the waiting clients to process, but my loop was wrong. Thus sending my server into an infinte loop. Once I fixed it, I was able to connect and send data to my server.

P.S. This is a multi-client server. So it takes more handling and care.

Now I am working on sending data back to the client. This is going to be fun! :cool:


- Stack Overflow

curlious
01-28-2005, 07:36 PM
It sounds like you will want to use select which takes a set of file discriptors in this case socket discriptors then if the discriptor is flagged to be watched it returns a subset based on the active sockets. You would then be able to put these into whatever data structure you wish. I am curious if you have implemented your hash table and how you monitor for activity otherwise.

Stack Overflow
01-29-2005, 11:26 AM
Ah,

Yes. I so far am using select(). Though, I'm using two instances. One to look for incoming connections, and the other, to read in the local input stream [stdin]. Through thorough testing, I succeeded at accomplishing the both desired effects.

Now, all is running well. I am working on a client, so I can send and receive information in a nice fashion.

My hashing function just stores the clients IP address and Port they connect with, dynamically. Each client's node includes a name and a few other miscellanous client storage variables. I'm going to try and make a chat server that takes in x amount of people, etc... So far my server is 75% complete, and my client 50% complete.


- Stack Overflow