#pragma comment(lib,"ws2_32.lib")
#include
#include
#include
#include
#include
void _cdecl get_keyboard_input(void * param)
{
SOCKET sock =(SOCKET)param;
while(1)
{
char buf[1024];
gets(buf);
int sByte = send(sock,buf,strlen(buf),0);
if(sByte == SOCKET_ERROR)
{
printf("send() failed:%d\n",WSAGetLastError());
}
}
}
int main()
{
WSADATA wsadata;
int err = WSAStartup(WINSOCK_VERSION,&wsadata);
if(err != 0)
{
printf("WSAStartup() failed :%d\n",WSAGetLastError());
return -1;
}
SOCKET sock = socket(AF_INET,SOCK_STREAM,0);
if(sock == INVALID_SOCKET)
{
printf("socket() failed:%d\n",WSAGetLastError());
WSACleanup();
return -1;
}
sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(27015);
serveraddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
err=connect(sock,(sockaddr *)&serveraddr,sizeof(serveraddr));
if(err == INVALID_SOCKET)
{
printf("connect() failed:%d\n",WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
_beginthread(get_keyboard_input,0,(void*)sock);
Sleep(1000);
char rbuf[1024];
while(1)
{
memset(rbuf,0,1024);
int rByte =recv(sock,rbuf,1024,0);
if(rByte == SOCKET_ERROR)
{
printf("recv() failed:%d\n",WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
if(rByte == 0)
{
printf("Connection is closed.\n");
break;
}
printf("TCP recv DATA:%s\n",rbuf);
Sleep(1000);
}
closesocket(sock);
WSACleanup();
return 0;
}
服务端:
#pragma comment(lib,"ws2_32.lib")
#include
#include
#include
#define MAX_CLIENT_NUM 63
int main()
{
WSADATA wsadata;
int err = WSAStartup(WINSOCK_VERSION,&wsadata);
if(err != 0)
{
printf("WSAStartup() failed :%d\n",WSAGetLastError());
return -1;
}
SOCKET sock = socket(AF_INET,SOCK_STREAM,0);
if(sock == INVALID_SOCKET)
{
printf("socket() failed:%d\n",WSAGetLastError());
WSACleanup();
return -1;
}
sockaddr_in localaddr;
localaddr.sin_family = AF_INET;
localaddr.sin_port = htons(27015);
localaddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
err = bind(sock,(sockaddr *)&localaddr,sizeof(localaddr));
if(err == INVALID_SOCKET)
{
printf("bind() failed:%d\n",WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
err=listen(sock,5);
if(err == INVALID_SOCKET)
{
printf("listen() failed:%d\n",WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
SOCKET client[MAX_CLIENT_NUM];
for(int i = 0;i
{
client[i] = INVALID_SOCKET;
}
fd_set rset,allset;
FD_ZERO(&allset);
FD_SET(sock,&allset);
#define MSG_MAX_SIZE 1024
char buf[MAX_CLIENT_NUM][MSG_MAX_SIZE];
while(1)
{
rset = allset;
int ret = select(0,&rset,NULL,NULL,NULL);
if(ret == SOCKET_ERROR)
{
printf("select() failed:%ld\n",WSAGetLastError());
break;
}
if(ret == 0){
continue;
}
if(FD_ISSET(sock,&rset))
{
sockaddr_in clientaddr;
int len = sizeof(clientaddr);
SOCKET sockconn = accept(sock,(sockaddr *)&clientaddr,&len);
if(sockconn == INVALID_SOCKET)
{
printf("accept() failed:%d\n",WSAGetLastError());
break;
}
printf("Client's IP:%s\nClient's Port:%d\n",inet_ntoa(clientaddr.sin_addr),clientaddr.sin_port);
for(i=0;i
{
if(client[i] == INVALID_SOCKET)
{
client[i] = sockconn;
break;
}
}
if(i
{
FD_SET(sockconn,&allset);
}
else{
printf("too many client");
closesocket(sockconn);
}
}
for(i=0;i
{
if((client[i]!=INVALID_SOCKET)&&FD_ISSET(client[i],&rset))
{
memset(buf[i],0,MSG_MAX_SIZE);
int rByte = recv(client[i],buf[i],MSG_MAX_SIZE,0);
if(rByte == SOCKET_ERROR)
{
printf("recv() failed:%d\n",WSAGetLastError());
FD_CLR(client[i],&allset);
closesocket(client[i]);
client[i] = INVALID_SOCKET;
continue;
}
if(rByte == 0)
{
printf("Connection is closed. ");
FD_CLR(client[i],&allset);
closesocket(client[i]);
client[i] = INVALID_SOCKET;
continue;
}
for(int j=0;j
{
if((client[j]!=INVALID_SOCKET) )
{
int sByte = send(client[j],buf[i],1024,0);
if(sByte == SOCKET_ERROR)
{
printf("send() failed:%d\n",WSAGetLastError());
}
}
}
printf("TCP recv[%d]DATA:%s\n",rByte,buf[i]);
}
}
}
closesocket(sock);
WSACleanup();
return 0;
}