开发者

udp winsock programming

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 h开发者_高级运维ave 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);


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜