#define WIN32_LEAN_AND_MEAN #define _WINSOCK_DEPRECATED_NO_WARNINGS #include<iostream> #include<stdio.h> #include<Windows.h> #include<WinSock2.h> //#pragma comment(lib,"ws2_32.lib")// win 写法 + 动态库 using namespace std; int main() { WORD ver = MAKEWORD(2, 2); WSADATA data; WSAStartup(ver, &data); //socket 网络通信 代码 //1.建立socket SOCKET _cSock = socket(AF_INET, SOCK_STREAM, 0); if (INVALID_SOCKET == _cSock) { printf("qaaabuild socket failed "); } else { printf("套接字建立成功 "); } //2.连接服务器 connect sockaddr_in _sin = {}; _sin.sin_family = AF_INET; _sin.sin_port = htons(4567); _sin.sin_addr.S_un.S_addr = inet_addr("192.168.0.100"); int ret= connect(_cSock, (sockaddr*)&(_sin), sizeof(_sin)); if (SOCKET_ERROR == ret) { printf("服务器连接成功失败 "); } else { printf("服务器连接成功 "); } //2.1 while (true) { //2.1 输入请求命令 char cmdBuf[128] = {}; scanf("%s", cmdBuf); //2.2处理请求 if (0 == strcmp(cmdBuf, "exit")) { break; } else { //2.3向服务器发送请求 send(_cSock, cmdBuf, strlen(cmdBuf)+1, 0); } //3.recv接收服务器信息 char recvbuf[256] = {}; int nlen = recv(_cSock, recvbuf, 256, 0); if (nlen > 0) { printf("接收到%d字节: %s", nlen, recvbuf); } } //3.recv接收服务器信息 char recvbuf[256] = {}; int nlen= recv(_cSock, recvbuf, 256, 0); if (nlen > 0) { printf("%d: %s", nlen ,recvbuf); } //4. closesocket closesocket(_cSock); WSACleanup(); getchar(); return 0; }
#define WIN32_LEAN_AND_MEAN #define _WINSOCK_DEPRECATED_NO_WARNINGS #include<iostream> #include<stdio.h> #include<Windows.h> #include<WinSock2.h> //#pragma comment(lib,"ws2_32.lib")// win 写法 + 动态库 using namespace std; int main() { WORD ver = MAKEWORD(2, 2); WSADATA data; WSAStartup(ver, &data); //socket 网络通信 代码 //tcp 服务端 //1. 建立一个socket SOCKET _sock= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //2.绑定用于客户端接收的网路端口 sockaddr_in _sin = {}; _sin.sin_family = AF_INET; _sin.sin_port = htons(4567); _sin.sin_addr.S_un.S_addr = htonl(INADDR_ANY);// inet_addr("127.0.0.1");//服务器 要绑定的ip地址 if (SOCKET_ERROR == bind(_sock, (sockaddr*)&_sin, sizeof(_sin))) { printf("bind error "); } else { printf("bind successful "); } //3.监听端口 if (SOCKET_ERROR == listen(_sock, 5)) { printf("listen error "); } else { printf("listen successful "); } //4。accept 等待接收客户连接 sockaddr_in _clientAddr = {}; SOCKET _cSock ; char msgbuf[] = "hello world"; //int _cSock_Strlen = strlen(msgbuf) + 1; int len = sizeof(_clientAddr); //_cSock = accept(_sock, (sockaddr*)&_clientAddr, (int*)sizeof(_clientAddr)); //会返回客户端地址 _cSock = accept(_sock, (sockaddr*)&_clientAddr, &len); //会返回客户端地址 if (INVALID_SOCKET == _cSock) { printf("连接可客户端失败"); } printf("new client:IP=%s ", inet_ntoa(_clientAddr.sin_addr)); char _recv_cmdbuf[128] = {}; while (true) { //持续处理请求的c/s //4.1 int rLen = recv(_cSock, _recv_cmdbuf, 128, 0); if (rLen <= 0) { printf("client exited()"); break; } printf("收到命令%s ", _recv_cmdbuf); //4.2 //处理请求 if (0 == strcmp(_recv_cmdbuf, "getName")) { char msgbuf[] = "狗娃 "; send(_cSock, msgbuf, strlen(msgbuf) + 1, 0); } else if (0 == strcmp(_recv_cmdbuf, "getAge")) { char msgbuf[] = "狗娃 18 "; send(_cSock, msgbuf, strlen(msgbuf) + 1, 0); } else { char msgbuf[] = "????"; send(_cSock, msgbuf, strlen(msgbuf) + 1, 0); } //5。send 发送一条数据 send(_cSock, msgbuf, strlen(msgbuf) + 1, 0); } //6. 关闭socket closesocket(_sock); WSACleanup(); getchar(); return 0; }