Centos

無法通過公網IP連接到自己的機器

  • May 27, 2013

我無法弄清楚為什麼我無法通過我的公共 IP 建立連接。使用本地IP時,它可以工作。

我有適當的防火牆埠例外,並且我的路由器 NAT 具有用於將埠轉發到我的機器的配置。

這是一個相當基本的程序,能夠通過我的本地 IP 進行連接,但在使用公共 IP 時會出現“連接被拒絕”。

有什麼幫助嗎?(我還設置了一個無法從公共 IP 訪問的 nginx 伺服器,但可以通過本地伺服器執行)

客戶:

#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;
}

伺服器:

#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;  
}

您需要配置您的路由器/防火牆以允許 NAT 髮夾。如果您有 SOHO 路由器,它可能不支持此功能。如果您有 Cisco ASA 之類的東西,您應該閱讀文件。

引用自:https://serverfault.com/questions/511161