#include #include #include._tcp client 模式">搜索查看编辑修改首页UNITYNODEJSPYTHONAIGITPHPGOCEF3JAVAHTMLCSS搜索运维做开发 这个屌丝很懒,什么也没留下! 关注作者热门标签jqueryHTMLCSSPHPASPPYTHONGOAICC++C#PHOTOSHOPUNITYiOSandroidvuexml爬虫SEOLINUXWINDOWSJAVAMFCCEF3CADNODEJSGITPyppeteerarticle热门文章1leetcode刷题:相交链表2网络安全学习篇_虚拟机系统更新怎么关闭3大语言模型应用指南:什么是尺度定律4–jasypt.encryptor.password配置文件密码加密在idea中的配置_idea 配置--jasypt.encryptor.password5Python库(1)—— Numpy 科学计算库_numpy库6【AI文本工具站】日活近4万7趣味算法------开灯问题8Ubuntu20.04双系统 无线网卡驱动(未发现wifi适配器)、Nvidia显卡驱动安装一条龙教程+疑难杂症修复【多坑预警】_ubuntu20.04安装无线网卡驱动9银河麒麟高级服务器操作系统V10SP2(X86)PXE服务端部署以及测试_服务器用网络安装装麒麟系统10为什么输入法显示中文打不出中文_搜狗输入法打不出中文怎么回事 搜狗输入法在qq上打不出汉字解决办法...当前位置: article > 正文 TCP模式的一个服务器和多个客户端通信_tcp client 模式 作者:运维做开发 | 2024-08-08 20:06:30 赞踩tcp client 模式 服务端 #define _WINSOCK_DEPRECATED_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS#include <WinSock2.h>#include <WS2tcpip.h>#pragma comment(lib,"ws2_32.lib")#include <stdio.h>#include <Windows.h>#include <conio.h>#include <map>#include <algorithm>#include <string>#include <vector>#include <iostream>using std::string;using std::map;using std::vector;map<USHORT, SOCKET> socketMap; //发送数据void WINAPI SendMsg(){ while (1) { // '+' 43 // '/' 47 // '*' 42 int key = _getch(); if (key == 43) //发送消息 { printf("【*】群发【/】单发\n"); key = _getch(); printf("发送内容:"); char sendStr[100] = {}; gets_s(sendStr, sizeof(sendStr)); //scanf("%s",sendStr); int sendRet = 0; if(key == 42 ) //群发 { for (auto inter = socketMap.begin(); inter != socketMap.end(); ++inter) { SOCKET clientSocket = inter->second; sendRet = send(clientSocket, sendStr, strlen(sendStr), 0); } } else if(key == 47) //单发 { printf("客户端端口号:"); int hostPort = 0; //scanf("%d",&port); char temp[100] = {}; gets_s((char*)&temp, sizeof(temp)); sscanf(temp, "%d", &hostPort); if (socketMap.find(hostPort) != socketMap.end()) { SOCKET clientSocket = socketMap.at(hostPort); sendRet = send(clientSocket, sendStr, strlen(sendStr), 0); } else { printf("输入端口号有误\n"); continue; } } if(sendRet != 0) printf("已发送\n"); else printf("发送失败\n"); } }} //接收客户端数据DWORD WINAPI RecvMsg(SOCKET* clientSocket){ //int threadId = GetCurrentThreadId(); //int n = 5; while (1) { char recvStr[10000] = {}; int recvRet = recv(*clientSocket, recvStr, sizeof(recvStr), 0); if (recvRet != -1 && recvRet != 0) printf("thread【%d】:%s\n=========\n", (int)GetCurrentThreadId(), recvStr); else { auto retVal = std::find_if(socketMap.begin(), socketMap.end(), [clientSocket](map<USHORT, SOCKET>::value_type val) { return val.second == *clientSocket; }); if (retVal != socketMap.end()) { printf("客户端%d已断开\n", retVal->first); closesocket(retVal->second); socketMap.erase(retVal); break; } else { printf("没有该客户端\n"); } } } return 0;} int main(){ CreateThread(0, 0, (LPTHREAD_START_ROUTINE)SendMsg, 0, 0, 0); WSADATA wsaData = { 0 }; WSAStartup(MAKEWORD(2,2),&wsaData); SOCKET serverSocket = socket(AF_INET,SOCK_STREAM,0); sockaddr_in sockaddr; sockaddr.sin_family = AF_INET; sockaddr.sin_addr.S_un.S_addr = INADDR_ANY; //监听本机任意IP地址的8888端口 //sockaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); sockaddr.sin_port = htons(8888); int bindRet = bind(serverSocket, (SOCKADDR*)&sockaddr, sizeof(sockaddr)); if (bindRet == SOCKET_ERROR) { printf("绑定失败\n"); closesocket(serverSocket); WSACleanup(); getchar(); exit(0); } int listenRet = listen(serverSocket, 3); if (listenRet == -1) { printf("监听失败\n"); closesocket(serverSocket); WSACleanup(); system("pause"); return 0; } printf("监听中...\n"); while (1) { SOCKADDR_IN clientSockAddr_in = {}; int len = sizeof(clientSockAddr_in); SOCKET clientSocket = accept(serverSocket, (SOCKADDR*)&clientSockAddr_in, &len); printf("有客户端连接\n"); printf("ip=%s,port=%d\n", inet_ntoa(clientSockAddr_in.sin_addr), ntohs(clientSockAddr_in.sin_port)); socketMap[ntohs(clientSockAddr_in.sin_port)] = clientSocket; CreateThread(0, 0, (LPTHREAD_START_ROUTINE)RecvMsg, &socketMap[ntohs(clientSockAddr_in.sin_port)], 0, 0); }} 客户端 #define _WINSOCK_DEPRECATED_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #include <WinSock2.h>#include <WS2tcpip.h>#pragma comment(lib,"ws2_32.lib")#include <stdio.h>#include <iostream> DWORD WINAPI RecvMsg(const SOCKET* clientSocket){ while (1) { char recvStr[100] = {}; int recvRet = recv(*clientSocket, recvStr, sizeof(recvStr), 0); if (recvRet != -1) printf("服务器说:%s\n", recvStr); else { printf("服务器已断开\n"); closesocket(*clientSocket); WSACleanup(); system("pause"); closesocket(*clientSocket); WSACleanup(); exit(0); } } return 0;} int main(){ WSADATA wsaData = { 0 }; WSAStartup(MAKEWORD(2, 2), &wsaData); SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0); //******bind自定义端口号测试*********/ SOCKADDR_IN clientSockAddr_in = { 0 }; clientSockAddr_in.sin_family = AF_INET; clientSockAddr_in.sin_addr.S_un.S_addr = INADDR_ANY; //本机IP clientSockAddr_in.sin_port = htons(2222); if(bind(clientSocket, (SOCKADDR*)&clientSockAddr_in, sizeof(clientSockAddr_in)) == SOCKET_ERROR) { std::cout << "绑定套接字失败\n"; closesocket(clientSocket); WSACleanup(); system("pause"); return 0; } /***************************************/ SOCKADDR_IN sockaddr_in; sockaddr_in.sin_family = AF_INET; sockaddr_in.sin_addr.S_un.S_addr = inet_addr("172.16.20.203"); //inet_addr("192.168.18.173"); //服务端ip地址 //sockaddr_in.sin_addr.S_un.S_addr = inet_addr("192.168.74.128"); sockaddr_in.sin_port = htons(8888); //服务端监听的端口 int conRet = connect(clientSocket,(SOCKADDR*)&sockaddr_in, sizeof(sockaddr_in)); if (conRet != SOCKET_ERROR) { printf("连接成功\n"); CreateThread(0, 0, (LPTHREAD_START_ROUTINE)RecvMsg, &clientSocket, 0, 0); } else { printf("连接失败,返回%d\n", conRet); closesocket(clientSocket); WSACleanup(); } while (1) { printf("输入内容\n"); char sendStr[10000] = {}; //scanf("%s", sendStr); //遇到空格会截断后面内容 gets_s(sendStr, sizeof(sendStr)); int ret = send(clientSocket, sendStr, strlen(sendStr), 0); if (ret != 0) printf("客户端发送数据%dbyte\n", ret); }} 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/949687推荐阅读article图神经网络(GNNs)模型学习笔记与总结_图神经网路学习笔记 知乎...GCN学习笔记1 基于谱域的GCN1.1 知识要点:1.2 Spectral-based models1.2.1 Spe... 赞踩article探索Go语言中的MP4处理库:go-mp4...探索Go语言中的MP4处理库:go-mp4项目地址:https://gitcode.com/abema/go-mp4项目... 赞踩article信号时域统计分析—matlab_时域统计分析是什么...信号时域分析—matlab_时域统计分析是什么时域统计分析是什么 时域分析即在时间域上对... 赞踩articleRabbitMQ面试题_rabbitmq 面试...Exchange是消息的交换机,它接收发布到RabbitMQ的消息,并根据指定的规则将消息路由到一个或多个Queue中。... 赞踩article斯坦福大学分析3.2万篇大模型HuggingFace文档后指出:现状堪忧!_huggingface统...是一种标准化的文档形式,旨在提供关于AI模型的基本信息,其灵感来源于食品营养标签和电子行业的数据表。模型卡片的核心部分包... 赞踩article语音识别预训练wav2vec 2.0...语音识别预训练wav2vec 2.0_wav2vec 2.0wav2vec 2.0 ... 赞踩articleOne Test to Rule Them All-一种通过规范化减少语义复杂性从而简化用例集的方法...本文介绍了一种通过规范化和泛化减少软件测试用例冗余的方法,利用TSTL工具包和DSL,旨在将多个测试转化为单一测试,降低... 赞踩article产品经理日常工作流程汇总_产品经理工作管理流程...产品经理全生命周期工作流程汇总_产品经理工作管理流程产品经理工作管理流程 产品经理在日常... 赞踩article大模型基础教程:一图尽览大模型知识点!_大模型学习...近年来,人工智能技术在全球范围内取得了显著的成果,特别是在计算机视觉、自然语言处理、语音识别等领域。这些成果的取得离不开... 赞踩article2024年死磕这4款AI编程工具,助你代码起飞_marscode和tongyilingma...以上四款 AI 代码助手工具,V 哥在使用过后,觉得都非常不错,在国内的 AI 编程助手工具中是领先的,相比国外的 Gi... 赞踩articleflink集成springboot案例_Flink快速入门...本文介绍了如何快速入门Flink,从开发环境配置到Flink程序的开发步骤,包括选择IntelliJ IDEA作为开发工... 赞踩article【人工智能】Transformers之Pipeline(二):自动语音识别(automatic-sp...本文对transformers之pipeline的自动语音识别(automatic-speech-recognition... 赞踩articleGit 命令行操作_git的命令行操作...1、本地库初始化命令:git init效果注意:.git 目录中存放的是本地库相关的子目录和文件,不要删除,也不要胡 乱... 赞踩article鸿蒙(OpenHarmony)开发实战:WiFi扫码自动配网_鸿蒙wifi配网...它利用了移动互联网时代的技术优势,解决了传统WiFi连接中的痛点,是一种值得推广的网络连接方式。_鸿蒙wifi配网鸿蒙w... 赞踩articleFPGA之zynq以太网(2)_zynq串口配置以太网ip...本文讲述了使用Zynq与PC通过网线连接进行以太网配置的过程,遇到的IPv4连接问题,包括IP地址设置、默认网关冲突以及... 赞踩articleZooKeeper 安装...文章详细介绍了如何在Linux环境中安装ZooKeeper,包括下载JDK,上传并解压ZooKeeper安装包,配置zo... 赞踩articlejson.dump(), json.dumps()与json.load(), json.loads(...json 模块提供了一种很简单的方式来编码和解码JSON数据。 其中四个主要的函数是 json.dump(), json... 赞踩articleWindows消息类型及说明_windows编程窗口收到电源状态变化事件...WM_Window Message窗口消息,一般用在SendMessage,PostMessage这样的消息函数中SM_... 赞踩article2022互联网年终奖大赏!你们年终奖发了多少?...点击关注下方公众号,架构师全套资料 都在这里0、2T架构师学习资料干货分享上一篇:Spring Boot官宣:正式弃用 ... 赞踩article隧道技术和代理技术(四)SSH&DNS&ICMP&SMB&上线通讯_icmp ssh...本文详细讲解了SSH、DNS、ICMP和SMB隧道技术在内网穿透中的应用,以及如何通过DNS隧道解决网络控制和通讯问题。... 赞踩相关标签人工智能图嵌入深度学习神经网络机器学习信号处理rabbitmq分布式语音识别功能测试测试工具论文阅读产品经理产品运营学习aiAIGCAI编程java威哥爱编程flink集成springboot案例
赞
踩
#define _WINSOCK_DEPRECATED_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS#include <WinSock2.h>#include <WS2tcpip.h>#pragma comment(lib,"ws2_32.lib")#include <stdio.h>#include <Windows.h>#include <conio.h>#include <map>#include <algorithm>#include <string>#include <vector>#include <iostream>using std::string;using std::map;using std::vector;map<USHORT, SOCKET> socketMap; //发送数据void WINAPI SendMsg(){ while (1) { // '+' 43 // '/' 47 // '*' 42 int key = _getch(); if (key == 43) //发送消息 { printf("【*】群发【/】单发\n"); key = _getch(); printf("发送内容:"); char sendStr[100] = {}; gets_s(sendStr, sizeof(sendStr)); //scanf("%s",sendStr); int sendRet = 0; if(key == 42 ) //群发 { for (auto inter = socketMap.begin(); inter != socketMap.end(); ++inter) { SOCKET clientSocket = inter->second; sendRet = send(clientSocket, sendStr, strlen(sendStr), 0); } } else if(key == 47) //单发 { printf("客户端端口号:"); int hostPort = 0; //scanf("%d",&port); char temp[100] = {}; gets_s((char*)&temp, sizeof(temp)); sscanf(temp, "%d", &hostPort); if (socketMap.find(hostPort) != socketMap.end()) { SOCKET clientSocket = socketMap.at(hostPort); sendRet = send(clientSocket, sendStr, strlen(sendStr), 0); } else { printf("输入端口号有误\n"); continue; } } if(sendRet != 0) printf("已发送\n"); else printf("发送失败\n"); } }} //接收客户端数据DWORD WINAPI RecvMsg(SOCKET* clientSocket){ //int threadId = GetCurrentThreadId(); //int n = 5; while (1) { char recvStr[10000] = {}; int recvRet = recv(*clientSocket, recvStr, sizeof(recvStr), 0); if (recvRet != -1 && recvRet != 0) printf("thread【%d】:%s\n=========\n", (int)GetCurrentThreadId(), recvStr); else { auto retVal = std::find_if(socketMap.begin(), socketMap.end(), [clientSocket](map<USHORT, SOCKET>::value_type val) { return val.second == *clientSocket; }); if (retVal != socketMap.end()) { printf("客户端%d已断开\n", retVal->first); closesocket(retVal->second); socketMap.erase(retVal); break; } else { printf("没有该客户端\n"); } } } return 0;} int main(){ CreateThread(0, 0, (LPTHREAD_START_ROUTINE)SendMsg, 0, 0, 0); WSADATA wsaData = { 0 }; WSAStartup(MAKEWORD(2,2),&wsaData); SOCKET serverSocket = socket(AF_INET,SOCK_STREAM,0); sockaddr_in sockaddr; sockaddr.sin_family = AF_INET; sockaddr.sin_addr.S_un.S_addr = INADDR_ANY; //监听本机任意IP地址的8888端口 //sockaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); sockaddr.sin_port = htons(8888); int bindRet = bind(serverSocket, (SOCKADDR*)&sockaddr, sizeof(sockaddr)); if (bindRet == SOCKET_ERROR) { printf("绑定失败\n"); closesocket(serverSocket); WSACleanup(); getchar(); exit(0); } int listenRet = listen(serverSocket, 3); if (listenRet == -1) { printf("监听失败\n"); closesocket(serverSocket); WSACleanup(); system("pause"); return 0; } printf("监听中...\n"); while (1) { SOCKADDR_IN clientSockAddr_in = {}; int len = sizeof(clientSockAddr_in); SOCKET clientSocket = accept(serverSocket, (SOCKADDR*)&clientSockAddr_in, &len); printf("有客户端连接\n"); printf("ip=%s,port=%d\n", inet_ntoa(clientSockAddr_in.sin_addr), ntohs(clientSockAddr_in.sin_port)); socketMap[ntohs(clientSockAddr_in.sin_port)] = clientSocket; CreateThread(0, 0, (LPTHREAD_START_ROUTINE)RecvMsg, &socketMap[ntohs(clientSockAddr_in.sin_port)], 0, 0); }}
#define _WINSOCK_DEPRECATED_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #include <WinSock2.h>#include <WS2tcpip.h>#pragma comment(lib,"ws2_32.lib")#include <stdio.h>#include <iostream> DWORD WINAPI RecvMsg(const SOCKET* clientSocket){ while (1) { char recvStr[100] = {}; int recvRet = recv(*clientSocket, recvStr, sizeof(recvStr), 0); if (recvRet != -1) printf("服务器说:%s\n", recvStr); else { printf("服务器已断开\n"); closesocket(*clientSocket); WSACleanup(); system("pause"); closesocket(*clientSocket); WSACleanup(); exit(0); } } return 0;} int main(){ WSADATA wsaData = { 0 }; WSAStartup(MAKEWORD(2, 2), &wsaData); SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0); //******bind自定义端口号测试*********/ SOCKADDR_IN clientSockAddr_in = { 0 }; clientSockAddr_in.sin_family = AF_INET; clientSockAddr_in.sin_addr.S_un.S_addr = INADDR_ANY; //本机IP clientSockAddr_in.sin_port = htons(2222); if(bind(clientSocket, (SOCKADDR*)&clientSockAddr_in, sizeof(clientSockAddr_in)) == SOCKET_ERROR) { std::cout << "绑定套接字失败\n"; closesocket(clientSocket); WSACleanup(); system("pause"); return 0; } /***************************************/ SOCKADDR_IN sockaddr_in; sockaddr_in.sin_family = AF_INET; sockaddr_in.sin_addr.S_un.S_addr = inet_addr("172.16.20.203"); //inet_addr("192.168.18.173"); //服务端ip地址 //sockaddr_in.sin_addr.S_un.S_addr = inet_addr("192.168.74.128"); sockaddr_in.sin_port = htons(8888); //服务端监听的端口 int conRet = connect(clientSocket,(SOCKADDR*)&sockaddr_in, sizeof(sockaddr_in)); if (conRet != SOCKET_ERROR) { printf("连接成功\n"); CreateThread(0, 0, (LPTHREAD_START_ROUTINE)RecvMsg, &clientSocket, 0, 0); } else { printf("连接失败,返回%d\n", conRet); closesocket(clientSocket); WSACleanup(); } while (1) { printf("输入内容\n"); char sendStr[10000] = {}; //scanf("%s", sendStr); //遇到空格会截断后面内容 gets_s(sendStr, sizeof(sendStr)); int ret = send(clientSocket, sendStr, strlen(sendStr), 0); if (ret != 0) printf("客户端发送数据%dbyte\n", ret); }}