当前位置:   article > 正文

C++网络通信实例(TCP/IP协议,包括服务端与客户端通信)_c++通信协议编程

c++通信协议编程

创作不易 觉得有帮助请点赞关注收藏

TCP/IP是当下网络协议栈中的主流协议 TCP属于传输层的协议  可靠传输 包括经典的三次握手等等

IP协议是网络层协议 尽全力传输但不可靠 学过计算机网络的同学们对这个应该比较熟悉

以下是使用C++进行网络通信的实例 

服务端

主要使用多线程技术建立连接 也就是一个服务端可以连接多个客户端 最大连接数可以限定

向客户端发送字符串this is server 并启动一个线程等待客户端发送信息过来

如果客户端发送A过来 服务器返回B 发送C过来 服务器返回D 发送exit之后服务器关闭线程

效果图如下

 

代码如下

  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<WinSock2.h>
  4. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  5. using namespace std;
  6. #pragma comment(lib, "ws2_32.lib")
  7. DWORD WINAPI threadpro(LPVOID pParam) {
  8. SOCKET hsock = (SOCKET)pParam;
  9. char buffer[1024];
  10. char sendBuffer[1024];
  11. if (hsock != INVALID_SOCKET)
  12. cout << "start receive" << endl;
  13. while (1)
  14. {
  15. int num = recv(hsock, buffer, 1024, 0);
  16. if (num >= 0)
  17. cout << "receive from clinet" << buffer << endl;
  18. if (!strcmp(buffer, "A"))
  19. {
  20. memset(sendBuffer, 0, 1024);
  21. strcpy_s(sendBuffer, "B");
  22. int ires = send(hsock, sendBuffer, sizeof(sendBuffer), 0);//回送信息
  23. cout << "send to client" << sendBuffer << endl;
  24. }
  25. else if (!strcmp(buffer, "C"))
  26. {
  27. memset(sendBuffer, 0, 1024);
  28. strcpy_s(sendBuffer, "D");
  29. int ires = send(hsock, sendBuffer, sizeof(sendBuffer), 0);//回送信息
  30. cout << "send to client" << sendBuffer << endl;
  31. }
  32. else if (!strcmp(buffer, "exit"))
  33. {
  34. cout << "client close" << endl;
  35. cout << "server process close" << endl;
  36. return 0;
  37. }
  38. else
  39. {
  40. memset(sendBuffer, 0, 1024);
  41. strcpy_s(sendBuffer, "ERR");
  42. int ires = send(hsock, sendBuffer, sizeof(sendBuffer), 0);//回送信息
  43. cout << "send to client" << sendBuffer << endl;
  44. }
  45. }
  46. return 0;
  47. }
  48. void main() {
  49. WSADATA wsd;
  50. WSAStartup(MAKEWORD(2, 2), &wsd);
  51. SOCKET m_SockServer;
  52. sockaddr_in serveraddr;
  53. sockaddr_in serveraddrfrom;
  54. SOCKET m_Server[20];
  55. serveraddr.sin_family = AF_INET;
  56. serveraddr.sin_port = htons(4600);//端口号
  57. //serveraddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
  58. m_SockServer = socket(AF_INET, SOCK_STREAM, 0);
  59. int i = bind(m_SockServer, (sockaddr*)&serveraddr, sizeof(serveraddr));//绑定数
  60. cout << "bind:" << i << endl;
  61. int imaxconnect = 20;
  62. int iconnect = 0;
  63. int ilisret;
  64. char buf[] = "this is server\0";
  65. char WarnBuf[] = "it is over max connect\0";
  66. int len = sizeof(sockaddr);
  67. while (1) {
  68. ilisret = listen(m_SockServer, 0);
  69. m_Server[iconnect] = accept(m_SockServer, (sockaddr*)&serveraddrfrom, &len);
  70. if (m_Server[iconnect] != INVALID_SOCKET)
  71. {
  72. int ires = send(m_Server[iconnect], buf, sizeof(buf), 0);
  73. cout << "accept:" << ires << endl;
  74. iconnect++;
  75. if (iconnect > imaxconnect)
  76. {
  77. int ires = send(m_Server[iconnect], WarnBuf, sizeof(WarnBuf), 0);
  78. }
  79. else
  80. {
  81. HANDLE m_handle;
  82. DWORD nThreadld = 0;
  83. m_handle = (HANDLE)::CreateThread(NULL, 0, threadpro, (LPVOID)m_Server[--iconnect], 0, &nThreadld);//启动 线程
  84. }
  85. }
  86. WSACleanup();
  87. }
  88. }

客户端

  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include"winsock2.h"
  4. #include<time.h>
  5. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  6. using namespace std;
  7. #pragma comment(lib,"ws2_32.lib")
  8. void main() {
  9. WSADATA wsd;
  10. WSAStartup(MAKEWORD(2, 2), &wsd);
  11. SOCKET m_SockClient;
  12. sockaddr_in clientaddr;
  13. clientaddr.sin_family = AF_INET;
  14. clientaddr.sin_port = htons(4600);
  15. //clientaddr.sin_addr.S_un.S_addr =inet_addr("127.0.0.1");
  16. m_SockClient = socket(AF_INET, SOCK_STREAM, 0);
  17. int i = connect(m_SockClient, (sockaddr*)&clientaddr, sizeof(clientaddr));
  18. cout << "connect" << i << endl;
  19. char buffer[1024];
  20. char inBuf[1024];
  21. int num;
  22. num = recv(m_SockClient, buffer, 1024, 0);
  23. if (num > 0) {
  24. cout << "receive from server" << buffer << endl;
  25. while (1) {
  26. num = 0;
  27. cin >> inBuf;
  28. if (!strcmp(inBuf, "exit")) {
  29. send(m_SockClient, inBuf, sizeof(inBuf), 0);
  30. return;
  31. }
  32. send(m_SockClient, inBuf, sizeof(inBuf), 0);
  33. num = recv(m_SockClient, buffer, 1024, 0);
  34. if (num >= 0)
  35. cout << "reveive from server" << buffer << endl;
  36. }
  37. }
  38. }

创作不易 觉得有帮助请点赞关注收藏

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/172172
推荐阅读
相关标签
  

闽ICP备14008679号