UDP Tx 循环每次恰好在 3970 个数据包处停止工作,在我朋友的计算机上为 14386 个数据包
在过去的四天里我一直被这个问题困扰,但我似乎无法弄清楚。我正在尝试使用 UDP 数据包向自己发送数据,以便另一个程序可以读取字节。
我可以清楚地读取数据包,但在程序的 UDP 部分挂起之前我只能读取到 3970。过剩和其他一切都继续运行良好。我给了我的朋友同样的代码,他在他的计算机上运行了它。在挂起之前他进行了 14386 次迭代。变量 temp 将计算发送的数据包数量。 -1 不好。计数器对 while 循环迭代进行计数。我正在遵循此处的示例:
http:// msdn.microsoft.com/en-us/library/ms740148(v=vs.85).aspx
示例代码:
#include "stdafx.h"
#include <WinSock2.h> // don't forget to 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 as 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;
}
I've been stuck at this issue for the last 4 days and I can't seem to figure it out. I'm trying to send data to myself using UDP packets so another program can read the bytes.
I can clearly read the packets but I only get up to 3970 before the UDP portion of the program hangs. Glut and everything else continues to run fine. I gave my friend the same code and he ran it on his computer. He got 14386 iterations before it hangs. variable temp will count the number of packets sent. -1 is bad. counter counts the while loop iterations. I'm following the 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 to 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 as 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在循环中分配新的套接字(
sendto
的第一个参数),您还为bind
分配另一个套接字,但这些套接字永远不会被释放。您最终会用完要分配的套接字句柄,因此您的程序会挂起。相反,在
SUDP_init
中分配一次套接字,存储它而不是丢弃它,然后将其传递给bind
和sendto
You are allocating new sockets in a loop (the first argument to
sendto
), you are also allocating another forbind
, but these are never freed. You eventually run out of socket handles to allocate, hence your program hanging.Instead, allocate a socket once in
SUDP_init
, store it instead of discarding it, then pass that tobind
andsendto