UDP Winsock编程

发布于 2024-09-26 06:35:13 字数 1104 浏览 3 评论 0 原文

我是套接字编程的初学者。我想从端口连续接收udp数据包。为此,我创建了套接字并使用我在程序中完成的绑定和接收调用。我在缓冲区中存储 udp 数据包。如何逐包接收。如何为特定时间间隔设置条件?提前致谢。

static int recvData = 1;
sockID = socket(AF_INET, SOCK_DGRAM, 0);
 if(sockID < 0)
 {
  printf("Socket creation error\n");
        WSACleanup();
 }
 else
 {
  printf("Socket Created\n");
 }

 fepAddr.sin_family = AF_INET;
 fepAddr.sin_port = htons(inputData.portNo);
 fepAddr.sin_addr.s_addr = inet_addr(inputData.destIPAddr);

 if (bind(sockID, (struct sockaddr *)&fepAddr, sizeof(fepAddr)) == SOCKET_ERROR)
 {
  printf("bind() failed: %ld.\n", WSAGetLastError());
  closesocket(sockID);
  return 0;
 }

 else
 {
  printf("bind() is OK!\n");
 }

 memset(udpBuf,sizeof(udpBuf),0);
 while (recvData)
 {
  printf("receiving data\n");
  recvResult =  recvfrom( sockID, udpBuf, sizeof(udpBuf), 0,(struct sockaddr *)&fepAddr, &sock_len); 


  fprintf(udp, "%s", udpBuf);
  //fwrite(udpBuf, sizeof(udpBuf), 1, udp);
  recvData-- ;
 }
exit:
    if(udp) 
    {
         fclose(udp);
         udp = 0; 
    }

 //shutdown socket
 closesocket(sockID); 
 fclose(udp);

I'm the beginner in socket programming. I want to receive udp packets continuously from the port. For that I created socket and using bind and recv calls I have done with my program. In a buffer I'm storing the udp packets. How to receive packet by packet. How to put condition for particular time interval? Thanks in advance.

static int recvData = 1;
sockID = socket(AF_INET, SOCK_DGRAM, 0);
 if(sockID < 0)
 {
  printf("Socket creation error\n");
        WSACleanup();
 }
 else
 {
  printf("Socket Created\n");
 }

 fepAddr.sin_family = AF_INET;
 fepAddr.sin_port = htons(inputData.portNo);
 fepAddr.sin_addr.s_addr = inet_addr(inputData.destIPAddr);

 if (bind(sockID, (struct sockaddr *)&fepAddr, sizeof(fepAddr)) == SOCKET_ERROR)
 {
  printf("bind() failed: %ld.\n", WSAGetLastError());
  closesocket(sockID);
  return 0;
 }

 else
 {
  printf("bind() is OK!\n");
 }

 memset(udpBuf,sizeof(udpBuf),0);
 while (recvData)
 {
  printf("receiving data\n");
  recvResult =  recvfrom( sockID, udpBuf, sizeof(udpBuf), 0,(struct sockaddr *)&fepAddr, &sock_len); 


  fprintf(udp, "%s", udpBuf);
  //fwrite(udpBuf, sizeof(udpBuf), 1, udp);
  recvData-- ;
 }
exit:
    if(udp) 
    {
         fclose(udp);
         udp = 0; 
    }

 //shutdown socket
 closesocket(sockID); 
 fclose(udp);

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

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

发布评论

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

评论(1

泪眸﹌ 2024-10-03 06:35:13

recvfrom() 逐包接收 UDP 数据。如果给定的数据包太大,recvfrom() 将返回错误。至于计时,您可以使用 select() 来知道套接字何时可读。

尝试这样的事情:

sockID = socket(AF_INET, SOCK_DGRAM, 0); 
if (sockID == INVALID_SOCKET) 
{ 
  printf("Socket creation error\n"); 
  goto exit;
} 

printf("Socket Created\n"); 

memset(&fepAddr, 0, sizeof(fepAddr));
fepAddr.sin_family = AF_INET; 
fepAddr.sin_port = htons(inputData.portNo); 
fepAddr.sin_addr.s_addr = inet_addr(inputData.destIPAddr); 

if (bind(sockID, (struct sockaddr *)&fepAddr, sizeof(fepAddr)) == SOCKET_ERROR) 
{ 
  printf("bind() failed: %ld.\n", WSAGetLastError()); 
  goto exit;
} 

printf("bind() is OK!\n"); 

memset(udpBuf, 0, sizeof(udpBuf)); 

printf("receiving data\n"); 
while (...) 
{
  printf("."); 

  recvResult = recvfrom(sockID, udpBuf, sizeof(udpBuf), 0, (struct sockaddr *)&fepAddr, &addr_len);  
  if (recvResult == SOCKET_ERROR)
  {
    if (WSAGetLastError() != WSAEWOULDBLOCK)
    {
      printf("\nrecvfrom() failed: %ld.\n", WSAGetLastError()); 
      goto exit;
    }

    fd_set fd;
    FD_ZERO(&fd);
    FD_SET(sockID, &fd);

    timeval t;
    t.tv_sec = ...; // seconds
    t.tv_usec = ...; // microseconds

    selectResult = select(0, &fd, NULL, NULL, &t);
    if (selectResult == SOCKET_ERROR)
    {
      printf("\nselect() failed: %ld.\n", WSAGetLastError()); 
      goto exit;
    }

    if (selectResult == 0)
    {
      printf("\nsocket timed out.\n");
      goto exit;
    }

    continue;
  }

  if (recvResult > 0)
    fwrite(udpBuf, recvResult, 1, udp); 
} 

exit: 
  if (udp != 0)
  { 
    fclose(udp); 
    udp = 0;  
  } 

  if (sockID != INVALID_SOCKET) 
  {
    closesocket(sockID);  
    sockID = INVALID_SOCKET;
  }

recvfrom() receives UDP data packet-by-packet. If a given packet is too large, recvfrom() will return an error. As for timing, you can use select() to know when the socket is readable.

Try something like this:

sockID = socket(AF_INET, SOCK_DGRAM, 0); 
if (sockID == INVALID_SOCKET) 
{ 
  printf("Socket creation error\n"); 
  goto exit;
} 

printf("Socket Created\n"); 

memset(&fepAddr, 0, sizeof(fepAddr));
fepAddr.sin_family = AF_INET; 
fepAddr.sin_port = htons(inputData.portNo); 
fepAddr.sin_addr.s_addr = inet_addr(inputData.destIPAddr); 

if (bind(sockID, (struct sockaddr *)&fepAddr, sizeof(fepAddr)) == SOCKET_ERROR) 
{ 
  printf("bind() failed: %ld.\n", WSAGetLastError()); 
  goto exit;
} 

printf("bind() is OK!\n"); 

memset(udpBuf, 0, sizeof(udpBuf)); 

printf("receiving data\n"); 
while (...) 
{
  printf("."); 

  recvResult = recvfrom(sockID, udpBuf, sizeof(udpBuf), 0, (struct sockaddr *)&fepAddr, &addr_len);  
  if (recvResult == SOCKET_ERROR)
  {
    if (WSAGetLastError() != WSAEWOULDBLOCK)
    {
      printf("\nrecvfrom() failed: %ld.\n", WSAGetLastError()); 
      goto exit;
    }

    fd_set fd;
    FD_ZERO(&fd);
    FD_SET(sockID, &fd);

    timeval t;
    t.tv_sec = ...; // seconds
    t.tv_usec = ...; // microseconds

    selectResult = select(0, &fd, NULL, NULL, &t);
    if (selectResult == SOCKET_ERROR)
    {
      printf("\nselect() failed: %ld.\n", WSAGetLastError()); 
      goto exit;
    }

    if (selectResult == 0)
    {
      printf("\nsocket timed out.\n");
      goto exit;
    }

    continue;
  }

  if (recvResult > 0)
    fwrite(udpBuf, recvResult, 1, udp); 
} 

exit: 
  if (udp != 0)
  { 
    fclose(udp); 
    udp = 0;  
  } 

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