飞道的博客

项目(百万并发网络通信架构)01---Windows下Tcp Socket的基本结构(客户端、服务端)

244人阅读  评论(0)

一、服务端代码


  
  1. #define WIN32_LEAN_AND_MEAN
  2. #define _WINSOCK_DEPRECATED_NO_WARNINGS //for inet_pton()
  3. #include <windows.h>
  4. #include <WinSock2.h>
  5. #include <iostream>
  6. #pragma comment(lib, "ws2_32.lib")
  7. using namespace std;
  8. int main()
  9. {
  10. WORD ver = MAKEWORD( 2, 2);
  11. WSADATA dat;
  12. WSAStartup(ver, &dat);
  13. //建立socket
  14. SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  15. if (INVALID_SOCKET == _sock){
  16. std:: cout << "ERROR:建立socket失败!" << std:: endl;
  17. }
  18. else {
  19. std:: cout << "建立socket成功!" << std:: endl;
  20. }
  21. //初始化服务端地址
  22. struct sockaddr_in _sin = {};
  23. _sin.sin_addr.S_un.S_addr = inet_addr( "192.168.0.104");
  24. //_sin.sin_addr.S_un.S_addr = INADDR_ANY;
  25. _sin.sin_family = AF_INET;
  26. _sin.sin_port = htons( 4567);
  27. //绑定服务端地址
  28. if (SOCKET_ERROR == bind(_sock, (struct sockaddr*)&_sin, sizeof(_sin))){
  29. std:: cout << "ERROR:绑定地址失败!" << std:: endl;
  30. }
  31. else {
  32. std:: cout << "绑定地址成功!" << std:: endl;
  33. }
  34. //监听网络端口
  35. if (SOCKET_ERROR == listen(_sock, 5)) {
  36. std:: cout << "ERROR:监听网络端口失败!" << std:: endl;
  37. }
  38. else {
  39. std:: cout << "监听网络端口成功!" << std:: endl;
  40. }
  41. //用来保存客户端地址
  42. struct sockaddr_in _clientAddr = {};
  43. int nAddrLen = sizeof(_clientAddr);
  44. SOCKET _cSock = INVALID_SOCKET;
  45. char msgBuf[] = "Hello, I'm Server.";
  46. //接收客户端连接
  47. _cSock = accept(_sock, (struct sockaddr*)&_clientAddr, &nAddrLen);
  48. if (INVALID_SOCKET == _cSock) {
  49. std:: cout << "ERROR:接收到无效客户端!" << std:: endl;
  50. }
  51. else {
  52. std:: cout << "接受到新的客户端连接,IP=" << inet_ntoa(_clientAddr.sin_addr)
  53. << ",Socket=" << static_cast< int>(_cSock) << std:: endl;
  54. }
  55. char recvBuf[ 1024] = {};
  56. //循环处理客户端的数据
  57. while ( true)
  58. {
  59. //接受数据
  60. int _nLen = recv(_cSock, recvBuf, 1024, 0);
  61. if (_nLen <= 0) {
  62. std:: cout << "客户端已退出" << std:: endl;
  63. break;
  64. }
  65. std:: cout << "收到命令" << recvBuf << std:: endl;
  66. if ( 0 == strcmp(recvBuf, "getName")) {
  67. char msgBuf[] = "dongshao";
  68. send(_cSock, msgBuf, strlen(msgBuf) + 1, 0);
  69. }
  70. else if ( 0 == strcmp(recvBuf, "getAge")) {
  71. char msgBuf[] = "21";
  72. send(_cSock, msgBuf, strlen(msgBuf) + 1, 0);
  73. }
  74. else {
  75. char msgBuf[] = "命令不正确";
  76. send(_cSock, msgBuf, strlen(msgBuf) + 1, 0);
  77. }
  78. }
  79. //关闭服务端套接字
  80. closesocket(_sock);
  81. WSACleanup();
  82. std:: cout << "服务端停止工作!" << std:: endl;
  83. getchar(); //防止程序一闪而过
  84. return 0;
  85. }

二、客户端代码


  
  1. #define WIN32_LEAN_AND_MEAN
  2. #define _WINSOCK_DEPRECATED_NO_WARNINGS //for inet_pton()
  3. #include <windows.h>
  4. #include <WinSock2.h>
  5. #include <iostream>
  6. #pragma comment(lib, "ws2_32.lib")
  7. using namespace std;
  8. int main()
  9. {
  10. WORD ver = MAKEWORD( 2, 2);
  11. WSADATA dat;
  12. WSAStartup(ver, &dat);
  13. //建立socket
  14. SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  15. if (INVALID_SOCKET == _sock) {
  16. std:: cout << "ERROR:建立socket失败!" << std:: endl;
  17. }
  18. else {
  19. std:: cout << "建立socket成功!" << std:: endl;
  20. }
  21. //声明要连接的服务端地址
  22. struct sockaddr_in _sin = {};
  23. _sin.sin_addr.S_un.S_addr = inet_addr( "192.168.0.104");
  24. _sin.sin_family = AF_INET;
  25. _sin.sin_port = htons( 4567);
  26. //连接服务端
  27. int ret = connect(_sock, (struct sockaddr*)&_sin, sizeof(_sin));
  28. if (SOCKET_ERROR == ret) {
  29. std:: cout << "ERROR:连接服务端失败!" << std:: endl;
  30. }
  31. else {
  32. std:: cout << "连接服务端成功!" << std:: endl;
  33. }
  34. char cmdBuf[ 256] = {};
  35. while ( true)
  36. {
  37. //输入数据
  38. std:: cout << "cmd#:";
  39. std:: cin >> cmdBuf;
  40. //如果输入的是exit,退出程序
  41. if ( 0 == strcmp(cmdBuf, "exit")) {
  42. break;
  43. }
  44. //向服务端发送自己的命令
  45. int _nLen = send(_sock, cmdBuf, strlen(cmdBuf) + 1, 0);
  46. if (_nLen == 0) {
  47. std:: cout << "输入有误,重新发送" << std:: endl;
  48. continue;
  49. }
  50. else {
  51. char recvBuf[ 1024] = {};
  52. _nLen = recv(_sock, recvBuf, sizeof(recvBuf), 0);
  53. if (_nLen <= 0) {
  54. std:: cout << "命令接收错误" << std:: endl;
  55. continue;
  56. }
  57. else {
  58. std:: cout << recvBuf << std:: endl;
  59. }
  60. }
  61. }
  62. //关闭服务端套接字
  63. closesocket(_sock);
  64. WSACleanup();
  65. std:: cout << "服务端停止工作!" << std:: endl;
  66. getchar(); //防止程序一闪而过
  67. return 0;
  68. }

三、演示效果

  • 双击服务端程序

  • 双击客户端程序,可以看到服务端成功接收到请求,客户端等待命令输入

  • 客户端输入信息到服务端,并交互数据


转载:https://blog.csdn.net/qq_41453285/article/details/105281284
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场