测试一个udp服务器时,写了如下程序。
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
int srv_sock;
socklen_t clt_len;
struct sockaddr_in srv_addr;
struct sockaddr_in clt_addr;
struct timeval wait_time;
int port;
int ret;
fd_set read_fds;
int num;
char recv_buf[1024];
if(argc!=2){
printf("Usage: %s port_name\n",argv[0]);
return 1;
}
port=atoi(argv[1]);
srv_sock=socket(AF_INET,SOCK_DGRAM,0);
if(srv_sock<0){
perror("cannot create socket");
return 1;
}
memset(&srv_addr,0,sizeof(srv_addr));
srv_addr.sin_family=AF_INET;
srv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
srv_addr.sin_port=htons(port);
ret=bind(srv_sock,(struct sockaddr*)&srv_addr,sizeof(srv_addr));
if(ret<0){
perror("cannot bind the socket");
return 1;
}
// clt_len = sizeof(struct sockaddr_in);
while(1){
wait_time.tv_sec=0;
wait_time.tv_usec=0;
FD_ZERO(&read_fds);
FD_SET(srv_sock,&read_fds);
num=select(srv_sock+1,&read_fds,NULL,NULL,&wait_time);
if(num<0){
perror("select fail");
continue;
}
if(FD_ISSET(srv_sock,&read_fds)){
int n=recvfrom(srv_sock,recv_buf,sizeof(recv_buf),0,(struct sockaddr*)&clt_addr,&clt_len);
printf("clt_len is %d\n",clt_len);
if(n<0){
perror("cannot receive client message");
close(srv_sock);
return 1;
}
recv_buf[n] = 0;
printf("server receive: %s\n",recv_buf);
memset(recv_buf,0,sizeof(recv_buf));
n = sendto(srv_sock,"hello\n",6,0,(struct sockaddr*)&clt_addr,clt_len);
if(n < 0)
{
perror("can't sent client message");
}
}
}
}
测试代码时,发现服务器第一次收到数据发送总是提示
can't sent client message: Address family not supported by protocol
后面的就会一直成功。
不明白什么原因。
比较了几个udp例子之后,发现把程序里面红色部分不注释掉,就可以成功了。
始终找不到原因,到后续再看看源代码看怎么解决?
希望知道的兄弟能说下。
阅读(7309) | 评论(2) | 转发(0) |