非阻塞select()函数出错
我写了一个聊天工具但是运行后客户端输入信息后服务端没收到但是服务端输入信息后就马上收到客户端刚发的信息,具体代码如下:服务端:
#include "stdafx.h"
#include <winsock2.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
void server();
int _tmain(int argc, _TCHAR* argv[],TCHAR* envp[])
{
server();
system("pause");
return 0;
}
void server()
{
cout<<"----------服务器端----------"<<endl;
WSADATA wsadata;
if(WSAStartup(0x0101,&wsadata))
{
cout<<"初始化套接字失败!"<<endl;
return ;
}
SOCKET hSock=socket(AF_INET,SOCK_STREAM,0);
if(hSock==INVALID_SOCKET)
{
cout<<"建立流套接字句柄失败!"<<endl;
return;
}
sockaddr_in addin;
ZeroMemory(&addin,sizeof(addin));
addin.sin_addr.s_addr =inet_addr("192.168.168.165");
addin.sin_family=AF_INET;
addin.sin_port=htons(33333);
if(bind(hSock,(SOCKADDR*)&addin,sizeof(addin))==SOCKET_ERROR)
{
cout<<"绑定地址到套接字失败!"<<endl;
return;
}
if(listen(hSock,5)==SOCKET_ERROR)
{
cout<<"进入监听状态失败!"<<endl;
return;
}
cout<<"等待客户端连接:"<<endl;
sockaddr_in clientaddin;
int addinlength=sizeof(clientaddin);
SOCKET connected;
if(( connected=accept(hSock,(sockaddr *)(&clientaddin),&addinlength))==INVALID_SOCKET)
{
cout<<"接受连接失败!"<<endl;
closesocket(connected);
}
else
{
char *ipclient;
ipclient=inet_ntoa(clientaddin.sin_addr);
cout<<ipclient<<"接入"<<endl;
}
char sen[100],rec[100];
int errorcode;
fd_set read,write;
timeval timeout;
memset(&timeout,0,sizeof(timeval));
timeout.tv_sec=2;
for(;;)
{
FD_ZERO(&read);FD_ZERO(&write);
FD_SET(connected,&read);FD_SET(connected,&write);
DWORD value=1;
if(ioctlsocket(connected,FIONBIO,&value)==SOCKET_ERROR)
{
cout<<"设置非阻塞模式失败!"<<endl;
return;
}
select(0,NULL,&write,NULL,&timeout);
if(FD_ISSET(connected,&write))
{
cin>>sen;
if(send(connected,sen,sizeof(sen),0)==-1)
{cout<<"send error!"<<endl;
errorcode=WSAGetLastError();
cout<<"失败!错误代码:"<<errorcode<<endl;
}
}
select(0,&read,NULL,NULL,&timeout);
if(FD_ISSET(connected,&read))
{
if(recv(connected,rec,sizeof(rec),0)==0)
{
printf("读取客户端时客户端已经关闭!\n");
errorcode=WSAGetLastError();
cout<<"失败!错误代码:"<<errorcode<<endl;
}
cout<<rec<<endl;
}
}
}
客户端代码:
#include "stdafx.h"
#include <winsock2.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
void client();
int _tmain(int argc, _TCHAR* argv[])
{
client();
system("pause");
return 0;
}
void client()
{
cout<<"----------客户端----------"<<endl;
WSADATA wsadata;
if(WSAStartup(0x0101,&wsadata))
{
cout<<"初始化套接字失败!"<<endl;
return;
}
SOCKET cSock=socket(AF_INET,SOCK_STREAM,0);
if(cSock==INVALID_SOCKET)
{
cout<<"建立流套接字句柄失败!"<<endl;
return;
}
cout<<"请输入服务器IP:"<<endl;
sockaddr_in addin;
ZeroMemory(&addin,sizeof(addin));
addin.sin_family=AF_INET;
addin.sin_port=htons(33333);
char tempgetip[20];
cin>>tempgetip;
addin.sin_addr.S_un.S_addr=inet_addr(tempgetip);
if(connect(cSock,(sockaddr *)(&addin),sizeof(addin))==SOCKET_ERROR)
{
cout<<"连接服务器地址失败!"<<endl;
return;
}
char sen[100],rec[100];
int errorcode;
fd_set read,write;
timeval timeout;
memset(&timeout,0,sizeof(timeval));
timeout.tv_sec=2;
for(;;)
{
FD_ZERO(&read);FD_ZERO(&write);
FD_SET(cSock,&read);FD_SET(cSock,&write);
DWORD value=1;
if(ioctlsocket(cSock,FIONBIO,&value)==SOCKET_ERROR)
{
printf("设置非阻塞模式失败!\n");
return;
}
select(0,NULL,&write,NULL,&timeout);
if(FD_ISSET(cSock,&write))
{
cin>>sen;
if(send(cSock,sen,sizeof(sen),0)==-1)
{cout<<"send error!"<<endl;
errorcode=WSAGetLastError();
cout<<"失败!错误代码:"<<errorcode<<endl;
}
}
select(0,&read,NULL,NULL,&timeout);
if(FD_ISSET(cSock,&read))
{
if(recv(cSock,rec,sizeof(rec),0)==0)
{
printf("读取客户端时客户端已经关闭!\n");
errorcode=WSAGetLastError();
cout<<"失败!错误代码:"<<errorcode<<endl;
}
cout<<rec<<endl;
}
}
}哪里用错了请帮看下谢谢