我在C中制作一个服务器,但是当我想听取连接时,听取回-1,我不知道为什么。创建套接字没有任何问题,端口已分配,但即使listen给我一个错误。有人有线索吗?我发布下面的代码。 先感谢您。
if((UDP_socket = socket(AF_INET, SOCK_DGRAM,0)) > 0){
fprintf(stdout,"socket created\n");
}
bzero((char*)&addrUDP, sizeof(struct sockaddr_in));
addrUDP.sin_family = AF_INET;
addrUDP.sin_port = htons(0);
if(bind(UDP_socket,(struct sockaddr*)&addrUDP, sizeof(struct sockaddr_in))<0){
fprintf(stdout,"asign port UDP Error\n");
close(UDP_socket); exit(1);
}
fprintf(stdout,"Assign port UDP OK \n");
bzero((char*)&addrUDP, sizeof(struct sockaddr_in));
TCP_socket = socket(AF_INET, SOCK_DGRAM, 0);
if(TCP_socket < 0){
fprintf(stdout,"Socket TCP Error\n");
exit(1);
}
fprintf(stdout,"Socket TCP OK\n");
bzero((char *)&addrTCP, sizeof(struct sockaddr_in));
addrTCP.sin_family = AF_INET;
addrTCP.sin_port = htons(0);
if(bind(TCP_socket,(struct sockaddr*)&addrTCP, sizeof(struct sockaddr_in))<0){
fprintf(stdout,"Assign server port TCP ERROR\n");
exit(1);
}
fprintf(stdout,"Assign server port TCP OK\n");
int listening;
listening = listen(TCP_socket, 1024);
if(listening < 0){
fprintf(stdout,"Listening ERROR\n");
exit(1);
}else{
fprintf(stdout,"Listening OK\n");
}
答案 0 :(得分:3)
TCP_socket = socket(AF_INET,SOCK_DGRAM,0);
你想要SOCK_STREAM。