Accessing member functions of sockaddr_in

This is a discussion on Accessing member functions of sockaddr_in within the Linux Programming forums, part of the Platform Specific Boards category; Hello, I am working on a small project in Linux that allows communication to and from a server. So far ...

  1. #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.

  2. #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.

  3. #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

  4. #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?

  5. #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.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    >> 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?
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #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.

  8. #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.

  9. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 11
    Last Post: 08-28-2008, 04:10 AM
  3. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  4. Class member variables only readable by member functions?
    By _Elixia_ in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2003, 03:52 PM
  5. trouble defining member functions
    By dP munky in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2003, 08:52 PM

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