0

我在 sendto() 调用中收到 10038 套接字错误。这可能是什么原因造成的?在套接字创建或绑定时我没有收到任何错误。这是我设置套接字的方法:

Client client;
client.client_socket = client.open_port(PEER_PORT2); //5001
client.prompt("Enter the router hostname: ",router);
client.sa_out = client.prepare_peer_connection(router, ROUTER_PORT2); //7001

开放端口

SOCKET Client::open_port(int port)
{
    SOCKET sock; 
    SOCKADDR_IN sa;
    HOSTENT *hp; 
    char hostname[11]; 

    gethostname(hostname,11);

    if((hp=gethostbyname(hostname)) == NULL) 
        throw "Could not determine a host address from supplied name";

    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);
    sa.sin_addr.s_addr = htonl(INADDR_ANY);

    if((sock = socket(AF_INET,SOCK_DGRAM,0))==INVALID_SOCKET) 
    throw "Generating a new local socket failed";

    if (bind(sock,(LPSOCKADDR)&sa,sizeof(sa)) == SOCKET_ERROR) 
    throw "Could not bind socket to supplied port";

    return sock;
}

prepare_peer_connection

SOCKADDR_IN Client::prepare_peer_connection(char* hostname, int port)
{
    SOCKADDR_IN sa;
    HOSTENT *hp;

    if((hp = gethostbyname(hostname)) == NULL) 
         throw "Could not determine a host address from supplied name";

    memcpy(&sa_out.sin_addr, hp->h_addr, hp->h_length);
    sa.sin_family = hp->h_addrtype;
    sa.sin_port = htons(port);

    return sa;
}

带有 sendTo() 调用的函数导致 10038

int Client::send_packet(SOCKET sock, SOCKADDR_IN sa, char* buffer, int size, int pid)
{
    int ibytessent = 0;
    int packet_size = size + sizeof(int);
    int from = sizeof(sa); 
    char packet[132]; //128 + 4

    make_packet(packet, buffer, size, pid);

    if ((ibytessent = sendto(sock,packet,packet_size,0,(SOCKADDR*)&sa, from)) == SOCKET_ERROR)
    {
        throw "Send failed";
    }
    else
    {
        memset(buffer,0,size);
        return ibytessent - sizeof(int); 
    }
}
4

0 回答 0