- 创建VS工程
- 配置
- 属性页-链接器-输入-附加依赖项,添加: ws2_32.lib
- heads.h
1 2 3 4 5 6 7 8 9 |
#pragma once //避免宏的重复引用 #define WIN32_LEAN_AND_MEAN //使用到inet_ntoa #define _SOCKET_DEPRECATED_NO_WARNINGS #include <WinSocket2.h> #include <Windows.h> #include <iostream> using namespace std; |
- server.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
int main() { const WORD socket_ver = MAKEWORD(2, 2);//socket版本 WSADATA dat; auto start_result = WSAStartup(socket_ver, &dat);//启动socket环境 if (start_result != Aet::Start_error) { cout << "Socket evn start failed!!!" << endl; return Aet::Error_exit; } else cout << "Socket evn start success..." << endl; //1.创建socket auto server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (server_sock == INVALID_SOCKET) { cout << "Socket created failed!!!" << endl; return Aet::Error_exit; } else cout << "Socket created success..." << endl; //2.绑定端口 //inet_pton(AF_INET, "127.0.0.1", &_sin.sin_addr.S_un.S_addr);表示只能内网访问 //INADDR_ANY[0.0.0.0]表示所有网络都可以访问 sockaddr_in _sin{}; _sin.sin_family = AF_INET;//ipv4 _sin.sin_port = htons(4567);//host to net unsigned short inet_pton(AF_INET, "0.0.0.0", &_sin.sin_addr.S_un.S_addr); auto bind_result = bind(server_sock, reinterpret_cast<sockaddr*>(&_sin),sizeof(sockaddr_in)); if(bind_result == SOCKET_ERROR) { cout << "Socket bind failed with code :" << WSAGetLastError() << endl; return Aet::Error_exit; } //3.监听网络端口 if (listen(server_sock, 5) == SOCKET_ERROR) { cout << "Server listen failed!!!" << endl; return Aet::Error_exit; } else cout << "Server listen success..." << endl; //4.accept 等待接收客户端连接 sockaddr_in clientAddr{}; int addrLength = sizeof(sockaddr_in); char msgBuf[256] = "Hello,i'm Server."; SOCKET client_sock = accept(server_sock, reinterpret_cast<sockaddr*>(&clientAddr), &addrLength); if (client_sock == INVALID_SOCKET) { cout << "Invalid client socket!!!" << endl; return Aet::Error_exit; } string ip; inet_ntop(AF_INET, &clientAddr.sin_addr.S_un.S_addr, const_cast<PSTR>(ip.c_str()), 10); cout << "Socket:" << client_sock << ".Ip:" << ip.c_str() << endl; while (true) { char cmdBuff[128] = {}; //5.接收客户端请求 auto cmdLength = recv(client_sock, const_cast<char*>(cmdBuff), 128,0); if(cmdLength <= 0) { cout << "Commands data error!!!" << endl; break; } cout << "Get Command:" << cmdBuff << endl;//打印收到命令 //6.处理客户端请求 if(!strcmp(cmdBuff,"exit")) { cout << "Client exit ..." << endl; } else if(strcmp(cmdBuff,"getName") == 0) { strcpy_s(msgBuf,256,"Aet"); send(client_sock, msgBuf, 256, 0); } else if(strcmp(cmdBuff,"getAge") == 0) { strcpy_s(msgBuf, 256, "26"); send(client_sock, msgBuf, 256, 0); } else { //7.send strcpy_s(msgBuf,256,"What's the command meaning?"); send(client_sock, msgBuf, 256, 0); } } //8.closesocket closesocket(server_sock); WSACleanup(); return 0; } |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ Socket基础:TCP篇10/16
- ♥ 创建socket环境:hello socket10/16
- ♥ 51CTO:Linux C++网络编程四08/19
- ♥ 51CTO:C++网络通信引擎架构与实现一09/09
- ♥ Linux 高性能服务器编程:网络基础编程一11/27
- ♥ Linux 高性能服务器编程:网络基础编程二11/28