C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-27-2005, 12:32 PM   #1
C Programmer
 
Stack Overflow's Avatar
 
Join Date: Apr 2004
Posts: 477
Accessing member functions of sockaddr_in

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:
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
__________________
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
Stack Overflow is offline   Reply With Quote
Old 01-27-2005, 01:18 PM   #2
C Programmer
 
Stack Overflow's Avatar
 
Join Date: Apr 2004
Posts: 477
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:
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;
}
Alternatively, I could use the struct pointer syntax (->). Though, is that legal in this case?


- Stack Overflow
__________________
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
Stack Overflow is offline   Reply With Quote
Old 01-27-2005, 03:17 PM   #3
Registered User
 
Join Date: Sep 2004
Posts: 13
Yeah, it's legal. Why shouldn't be, there's no excepcion here, I guess


p->field == (*p).field
edugarcia is offline   Reply With Quote
Old 01-27-2005, 08:05 PM   #4
Registered User
 
Join Date: Jul 2003
Posts: 450
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?
curlious is offline   Reply With Quote
Old 01-28-2005, 11:00 AM   #5
C Programmer
 
Stack Overflow's Avatar
 
Join Date: Apr 2004
Posts: 477
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
__________________
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
Stack Overflow is offline   Reply With Quote
Old 01-28-2005, 12:52 PM   #6
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 5,034
>> 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?
Sebastiani is offline   Reply With Quote
Old 01-28-2005, 12:58 PM   #7
C Programmer
 
Stack Overflow's Avatar
 
Join Date: Apr 2004
Posts: 477
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!


- Stack Overflow
__________________
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
Stack Overflow is offline   Reply With Quote
Old 01-28-2005, 07:36 PM   #8
Registered User
 
Join Date: Jul 2003
Posts: 450
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.
curlious is offline   Reply With Quote
Old 01-29-2005, 11:26 AM   #9
C Programmer
 
Stack Overflow's Avatar
 
Join Date: Apr 2004
Posts: 477
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
__________________
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
Stack Overflow is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting an error with OpenGL: collect2: ld returned 1 exit status Lorgon Jortle C++ Programming 6 05-08-2009 08:18 PM
issues with accessing parent member functions by typecasting this pointer donglee C++ Programming 11 08-28-2008 04:10 AM
Static member functions more efficient? drrngrvy C++ Programming 6 06-16-2006 07:07 AM
Class member variables only readable by member functions? _Elixia_ C++ Programming 4 10-10-2003 03:52 PM
trouble defining member functions dP munky C++ Programming 7 04-13-2003 08:52 PM


All times are GMT -6. The time now is 11:02 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22