//=================[server]====================
#include "stdafx.h"
#include "stdio.h"
#include "winsock2.h"
#include <vector>
using std::vector;
SOCKET s;
vector<SOCKET> cc;
void onAccept()
{
sockaddr_in sa;
int add_len = sizeof(sa);
SOCKET c = accept(s,(sockaddr*)&sa,&add_len);
if(c!=INVALID_SOCKET)
{
printf("client:%s port:%d connected!\n",inet_ntoa(sa.sin_addr),sa.sin_port);
send(c,"hello",5,0);
cc.push_back(c);
}
else
{
printf("invalid connect\n");
}
}
void onRecv(const fd_set &fs)
{
for(int i=0;i<cc.size();++i)
{
if(FD_ISSET(cc[i],&fs))
{
char buffer[256]={0};
int rc= recv(cc[i],buffer,255,0);
if(rc>0)
{
printf("recv msg:%s\n",buffer);
send(cc[i],buffer,strlen(buffer)+1,0);
}
else if(rc == SOCKET_ERROR)
{
printf("recv msg failed:%d\n",::WSAGetLastError());
closesocket(cc[i]);
cc.erase(cc.begin()+i);
break;
}
}
}
}
int main(int argc, char* argv[])
{
//init
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
WSAStartup( wVersionRequested, &wsaData );
s = socket(AF_INET,SOCK_STREAM,0);
if(s!=INVALID_SOCKET)
{
printf("create socket success!\n");
}
{
sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons( 1500 );
sa.sin_addr.s_addr = 0 ; //*²»Ö¸¶¨µØÖ·
int rc = bind(s,(sockaddr *)&sa,sizeof(sa));
if(rc == SOCKET_ERROR)
{
printf("bind failed:%d\n",::WSAGetLastError());
return -1;
}
}
listen(s,SOMAXCONN);
timeval timeout={0,100};
while(true)
{
fd_set fs;
FD_ZERO(&fs);
FD_SET(s,&fs);
int rc = select(0,&fs,0,0,&timeout);
if(rc>0)
{
onAccept();
}
else if(rc == 0)
{
//printf("no connect!\n");
}
else
{
printf("select1 error:%d\n",::WSAGetLastError());
}
if(cc.size()>0)
{
FD_ZERO(&fs);
for(int i=0;i<cc.size();++i)
{
FD_SET(cc[i],&fs);
}
rc = select(0,&fs,0,0,&timeout);
if(rc>0)
{
onRecv(fs);
}
else if(rc == 0)
{
//printf("no recv!\n");
}
else
{
printf("select2 error:%d\n",::WSAGetLastError());
}
}
}
closesocket(s);
//clean
WSACleanup( );
return 1;
}
//=====================[client]====================
#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#include <process.h>
SOCKET c;
unsigned int __stdcall myrecv(void *)
{
while(true)
{
char recv_buffer[256]={0};
int rc =recv(c,recv_buffer,255,0)>0;
if(rc>0)
{
printf("recv:%s\n",recv_buffer);
}
else if(rc==SOCKET_ERROR)
{
printf("recv failed:%d\n",::WSAGetLastError());
break;
}
}
_endthreadex(0);
return 0;
}
int main(int argc, char* argv[])
{
//init
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
WSAStartup( wVersionRequested, &wsaData );
c = socket(AF_INET,SOCK_STREAM,0);
if(c!=INVALID_SOCKET)
{
printf("create socket success!\n");
}
//connect
{
sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons( 1500 );
sa.sin_addr.s_addr = inet_addr("127.0.0.1") ; //*²»Ö¸¶¨µØÖ·
int rc = connect(c,(sockaddr *)&sa,sizeof(sa));
if(rc == SOCKET_ERROR)
{
printf("connect failed:%d\n",::WSAGetLastError());
return -1;
}
}
_beginthreadex(0,0,&myrecv,0,0,0);
int count = 0;
timeval timeout={0,100};
while(true)
{
char send_buffer[256]={0};
sprintf(send_buffer,"s%d",count++);
int rc = send(c,send_buffer,strlen(send_buffer),0);
if(rc>0)
{
printf("send:%s\n",send_buffer);
}
else if(rc==SOCKET_ERROR)
{
printf("send failed:%d\n",::WSAGetLastError());
break;
}
}
closesocket(c);
//clean
WSACleanup( );
return 0;
}