免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1532 | 回复: 0

[Windows 7] windows select如何使用? [复制链接]

论坛徽章:
0
发表于 2013-04-28 19:11 |显示全部楼层
懂的话,这些代码不难理解,主要功能是实现服务器和客户端的异步通讯,但是运行的时候却做不到(通讯还是同步的),求解?
代码:
server.cpp
#include<windows.h>
#include<stdio.h>
#include<winsock.h>

#define SERVER_PORT 9999
#define SERVER_IP  "127.0.0.1"
int main(int argc,char *argv[])
{
SOCKET serSocket,cliSocket;
struct sockaddr_in localAddr,clientAddr;
WSADATA Ws;
int Bit;
int Lit;
char recvBuf[1024];
int recvR,sen;
int cliLen;
fd_set rfds;//////////
struct timeval time;//////////////
int maxrd=0;//////////
int ret;//////////

if(WSAStartup(MAKEWORD(2,2),&Ws)!=0)
{
printf("init windows socket failed:%d\n",GetLastError());
return -1;
}
serSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(serSocket==INVALID_SOCKET)
{
printf("create socket failed:%d\n",GetLastError());
return -1;
}
localAddr.sin_family=AF_INET;
localAddr.sin_port=htons(SERVER_PORT);
localAddr.sin_addr.s_addr=inet_addr(SERVER_IP);
memset(localAddr.sin_zero,0x00,;

Bit=bind(serSocket,(struct sockaddr*)&localAddr,sizeof(localAddr));
if(Bit!=0)
{
printf("bind failed:%d\n",GetLastError());
return -1;
}
Lit=listen(serSocket,5);
if(Lit!=0)
{
printf("listen failed:%d\n",GetLastError());
return -1;
}
printf("server has been startup\n";
while(true)
{
cliLen=sizeof(clientAddr);
cliSocket=accept(serSocket,(struct sockaddr*)&clientAddr,&cliLen);
if(cliSocket==INVALID_SOCKET)
{
printf("accept failed:%d\n",GetLastError());
return -1;
}
printf("client connect%d:%d\n",inet_ntoa(clientAddr.sin_addr),clientAddr.sin_port);/////////////////
while(true)
{
FD_ZERO(&rfds);///////////
FD_SET(0,&rfds);////////////////
FD_SET(cliSocket,&rfds);////////////////////////
time.tv_sec=1;////////
time.tv_usec=0;/////////////////////
maxrd=(maxrd>cliSocket?maxrd:cliSocket);/////////
ret=select(maxrd+1,&rfds,NULL,NULL,&time);////////////////////
if(ret==0)//////
continue;
else{
if(FD_ISSET(0,&rfds))
{ /////////////////////
memset(recvBuf,0x00,sizeof(recvBuf));
recvR=recv(cliSocket,recvBuf,1024,0);
if(recvR==0||recvR==SOCKET_ERROR)
{
printf("the client had quit\n";
break;
}
printf("recieve message:%s\n",recvBuf);
}

if(FD_ISSET(cliSocket,&rfds))
{
memset(recvBuf,0x00,sizeof(recvBuf));
gets(recvBuf);
sen=send(cliSocket,recvBuf,1024,0);
}
}
}
return 0;
}
closesocket(serSocket);
closesocket(cliSocket);
WSACleanup();
return 0;
}

client.cpp
#include<windows.h>
#include<stdio.h>
#include<winsock.h>

#define SERVERPORT 9999
#define SERVERIP "127.0.0.1"
int
main(int argc,char* argv[])
{
struct sockaddr_in serAddr;
SOCKET cliSocket;
int conn;
WSADATA Ws;
char sendBuf[1024];
int sen;
int recvR;
fd_set rfds;/////////////////
struct timeval time;///////////
int maxrd=0;////////////////
int ret;/////////////

if(WSAStartup(MAKEWORD(2,2),&Ws)!=0)
{
printf("init windows socket failed:%d\n",GetLastError());
return -1;
}

cliSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(cliSocket==INVALID_SOCKET)
{
printf("window socket failed:%d\n",GetLastError());
return -1;
}

serAddr.sin_family=AF_INET;
serAddr.sin_addr.s_addr=inet_addr(SERVERIP);
serAddr.sin_port=htons(SERVERPORT);
memset(serAddr.sin_zero,0x00,;

conn=connect(cliSocket,(struct sockaddr*)&serAddr,sizeof(serAddr));
if(conn==SOCKET_ERROR)
{
printf("connect socket failed:%d\n",GetLastError());
return -1;
}
else
{
printf("connect successfully!\n";
}
while(true)
{
FD_ZERO(&rfds);/////////
FD_SET(0,&rfds);/////////////////
FD_SET(cliSocket,&rfds);/////////
time.tv_sec=1;/////////////
time.tv_usec=0;//////////////////
maxrd=((maxrd>cliSocket)?maxrd:cliSocket);
ret=select(maxrd+1,&rfds,NULL,NULL,&time);
if(ret==0)
continue;
else{
if(FD_ISSET(0,&rfds))
{
memset(sendBuf,0x00,sizeof(sendBuf));
gets(sendBuf);
sen=send(cliSocket,sendBuf,(int)strlen(sendBuf),0);
if(sen==SOCKET_ERROR)
{
printf("send info error:%d\n",GetLastError());
break;
}
}
if(FD_ISSET(cliSocket,&rfds))
{
memset(sendBuf,0x00,sizeof(sendBuf));
recvR=recv(cliSocket,sendBuf,1024,0);
printf("recieve from server:%s\n",sendBuf);
}
}

}
closesocket(cliSocket);
WSACleanup();
return 0;

}

嗯,是这样的,这个程序类似于QQ聊天,无乱是服务器还是客户端都无需等待(或者等待时间很短)对方发送数据自己才可以发送,我在linux上实现十可以的,不知道在windows上可不可以?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

DTCC2020中国数据库技术大会 限时9.5折

【架构革新 高效可控】2020年8月17日~19日第十一届中国数据库技术大会将在北京隆重召开。

大会设置2大主会场,20+技术专场,将邀请超百位行业专家,重点围绕数据架构、AI与大数据、传统企业数据库实践和国产开源数据库等内容展开分享和探讨,为广大数据领域从业人士提供一场年度盛会和交流平台。

http://dtcc.it168.com


大会官网>>
  

北京盛拓优讯信息技术有限公司. 版权所有 16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122
中国互联网协会会员  联系我们:huangweiwei@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP