- 论坛徽章:
- 0
|
帮忙看一下为什么用select不阻塞?
我每次都打印了:值都为6,(就是listenfd的值,我在同一台机子上重启该进程,listenfd的值都不变)下面是我实现的一段代码:
int CreateSocket(int isblock)
{
int cnt;
int flag;
int sockfd;
int opt=1;
int len=sizeof(opt);
//struct linger lin={1,0};
if ((sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
{
printf("socket call error!\n" ;
close(sockfd);
exit (-1);
}
printf("------------>;>;开始建立SOCK = (%d)\n", sockfd);
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,len);
//设置为阻塞或非阻塞方式
flag=fcntl(sockfd,F_GETFL);
if (isblock!=1)
fcntl(sockfd,F_SETFL,flag|O_NDELAY);
else
fcntl(sockfd,F_SETFL,~O_NDELAY&flag);
return sockfd;
}
//实现RST分组来关闭连接
int ServLisen(int tcp_port)
{
int sockln;
int iRnt = -1;
struct sockaddr_in svr_tcp;
sockln=CreateSocket(1);
bzero((char *)&svr_tcp,sizeof(svr_tcp));
svr_tcp.sin_family=AF_INET;
svr_tcp.sin_port=htons(tcp_port);
//svr_tcp.sin_addr.s_addr=htonl(INADDR_ANY);
svr_tcp.sin_addr.s_addr=inet_addr("202.100.99.60" ;
if ((iRnt = bind(sockln,(struct sockaddr*)&svr_tcp,sizeof(struct sockaddr)))<0)
{
printf("\nTCP port is busy!\n" ;
return(-1);
}
else
printf("------------->;>;绑定 iRnt = (%d)成功\n", iRnt);
if ((iRnt = listen(sockln,LISTENNUM))<0)
{
printf("\nListen error!\n" ;
return(-1);
}
else
printf("------------->;>;监听 iRnt = (%d)成功\n", iRnt);
return sockln;
} |
|