我在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
open_port
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);
}
}