c++ 套接字的另一个“结果太大”问题并没有太大帮助。我有一个服务器代码,但每次我运行它时,我都会得到一个“结果太大”的错误 - 监听()函数。希望,你能帮上忙!
SOCKET sd;
SOCKET sd2;
char serve_1_clien_0_intern;
struct sockaddr_in server;
struct sockaddr_in client;
void udp_init(unsigned short port_number, int a1, int a2, int a3, int a4, char serve_1_clien_0 ){
WSADATA w;
serve_1_clien_0_intern = serve_1_clien_0;
if (WSAStartup(0x0101, &w) != 0){
fprintf(stderr, "Could not open Windows connection.\n");
return;
exit(0);
}
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == INVALID_SOCKET){
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
return;
exit(0);
}
memset((void *)&server, '\0', sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(port_number);
{
server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;
server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;
server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;
server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;
}
if(serve_1_clien_0_intern==1){
if (bind(sd,(struct sockaddr *)&server,sizeof(struct sockaddr_in)) == SOCKET_ERROR){
fprintf(stderr, "Could not bind name to socket, Maybe wrong IP-ADRESS??\n");
closesocket(sd);
WSACleanup();
return;
exit(0);
}
if(listen(sd,10) == SOCKET_ERROR){
perror("listen");
exit(0);
}
int sin_size;
sin_size = sizeof(struct sockaddr_in);
if ((sd2 = accept(sd, (struct sockaddr *)&client,&sin_size)) == SOCKET_ERROR){
perror("accept");
exit(0);
}
}
}