Thread: C Socket programming problem

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    C Socket programming problem

    Hi Guys and Girls... I should imagine it is actually mainly guys here, but best not to be sexist!

    I have written this code

    Code:
    #include <stdio.h>          /* stderr, stdout */
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <netinet/ip.h> /* superset of previous */
    #include <string.h>
    
    int net_listen() {
        int fn_socket, fn_bind_result;
        fn_socket = socket(AF_INET, SOCK_STREAM, 0);
        if (fn_socket < 0) return -1;
        printf ("net_listen: socket created.\n");
        struct sockaddr_in fn_bind_addr;
        memset(&fn_bind_addr, 0, sizeof(struct sockaddr_in));
        fn_bind_addr.sin_family = AF_INET;
        fn_bind_addr.sin_port = htons(4080);
        fn_bind_addr.sin_addr.s_addr = INADDR_ANY;
        fn_bind_result = bind(fn_socket, &fn_bind_addr, sizeof(struct sockaddr_in));
        if (fn_bind_result < 0) return -1;
        printf ("net_listen: bind success");
    }
    Codeblocks says this when I try to compile it:

    /home/jamie/aws/socket_linux.cpp|18|error: cannot convert ‘sockaddr_in*’ to ‘const sockaddr*’ for argument ‘2’ to ‘int bind(int, const sockaddr*, socklen_t)’|
    Anyone able to tell me the problem?

    Jamie

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    From experience...

    SOCKADDR_IN is a fine way to alias a SOCKADDR struct to get at the bits inside... but it's not a struct you want to be passing around to various functions. Almost none of the socket procedures can use it directly.

    You could try...
    Code:
       fn_bind_result = bind(fn_socket, (sockaddr*)&fn_bind_addr, sizeof(struct sockaddr_in));
    But I ain't makin' no promises.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    14

    Thumbs up

    my two cents:

    1. from your commandline i suspect you code onto linux, i am not that familiar with linux in include files, on bsd for this to work you will need at least(coding in c not c++)

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    2. for your socket problem commentater almost had it, if you look into the definition of bind you see it is asking for the struct so give it what it needs:
    Code:
    fn_bind_result = bind(fn_socket, (struct sockaddr *)&fn_bind_addr, sizeof(fn_bind_addr));
    i changed the sizeof as well to the sizeof fn_bind_addr.

    have fun,
    fc

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm working from windows ... yet another OS in the mix.

    In windows sockaddr is a typedef so no need for (struct sockaddr*), in fact I could use (PSOCKADDR) in the cast.

    Changing the size of is a good idea...

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    14

    Talking

    Quote Originally Posted by CommonTater View Post
    I'm working from windows ... yet another OS in the mix.
    right, there were some other OS in the wild

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why the local socket program occur core dump?
    By chenayang in forum Linux Programming
    Replies: 16
    Last Post: 08-16-2010, 08:39 AM
  2. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  3. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 04:46 AM
  4. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 07:01 AM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 10:01 AM