简单 UDP 客户端/服务器应用程序的问题

发布于 2024-10-06 05:16:36 字数 2181 浏览 1 评论 0原文

好的。所以我有一个简单的客户端程序通过 UDP 将图像发送到指定的 IP 地址。在指定的 IP 处,服务器程序正在侦听并且应该接收图像。

当我在本地主机上尝试这两个程序时(即它们都在 127.0.0.1 上运行),它可以工作并且服务器接收图像。当我尝试将客户端和服务器程序放在同一网络中的不同 PC 上时,它不起作用。

147.232.24.163是服务器IP,147.232.24.150是客户端IP。

这是我的客户端程序:

  // Initialize UDP.    
  struct sockaddr_in server;    
  int n_sent;
  int socketId = socket(AF_INET, SOCK_DGRAM, 0);    
  if (socketId < 0)    
  {    
    cout << "Problem creating socket." << endl;    
  }    
  server.sin_family = AF_INET;    
  server.sin_addr.s_addr = inet_addr("147.232.24.163");    
  // Establish the server port number - we must use network byte order!    
  server.sin_port = htons(42001);

  for ( int iter = 0; iter < 60; iter++ )    
  {
      // load image into jpegBuf

      n_sent = sendto(socketId, reinterpret_cast<char*> (&jpegBuf[0]), jpegBuf.size(), 0, (struct sockaddr*) &server, sizeof(server));    
      if (n_sent < 0) {    
        cout << "Problem sending data." << endl;    
      }    
  }

  close(socketId);

这是我的服务器程序:

int main()
{
    int bufferSize = 1024000;
    int iSockFd = -1;
    int iLength = 0;
    struct sockaddr_in servAddr, cliAddr;
    char buff[bufferSize];
    iSockFd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    memset(&servAddr, 0, sizeof(servAddr));
    memset(&cliAddr, 0, sizeof(cliAddr));

    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = inet_addr("147.232.24.163"); 
    servAddr.sin_port = htons(42001);
    int cliAddrLen = sizeof(struct sockaddr_in);
    int bindRet = bind(iSockFd, (struct sockaddr*)&servAddr, sizeof(servAddr));
    cout << "Bind returned " << bindRet << endl;

    int i = 0;
    while (true)
    {
        int iRcvdBytes=recvfrom(iSockFd, buff, bufferSize, 0, 
        (struct sockaddr*)&cliAddr, (socklen_t*)&cliAddrLen);

        if (0 == i % 5)
        {
            cout << "Received " << iRcvdBytes << " bytes from the client" << endl;
        }


        i++;
    }
    close(iSockFd);
    return 0;
}

有什么想法为什么它不起作用吗?我没有收到任何错误消息。

Ok. So I have a simple client program sending images over UDP to a specified IP address. At the specified IP a server program is listening and should receive the images.

When I try both programs on localhost (i.e. they are both running at 127.0.0.1) it works and the server receives the images. When I try to put the client and the server programs on different PCs in the same network it does not work.

147.232.24.163 is the server IP, 147.232.24.150 is the client IP.

Here is my client program:

  // Initialize UDP.    
  struct sockaddr_in server;    
  int n_sent;
  int socketId = socket(AF_INET, SOCK_DGRAM, 0);    
  if (socketId < 0)    
  {    
    cout << "Problem creating socket." << endl;    
  }    
  server.sin_family = AF_INET;    
  server.sin_addr.s_addr = inet_addr("147.232.24.163");    
  // Establish the server port number - we must use network byte order!    
  server.sin_port = htons(42001);

  for ( int iter = 0; iter < 60; iter++ )    
  {
      // load image into jpegBuf

      n_sent = sendto(socketId, reinterpret_cast<char*> (&jpegBuf[0]), jpegBuf.size(), 0, (struct sockaddr*) &server, sizeof(server));    
      if (n_sent < 0) {    
        cout << "Problem sending data." << endl;    
      }    
  }

  close(socketId);

Here is my server program:

int main()
{
    int bufferSize = 1024000;
    int iSockFd = -1;
    int iLength = 0;
    struct sockaddr_in servAddr, cliAddr;
    char buff[bufferSize];
    iSockFd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    memset(&servAddr, 0, sizeof(servAddr));
    memset(&cliAddr, 0, sizeof(cliAddr));

    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = inet_addr("147.232.24.163"); 
    servAddr.sin_port = htons(42001);
    int cliAddrLen = sizeof(struct sockaddr_in);
    int bindRet = bind(iSockFd, (struct sockaddr*)&servAddr, sizeof(servAddr));
    cout << "Bind returned " << bindRet << endl;

    int i = 0;
    while (true)
    {
        int iRcvdBytes=recvfrom(iSockFd, buff, bufferSize, 0, 
        (struct sockaddr*)&cliAddr, (socklen_t*)&cliAddrLen);

        if (0 == i % 5)
        {
            cout << "Received " << iRcvdBytes << " bytes from the client" << endl;
        }


        i++;
    }
    close(iSockFd);
    return 0;
}

Any ideas why it does not work? I don't get any error messages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

德意的啸 2024-10-13 05:16:36

这不是解决方案,但您的代码应该以与发送代码检查 sendto 结果相同的方式检查 iRcvdbytes 错误。

返回 0 表示套接字已正常关闭(如果是面向连接的 - 此处不适用),SOCKET_ERROR 表示错误(至少在 WinSock2 中)。

发送端的套接字需要使用 IPPROTO_UDP 创建,而不是 0。这肯定会导致您的失败。

This is not a solution but your code should be checking iRcvdbytes for error in the same way as the send code checks the result of sendto.

A return of 0 means the socket was closed gracefully (if connection-oriented - that should not apply here), SOCKET_ERROR means an error (in WinSock2 at least).

The socket on the send side needs to be created with IPPROTO_UDP, not 0. This could definitely be causing your failure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文