This question has already been answered:

I make a network console toy for a course, if you connect via 127.0.0.1 or via local ip, then everything works, but when I try to connect a client server through an Internet ip (like 84.242.206.234), does the client not connect, if not? Server:

int sock, n, port = def_port; int; struct sockaddr_in servaddr; sock = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(port); servaddr.sin_addr.s_addr = htonl(INADDR_ANY); bind(sock, (struct sockaddr *) &servaddr, sizeof(servaddr)); printf("Порт: %d ожидаю подключения\n", ntohs(servaddr.sin_port)); listen(sock, 0); int tmp = sock; sock = accept(sock, (struct sockaddr *) NULL, NULL); 

Customer:

 int sock, n, port = def_port; struct sockaddr_in servaddr; sock = socket(AF_INET, SOCK_STREAM, 0); char ip[100]; printf("Введите ip адрес: "); scanf("%s", ip); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(port); if(inet_aton(ip, &servaddr.sin_addr) == 0){ printf("Неправильный ip адрес\n"); return; } if(connect(sock, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0){ printf("Не удалось подключиться\n"); return; } 

Reported as a duplicate by members of aleksandr barakin , gbg , user194374, Streletz , cheops Jul 2, '16 at 21:14 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • How are you connected to the internet? - gbg
  • Additionally, the firewall on the computer should have a rule allowing incoming connections to the corresponding port ... - gecube

2 answers 2

Most likely your problem is that you are not running from the server. And through the home computer, which works through the router.

The bottom line is that for a home PC, the provider dynamically issues ip addresses within the network. And you need a static address for a full server. Either to forward the port (the provider will not allow, force you to buy a static ip).

Advice to use VPS, or any VPN (for example, hamachi).

    Everything turned out to be easier, it was necessary to open the port on the router