Socket in c TCP

Discussion in 'Tech Discussion' started by Zaart, May 7, 2020.

  1. Zaart

    Zaart Deer Master Race

    Joined:
    Oct 15, 2016
    Messages:
    1,320
    Likes Received:
    2,011
    Reading List:
    Link
    Hi guys,
    for a school project i have to use sockets to create a server, i think i managed to do it but i am supposed to use Telnet to test it an whenever i try i get a "telnet: connect to address 0.0.0.0: Connection refused" so i guess it is with the IP that i'm wrong
    the simplified version of my code that should still work for one connection is

    Code:
    int main(int argc , char *argv[])
    {
        int sock;
        int fd = 0;
        int err = 0;
        struct sockaddr_in sock_data;
        socklen_t addr_size;
        char *path;
    
        path = get_current_dir_name();
        if (argc == 2)
            printf("%s", Usage);
        sock = socket(AF_INET , SOCK_STREAM , 0);
        if (sock == -1) {
            printf("Could not create socket");
            return (84);
        }
        memset(&sock_data, 0, sizeof(struct sockaddr_in));
        sock_data.sin_family = AF_INET;
        sock_data.sin_port = htons(5133);
        sock_data.sin_addr.s_addr = htonl(INADDR_ANY);
        printf("%s, %s, %s\n", path, sock_data.sin_addr, inet_ntoa(sock_data.sin_addr));
        if (bind(sock, (const struct sockaddr *) &sock_data, sizeof(sock_data)) == -1)
            printf("Error with binding\n");
        if (listen(sock, LISTEN_BACKLOG) == -1)
            printf("Error with listen");
        addr_size = sizeof(struct sockaddr_in);
        err = accept(sock, (struct sockaddr *) &sock_data, &addr_size);
        while ((sock = accept(sock, NULL, NULL)) < 0) {
            err = 0;
        };
        return 84;
    }
    so do you know where i went wrong?
     
  2. FusaHatesHarems

    FusaHatesHarems Member

    Joined:
    Oct 27, 2019
    Messages:
    14
    Likes Received:
    15
    Reading List:
    Link
    What's a server
     
  3. Deleted member 155674

    Deleted member 155674 Guest

    Reading List:
    Link
    Did you try 'localhost' instead of '0.0.0.0' :hmm:? I mean you are testing the server socket on your machine, right?
     
    Zaart and TamaSaga like this.
  4. TamaSaga

    TamaSaga Well-Known Member

    Joined:
    Oct 11, 2016
    Messages:
    1,726
    Likes Received:
    2,173
    Reading List:
    Link
    Why not 127.0.0.1?

    Connection refused generally means that nothing is listening on that address and port. So make sure you're connecting on port 5133 too.
     
    Zaart and Deleted member 155674 like this.
  5. Zaart

    Zaart Deer Master Race

    Joined:
    Oct 15, 2016
    Messages:
    1,320
    Likes Received:
    2,011
    Reading List:
    Link
    i still got connection refused but thanks
    and yup ^^

    i get the same result with 127.0.0.1 and when i try to print the adresse i get 0.0.0.0
    how do i make sure that i am connecting on 5133?
     
    Deleted member 155674 likes this.
  6. TamaSaga

    TamaSaga Well-Known Member

    Joined:
    Oct 11, 2016
    Messages:
    1,726
    Likes Received:
    2,173
    Reading List:
    Link
    Telnet 127.0.0.1:5133

    Or something like that
     
    Zaart likes this.
  7. Zaart

    Zaart Deer Master Race

    Joined:
    Oct 15, 2016
    Messages:
    1,320
    Likes Received:
    2,011
    Reading List:
    Link
    yeah i'm truly an idiot Q-Q
    thanks man, i can't believe i spent three days re-writing my program because i forgot one stupid thing XD
     
  8. Zaart

    Zaart Deer Master Race

    Joined:
    Oct 15, 2016
    Messages:
    1,320
    Likes Received:
    2,011
    Reading List:
    Link
  9. TamaSaga

    TamaSaga Well-Known Member

    Joined:
    Oct 11, 2016
    Messages:
    1,726
    Likes Received:
    2,173
    Reading List:
    Link
    No worries. While you do need to ask more questions, this experience will help you in the long run as it exposes you to all sorts of weirdness that you won't get to see if you keep getting it right.
     
    Zaart likes this.
  10. Zaart

    Zaart Deer Master Race

    Joined:
    Oct 15, 2016
    Messages:
    1,320
    Likes Received:
    2,011
    Reading List:
    Link
    true ^^