一、服务端代码
-
#define WIN32_LEAN_AND_MEAN
-
#define _WINSOCK_DEPRECATED_NO_WARNINGS //for inet_pton()
-
-
#include <windows.h>
-
#include <WinSock2.h>
-
#include <iostream>
-
-
#pragma comment(lib, "ws2_32.lib")
-
-
using
namespace
std;
-
-
int main()
-
{
-
WORD ver = MAKEWORD(
2,
2);
-
WSADATA dat;
-
WSAStartup(ver, &dat);
-
-
//建立socket
-
SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
-
if (INVALID_SOCKET == _sock){
-
std::
cout <<
"ERROR:建立socket失败!" <<
std::
endl;
-
}
-
else {
-
std::
cout <<
"建立socket成功!" <<
std::
endl;
-
}
-
-
//初始化服务端地址
-
struct sockaddr_in _sin = {};
-
_sin.sin_addr.S_un.S_addr = inet_addr(
"192.168.0.104");
-
//_sin.sin_addr.S_un.S_addr = INADDR_ANY;
-
_sin.sin_family = AF_INET;
-
_sin.sin_port = htons(
4567);
-
-
//绑定服务端地址
-
if (SOCKET_ERROR == bind(_sock, (struct sockaddr*)&_sin,
sizeof(_sin))){
-
std::
cout <<
"ERROR:绑定地址失败!" <<
std::
endl;
-
}
-
else {
-
std::
cout <<
"绑定地址成功!" <<
std::
endl;
-
}
-
-
//监听网络端口
-
if (SOCKET_ERROR == listen(_sock,
5)) {
-
std::
cout <<
"ERROR:监听网络端口失败!" <<
std::
endl;
-
}
-
else {
-
std::
cout <<
"监听网络端口成功!" <<
std::
endl;
-
}
-
-
//用来保存客户端地址
-
struct sockaddr_in _clientAddr = {};
-
int nAddrLen =
sizeof(_clientAddr);
-
SOCKET _cSock = INVALID_SOCKET;
-
char msgBuf[] =
"Hello, I'm Server.";
-
-
//接收客户端连接
-
_cSock = accept(_sock, (struct sockaddr*)&_clientAddr, &nAddrLen);
-
if (INVALID_SOCKET == _cSock) {
-
std::
cout <<
"ERROR:接收到无效客户端!" <<
std::
endl;
-
}
-
else {
-
std::
cout <<
"接受到新的客户端连接,IP=" << inet_ntoa(_clientAddr.sin_addr)
-
<<
",Socket=" <<
static_cast<
int>(_cSock) <<
std::
endl;
-
}
-
-
char recvBuf[
1024] = {};
-
//循环处理客户端的数据
-
while (
true)
-
{
-
//接受数据
-
int _nLen = recv(_cSock, recvBuf,
1024,
0);
-
if (_nLen <=
0) {
-
std::
cout <<
"客户端已退出" <<
std::
endl;
-
break;
-
}
-
std::
cout <<
"收到命令" << recvBuf <<
std::
endl;
-
if (
0 ==
strcmp(recvBuf,
"getName")) {
-
char msgBuf[] =
"dongshao";
-
send(_cSock, msgBuf,
strlen(msgBuf) +
1,
0);
-
}
-
else
if (
0 ==
strcmp(recvBuf,
"getAge")) {
-
char msgBuf[] =
"21";
-
send(_cSock, msgBuf,
strlen(msgBuf) +
1,
0);
-
}
-
else {
-
char msgBuf[] =
"命令不正确";
-
send(_cSock, msgBuf,
strlen(msgBuf) +
1,
0);
-
}
-
}
-
-
//关闭服务端套接字
-
closesocket(_sock);
-
WSACleanup();
-
-
std::
cout <<
"服务端停止工作!" <<
std::
endl;
-
getchar();
//防止程序一闪而过
-
return
0;
-
}
二、客户端代码
-
#define WIN32_LEAN_AND_MEAN
-
#define _WINSOCK_DEPRECATED_NO_WARNINGS //for inet_pton()
-
-
#include <windows.h>
-
#include <WinSock2.h>
-
#include <iostream>
-
-
#pragma comment(lib, "ws2_32.lib")
-
-
using
namespace
std;
-
-
int main()
-
{
-
WORD ver = MAKEWORD(
2,
2);
-
WSADATA dat;
-
WSAStartup(ver, &dat);
-
-
//建立socket
-
SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
-
if (INVALID_SOCKET == _sock) {
-
std::
cout <<
"ERROR:建立socket失败!" <<
std::
endl;
-
}
-
else {
-
std::
cout <<
"建立socket成功!" <<
std::
endl;
-
}
-
-
//声明要连接的服务端地址
-
struct sockaddr_in _sin = {};
-
_sin.sin_addr.S_un.S_addr = inet_addr(
"192.168.0.104");
-
_sin.sin_family = AF_INET;
-
_sin.sin_port = htons(
4567);
-
-
//连接服务端
-
int ret = connect(_sock, (struct sockaddr*)&_sin,
sizeof(_sin));
-
if (SOCKET_ERROR == ret) {
-
std::
cout <<
"ERROR:连接服务端失败!" <<
std::
endl;
-
}
-
else {
-
std::
cout <<
"连接服务端成功!" <<
std::
endl;
-
}
-
-
char cmdBuf[
256] = {};
-
while (
true)
-
{
-
//输入数据
-
std::
cout <<
"cmd#:";
-
std::
cin >> cmdBuf;
-
-
//如果输入的是exit,退出程序
-
if (
0 ==
strcmp(cmdBuf,
"exit")) {
-
break;
-
}
-
-
//向服务端发送自己的命令
-
int _nLen = send(_sock, cmdBuf,
strlen(cmdBuf) +
1,
0);
-
if (_nLen ==
0) {
-
std::
cout <<
"输入有误,重新发送" <<
std::
endl;
-
continue;
-
}
-
else {
-
char recvBuf[
1024] = {};
-
_nLen = recv(_sock, recvBuf,
sizeof(recvBuf),
0);
-
if (_nLen <=
0) {
-
std::
cout <<
"命令接收错误" <<
std::
endl;
-
continue;
-
}
-
else {
-
std::
cout << recvBuf <<
std::
endl;
-
}
-
}
-
}
-
-
//关闭服务端套接字
-
closesocket(_sock);
-
WSACleanup();
-
-
std::
cout <<
"服务端停止工作!" <<
std::
endl;
-
getchar();
//防止程序一闪而过
-
return
0;
-
}
三、演示效果
- 双击服务端程序
- 双击客户端程序,可以看到服务端成功接收到请求,客户端等待命令输入
- 客户端输入信息到服务端,并交互数据
转载:https://blog.csdn.net/qq_41453285/article/details/105281284
查看评论