- 论坛徽章:
- 0
|
懂的话,这些代码不难理解,主要功能是实现服务器和客户端的异步通讯,但是运行的时候却做不到(通讯还是同步的),求解?
代码:
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上可不可以? |
|