【问题标题】:Weird IP address `0.0.0.0` being setup for the server. Not sure if it works or not正在为服务器设置奇怪的 IP 地址“0.0.0.0”。不确定它是否有效
【发布时间】:2015-10-31 15:29:11
【问题描述】:

我的目标是以发送者 服务器 -> 接收者的方式建立 TCP/IP 连接。

我有一个服务器、发送者和接收者。服务器初始部分如下所示:

int main(int argc, char *argv[]){
  int welcomeSocket, senderSocket;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;

  if (2 != argc) {
       fprintf(stderr, "Usage: %s <port>\n", argv[0]);
           exit(1);

  }
  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
  welcomeSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */
  serverAddr.sin_family = AF_INET;
  /* Set port number, using htons function to use proper byte order */
  serverAddr.sin_port = htons(atoi(argv[1]));
  /* Set IP address to localhost */
  serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);

//int len=20;
//char sbuffer[len];

//inet_ntop(AF_INET, &(serverAddr.sin_addr), sbuffer, len);
//printf("address:%s\n",sbuffer);

  /* Set all bits of the padding field to 0 */
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Bind the address struct to the socket ----*/
  bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

  /*---- Listen on the socket, with 5 max connection requests queued ----*/
  if(listen(welcomeSocket,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  /*---- Accept call creates a new socket for the incoming connection ----*/
  addr_size = sizeof serverStorage;
  senderSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);

//~~~~Some more code~~~
}

但是,这会将服务器 IP 地址设置为 0.0.0.0。我不知道这是否正确,但直到现在我都手动将 IP 地址设置为 127.0.1.1,这是我在终端中输入 hostname -i 时得到的。

所以,目前我让我的发件人连接到同一个 IP 地址,但由于我在同一台计算机上处​​理所有文件,我不知道它是否可以在网络中具有这个奇怪 IP 地址的其他计算机上工作@ 987654325@。有人可以向我澄清(并可能帮助解决)这个小问题吗?我曾尝试阅读其他解决方案,试图解释 0.0.0.0127.0.0.1 之间的区别,但我找不到任何与 TCP/IP 连接中服务器和发送方之间的连接/通信性能相关的内容。

然后IPv4地址在我的系统设置中列为129.15.78.12,但又一次,不确定应该为serverreceiver使用哪个地址。

【问题讨论】:

  • 0.0.0.0 等同于INADDR_ANY,这意味着您希望接收发送到与您的计算机关联的每个 IP 地址的数据包。如果您使用INADDR_LOOPBACK (127.0.0.1),则不会接受来自外部的数据包。这是否好,取决于你想完成什么。
  • 你应该看看这个帖子stackoverflow.com/questions/3655723/…
  • @szczurcio 我的最终目标是让发送者连接到服务器,从服务器发送和接收信息,最终将信息发送到服务器,服务器随后将其发送给接收器。这是否说明了我的目标?
  • @terencehill 我之前发过帖子,但结果非常混乱。如果我在同一个网络中有许多计算机,那么它们是否能够通信?
  • 所有程序都将在一台计算机上运行吗?如果是,您可以毫无问题地使用INADDR_LOOPBACK,否则使用INADDR_ANY。如果我解释loopback地址总是你自己的计算机,这可能会更有意义;换句话说,如果您将某些东西“发送到”该地址,那么您将其发送给自己。

标签: c tcp ip


【解决方案1】:

不同之处在于,通过使用 INADDR_ANY,您可以将服务绑定到所有接口,如

中所述

http://man7.org/linux/man-pages/man7/ip.7.html

understanding INADDR_ANY for socket programming - c

除此之外,如果您保留此配置,任何尝试访问您的服务器的计算机都会连接,如果它使用有效的外部 IP 地址,就像您在系统设置中看到的那样。希望这能澄清您的问题。

【讨论】:

    猜你喜欢
    • 2011-04-09
    • 2013-12-18
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 2016-11-10
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多