Server.cpp
#include<WINSOCK2.H>
#include<iostream>
using namespace std;
#include<stdlib.h>
#define BUF_SIZE 64
#pragma comment(lib,"WS2_32.lib")
int main()
{
WSADATA wsd;
SOCKET sServer;
SOCKET sClient;
int retVal;//调用Socket函数的返回值
char buf[BUF_SIZE];
//初始化Socket环境
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
printf("WSAStartup failed!
");
return 1;
}
//创建监听的Socket
sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sServer)
{
printf("socket failed!
");
WSACleanup();
return -1;
}
//设置服务器Socket地址
SOCKADDR_IN addrServ;
addrServ.sin_family=AF_INET;
addrServ.sin_port=htons(1298);
addrServ.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
//绑定Sockets Server
retVal=bind(sServer,(const struct sockaddr*)&addrServ,sizeof(SOCKADDR_IN));
if(SOCKET_ERROR==retVal)
{
printf("bind failed!
");
closesocket(sServer);
WSACleanup();
return -1;
}
//在Sockets Server上进行监听
retVal=listen(sServer,1);
if(SOCKET_ERROR==retVal)
{
printf("listen failed!
");
closesocket(sServer);
WSACleanup();
return -1;
}
//接收来自客户端的请求
printf("TCPServer start...
");
sockaddr_in addrClient;
int addrClientlen=sizeof(addrClient);
sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);
if(INVALID_SOCKET==sClient)
{
printf("accept failed!
");
closesocket(sServer);
WSACleanup();
return -1;
}
//循环接收客户端的数据,直接客户端发送quit命令后退出
while(true)
{
ZeroMemory(buf,BUF_SIZE);//清空接收数据的缓冲区
retVal=recv(sClient,buf,BUFSIZ,0);
if(SOCKET_ERROR==retVal)
{
printf("recv failed!
");
closesocket(sServer);
closesocket(sClient);
WSACleanup();
return -1;
}
//获取当前系统时间
SYSTEMTIME st;
GetLocalTime(&st);
char sDateTime[30];
sprintf(sDateTime,"%4d-%2d-%2d-%2d:%2d:%2d:%2d",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
//打印输出的信息
printf("%s,Recv From Client [%s:%d]:%s
",sDateTime,inet_ntoa(addrClient.sin_addr),addrClient.sin_port,buf);
//如果客户端发送quit字符串,则服务器退出
if(strcmp(buf,"quit")==0)
{
retVal=send(sClient,"quit",strlen("quit"),0);
break;
}
//否则向客户端发送回显字符串
else
{
char msg[BUF_SIZE];
sprintf(msg,"Message received - %s
",buf);
retVal=send(sClient,msg,strlen(msg),0);
if(SOCKET_ERROR==retVal)
{
printf("send failed!
");
closesocket(sServer);
closesocket(sClient);
WSACleanup();
return -1;
}
//释放Socket
closesocket(sServer);
closesocket(sClient);
WSACleanup();
//暂停,按任意键退出
system("pause");
return 0;
}
}
}
Client.cpp
#include<WINSOCK2.H>
#include<iostream>
#include<string>
using namespace std;
#include<stdlib.h>
#define BUF_SIZE 64
#pragma comment(lib,"WS2_32.lib")
int main()
{
WSADATA wsd;
SOCKET sHost;
SOCKADDR_IN servAddr;//服务器地址
int retVal;//调用Socket函数的返回值
char buf[BUF_SIZE];
//初始化Socket环境
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
printf("WSAStartup failed!
");
return 1;
}
//创建监听的Socket
sHost=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sHost)
{
printf("socket failed!
");
WSACleanup();
return -1;
}
//设置服务器Socket地址
servAddr.sin_family=AF_INET;
servAddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
//在实际应用中,建议将服务器的IP地址和端口号保存在配置文件中
servAddr.sin_port=htons(1298);
//计算地址的长度
int sServerAddlen=sizeof(servAddr);
//链接服务器
retVal=connect(sHost,(LPSOCKADDR)&servAddr,sizeof(servAddr));
if(SOCKET_ERROR==retVal)
{
printf("send failed!
");
closesocket(sHost);
WSACleanup();
return -1;
}
while(true)
{
//向服务器发送字符串,并显示反馈信息
printf("input a string to send:");
std::string str;
//接收输入的数据
std::getline(std::cin,str);
//将用户输入的数据复制到buf中
ZeroMemory(buf,BUF_SIZE);
strcpy(buf,str.c_str());
//向服务器发送数据
retVal=send(sHost,buf,strlen(buf),0);
if(SOCKET_ERROR==retVal)
{
printf("send failed!
");
closesocket(sHost);
WSACleanup();
return -1;
}
//接收服务器回传的数据
retVal=recv(sHost,buf,sizeof(buf)+1,0);
printf("Recv From Server : %s
",buf);
if(strcmp(buf,"quit")==0)
{
printf("quit!
");
break;
}
}
return 0;
}