1

I couldn't figure out why I can't make a connection through my public IP. When using a local IP it works.

I have the proper firewall port exceptions, and my router NAT has the configurations for port forwarding to my machine.

This is a fairly basic program that is able to connect through my local IP, but gets "connection refused" when using the public one.

Any help? (I've also setup a nginx server which can't be accessed from public IP, but works through local one)

Client:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main()
{
    sockaddr_in address;
    int res;
    int sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (-1 == sfd)
    {
        perror("cannot create socket");
        exit(EXIT_FAILURE);
    }

    memset(&address, 0, sizeof(address));

    address.sin_family = AF_INET;
    address.sin_port = htons(6511);
    res = inet_pton(AF_INET, "201.93.179.98", &address.sin_addr); /* connect through public IP doesn't work */
    //res = inet_pton(AF_INET, "192.168.1.71", &address.sin_addr);

    if (0 > res)
    {
        perror("error: first parameter is not a valid address family");
        close(sfd);
        exit(EXIT_FAILURE);
    }
    else if (0 == res)
    {
        perror("char string (second parameter does not contain valid ipaddress)");
        close(sfd);
        exit(EXIT_FAILURE);
    }

    if (-1 == connect(sfd, (sockaddr *)&address, sizeof(address)))
    {
        perror("connect failed");
        close(sfd);
        exit(EXIT_FAILURE);
    }

    /* perform read write operations ... */
    char msg[] = "Hello World";
    write(sfd, (void *)msg, sizeof(msg));
    printf("sent %s\n", msg);

    shutdown(sfd, SHUT_RDWR);

    close(sfd);
    return EXIT_SUCCESS;
}

Server:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main()
{
    sockaddr_in address;
    int sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    if(-1 == sfd)
    {
        perror("can not create socket");
        exit(EXIT_FAILURE);
    }

    memset(&address, 0, sizeof(address));

    address.sin_family = AF_INET;
    address.sin_port = htons(6511);
    address.sin_addr.s_addr = inet_addr("192.168.1.71"); /* serve on given interface */

    if(-1 == bind(sfd, (sockaddr *)&address, sizeof(address)))
    {
        perror("error bind failed");
        close(sfd);
        exit(EXIT_FAILURE);
    }

    if(-1 == listen(sfd, 10))
    {
        perror("error listen failed");
        close(sfd);
        exit(EXIT_FAILURE);
    }

    for(;;)
    {
        int cfd = accept(sfd, NULL, NULL);

        if(0 > cfd)
        {
            perror("error accept failed");
            close(sfd);
            exit(EXIT_FAILURE);
        }

        /* perform read write operations ... 
           read(cfd,buff,size)*/
        char msg[100] = {};
        read(cfd, (void *)msg, sizeof(msg));
        printf("got %s\n", msg);

        if (-1 == shutdown(cfd, SHUT_RDWR))
        {
            perror("can not shutdown socket");
            close(cfd);
            close(sfd);
            exit(EXIT_FAILURE);
        }
        close(cfd);
    }

    close(sfd);
    return EXIT_SUCCESS;  
}
7
  • 1
    Are you trying to access the external IP from your internal LAN behind NAT? Search for 'NAT Hairpin' if that's the case.
    – Cha0s
    May 27, 2013 at 13:58
  • 1
    @Cha0s, yes, I'm trying to access my own machine in the LAN (server/client in the same machine) and there's a fixed public IP setup with port fowarding configured in router.
    – oblitum
    May 27, 2013 at 14:01
  • 1
    In order for you to be able to access the external IP from your LAN you need to implement the so called 'NAT Hairpin' feature. Depending on your router this might be possible - or not. It's clearly a networking issue with your NAT setup and has nothing to do with your application.
    – Cha0s
    May 27, 2013 at 14:05
  • @Cha0s, I have another unrelated program that I can run in another machine, and start some specific server in mine, they communicate. So, is this a networking issue related to accessing my own machine solely? (Since I see LAN is working between different machines using the public IP)
    – oblitum
    May 27, 2013 at 14:11
  • you mean if NAT Hairpin is related only to your PC? If that's your question, then no, this is an issue on your router, not your PC. MDMarra posted a useful topic on the matter.
    – Cha0s
    May 27, 2013 at 14:36

1 Answer 1

2

You need to configure your router/firewall to allow NAT hairpins. If you have a SOHO router, it likely doesn't support this. If you have something like a Cisco ASA, you should read the documentation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .