0
我已經編寫了一個程序來發送數據並通過UDP並行偵聽數據。爲此,我創建了幾個功能:在UDP套接字上偵聽時出現錯誤10022
int initiate_TXRX_variables(void)
{
struct hostent * hp;
hp = gethostbyname("localhost");
if(hp == NULL)
return INVALID_SOCKET;
memset(&sa_udp_in, 0, sizeof(sa_udp_in));
memset(&sa_tcp_in, 0, sizeof(sa_tcp_in));
memset(&sa_udp_out, 0, sizeof(sa_udp_out));
memset(&sa_tcp_in, 0, sizeof(sa_tcp_in));
memcpy((char *)&sa_udp_in.sin_addr, hp->h_addr, hp->h_length);
memcpy((char *)&sa_tcp_in.sin_addr, hp->h_addr, hp->h_length);
memcpy((char *)&sa_udp_out.sin_addr, hp->h_addr, hp->h_length);
memcpy((char *)&sa_tcp_out.sin_addr, hp->h_addr, hp->h_length);
sa_udp_in.sin_family = hp->h_addrtype;
sa_tcp_in.sin_family = hp->h_addrtype;
sa_udp_out.sin_family = hp->h_addrtype;
sa_tcp_out.sin_family = hp->h_addrtype;
sa_udp_in.sin_port = htons((u_short)PORTNUM_UDP_IN);
sa_tcp_in.sin_port = htons((u_short)PORTNUM_TCP_IN);
sa_udp_out.sin_port = htons((u_short)PORTNUM_UDP_OUT);
sa_tcp_out.sin_port = htons((u_short)PORTNUM_TCP_OUT);
return 1;
}
int ultra_spam_network_udp(void)
{
struct hostent *hp, *local;
hp = gethostbyname("192.168.70.0");
hp = gethostbyname("255.255.255.255");
local = gethostbyname("localhost");
memcpy((char *)&sa_udp_out.sin_addr,hp->h_addr, hp->h_length);
//set_ip_udp_out(htonl(ntohl(sa_udp_out.sin_addr.s_addr) - 1));
InetPton(AF_INET, "192.168.30.0", &(sa_udp_out.sin_addr));
char str[] = "Hello";
char ipstr[256];
for(int i = 0; i < 256; i++)
{
std::cout << "Local: " << retLocalIP() << " and current IP: " << /*inet_ntoa(sa_udp_out.sin_addr)*/InetNtop(AF_INET, &(sa_udp_out.sin_addr), ipstr, 256) << " with counter: " << i << '\n';
sendto(UDP_OUT, str, sizeof(str), 0, (struct sockaddr*)&sa_udp_out, sizeof(sa_udp_out));
sa_udp_out.sin_addr.s_addr = htonl(ntohl(sa_udp_out.sin_addr.s_addr)+1);
};
//sendto(UDP_OUT, str, sizeof(str), 0, (struct sockaddr*)&sa_udp_out, sizeof(sa_udp_out));
return 0;
}
int/*char **/ listen_udp_debug(void)
{
struct sockaddr_in from;
int fromlen = sizeof(from);
char inStr[100];
char * ptr = &inStr[0];
int ret = recvfrom(UDP_IN, inStr, sizeof(inStr), 0, (struct sockaddr *)&from, &fromlen);
std::cout << "From listen_udp_debug: " << ret << " with error number " << WSAGetLastError() << '\n';
//return ptr;
return 0;
}
我把這些功能通過
initiate_TXRX_variables();
boost::thread thread_1 = boost::thread(ultra_spam_network_udp);
listen_udp_debug();
爲IN和OUT PORTNUMS是不同的,當然。我的問題是,現在我調用listen_udp_debug()
時出現錯誤10022。爲什麼我得到這個錯誤,我能做些什麼來規避這個錯誤?
非常感謝!
'char * ptr =&inStr [0];'在這裏沒有意義。你已經有了一個指向數組中第一個元素的指針,通過'inStr' –
什麼是'UDP_IN'?它是如何初始化的? –
'SOCKET UDP_IN,UDP_OUT;','UDP_IN = socket(hp-> h_addrtype,SOCK_DGRAM,0);','struct hostent * hp = gethostbyname(「localhost」);'。我忘了什麼嗎? –