C - Read bytes from the UDP socket buffer (Linux) -


i wrote code in order handle receiving udp packets. packets same length(120 bytes), , 1,000 packets coming in every second. simply, code this.

int sock = -1; int flag = 0; int nread = 0;  #define local_buff_size (8192) char buff[local_buff_size];  struct sockaddr_in sockaddr;  memset((void *)&sockaddr, 0x00, sizeof(struct sockaddr_in));  if((sock = socket(pf_inet, sock_dgram, 0)) < 0) {     /* print error , terminate */ }  /* make non-blocking */   flag = fcntl( sock, f_getfl, 0 ); fcntl( sock, f_setfl, flag | o_nonblock );  sockaddr.sin_family = af_inet; sockaddr.sin_port = htons(portnum); sockaddr.sin_addr.s_addr = inaddr_any;  if(bind(sock, (struct sockaddr *)&sockaddr, sizeof (sockaddr)) < 0) {     /* print error , terminate */ }  while(...) {     nread = recv(sock, buff, local_buff_size, 0);     if(nbytes > 0)     {         /* process data */     }     else     {         /* if it's error, handle error */     } } 

when wrote code, expect recv() function returns every bytes in udp socket buffer @ moment, but, seems returns 1 packet(120 byte) every time though there more bytes in buffer. encountered packet loss. know there many other ways solve problem, but, reading existent bytes in udp buffer @ once easiest way me. so, there way read bytes in udp buffer @ once?

thanks in advance

udp message oriented protocol, therefore, getting single message in 1 recv operation. can possible use recvmmsg() system call receive multiple messages in single call.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -