2014-01-24 83 views
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。为什么我得到这个错误,我能做些什么来规避这个错误?
非常感谢!

+0

'char * ptr =&inStr [0];'在这里没有意义。你已经有了一个指向数组中第一个元素的指针,通过'inStr' –

+0

什么是'UDP_IN'?它是如何初始化的? –

+1

'SOCKET UDP_IN,UDP_OUT;','UDP_IN = socket(hp-> h_addrtype,SOCK_DGRAM,0);','struct hostent * hp = gethostbyname(“localhost”);'。我忘了什么吗? –

回答

0

发现我的问题,我不得不在我的udp_receive函数中使用bind(),否则它将无法工作。