![]() |
| | #1 |
| C Programmer Join Date: Apr 2004
Posts: 477
| Accessing member functions of sockaddr_in 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;
}
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 | |
| | #2 |
| C Programmer 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;
}
- 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 | |
| | #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 | |
| | #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 | |
| | #5 |
| C Programmer 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 | |
| | #6 |
| Guest 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 | |
| | #7 |
| C Programmer 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 | |
| | #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 | |
| | #9 |
| C Programmer 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |