tags:

views:

50

answers:

1

Hi,

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);
A: 

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;
  }
Remy Lebeau - TeamB
thanks for your reply......
ulaga
I need one more help.....I have created raw socket using socket(PF_PACKET, SOCK_RAW, ETH_P_IP)....but it is giving invalid argument...how to create raw socket to receive udp packets from port??
ulaga
SOCK_RAW is not availalbe in the PF_PACKET family. It is only available in the AF_INET and AF_INET6 families.
Remy Lebeau - TeamB
bind succeeds even the port is not available. It returns zero. what's the reason??? Is there any flag to set for bind....Please help me...
ulaga
On Windows, if you bind the socket to a wildcard address (ie: ADDR_ANY), then the physical binding to the NIC may be delayed internally until listen() is called, long after bind() has exited. bind() cannot report an error in that situation. The error will come from listen() instead. This is described by MSDN in the listen() documentation. Also, on a side note, bind() is not allowed on a SOCK_RAW socket starting with XP SP2. This is documented by MSDN as well.
Remy Lebeau - TeamB