c++ - UDP Tx loop stops working every time at exactly 3970 packets, 14386 on my friends computer -
i've been stuck @ issue last 4 days , can't seem figure out. i'm trying send data myself using udp packets program can read bytes.
i can read packets 3970 before udp portion of program hangs. glut , else continues run fine. gave friend same code , ran on computer. got 14386 iterations before hangs. variable temp count number of packets sent. -1 bad. counter counts while loop iterations. i'm following example here:
http://msdn.microsoft.com/en-us/library/ms740148(v=vs.85).aspx
example code:
#include "stdafx.h" #include <winsock2.h> // don't forget add in //project properties>linker>input>additional dependences [ws2_32.lib] sockaddr_in dest; sockaddr_in local; wsadata data; static void sudp_init(void) { printf("--[udp socket initialized]--\r\n"); wsastartup( makeword( 2, 2 ), &data ); local.sin_family = af_inet; local.sin_addr.s_addr = inet_addr( "127.0.0.1" ); //same localhost local.sin_port = 6000; dest.sin_family = af_inet; dest.sin_addr.s_addr = inet_addr( "127.0.0.1" ); dest.sin_port = htons( 6000 ); bind( socket( af_inet, sock_dgram, 0 ), (sockaddr *)&local, sizeof(local) ); printf("socket bound...\r\n"); } static int counter = 0; int _tmain(int argc, _tchar* argv[]) { sudp_init(); while(1){ char packet[30]; sprintf(packet, "%0.3f,%0.3f,%0.3f", 55.4, 16.1, -27.88); int temp = sendto( socket( af_inet, sock_dgram, 0 ), packet, strlen(packet), 0, (sockaddr *)&dest, sizeof(dest) ); if(temp>=1){ counter++; } printf("bytes sent: %d, counter: %d\r\n",temp,counter); } return 0; }
you allocating new sockets in loop (the first argument sendto
), allocating bind
, these never freed. run out of socket handles allocate, hence program hanging.
instead, allocate socket once in sudp_init
, store instead of discarding it, pass bind
, sendto
Comments
Post a Comment