当前位置:   article > 正文

【C++】Winsock套接字编程,简单的socket通信代码(客户端、服务端)_用winsock的api套接字函数实现如下功能,服务器端接收客户端的连接

用winsock的api套接字函数实现如下功能,服务器端接收客户端的连接

上接前两章

操作系统:Windows10

开发环境:VS2015


服务端代码:

  1. // TCPserver.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <WinSock2.h>
  7. #include <WS2tcpip.h>
  8. #pragma comment(lib,"ws2_32.lib")
  9. using namespace std;
  10. #define SERVERIP "192.168.3.23"//IP地址
  11. #define SERVERPORT 5050//端口号
  12. int main()
  13. {
  14. WORD verision = MAKEWORD(2, 2);
  15. WSADATA lpData;
  16. int intEr = WSAStartup(verision, &lpData);//指定winsock版本并初始化
  17. if (intEr != 0)
  18. {
  19. cout << "winsock init failed!" << endl;
  20. return 0;
  21. }
  22. else
  23. cout << "winsock init success!" << endl;
  24. //创建侦听socket
  25. SOCKET listenScok = socket(AF_INET, SOCK_STREAM, 0);
  26. if (listenScok == INVALID_SOCKET)
  27. {
  28. cout << "socket error" << endl;
  29. return 0;
  30. }
  31. else
  32. cout << "create socket success" << endl;
  33. sockaddr_in hostAddr;
  34. hostAddr.sin_family = AF_INET;
  35. hostAddr.sin_port = htons(SERVERPORT);//转换成网络字节序
  36. //hostAddr.sin_addr.S_un.S_addr = inet_addr(SERVERIP);//转换成网络字节序
  37. //cout << "net IP:" << hostAddr.sin_addr.S_un.S_addr << endl;
  38. /*
  39. inet_addr()版本太低,被弃用使用inet_pton(协议族,字符串IP地址,void目标in_addr*)
  40. 头文件:WS2tcpip.h
  41. */
  42. in_addr addr;
  43. inet_pton(AF_INET, SERVERIP, (void*)&addr);
  44. hostAddr.sin_addr = addr;
  45. cout << "ip:" << addr.S_un.S_addr << endl;
  46. //侦听套接字listenSock绑定本机地址信息
  47. int err;
  48. err = bind(listenScok, (struct sockaddr*)&hostAddr, sizeof(sockaddr));
  49. if (err != 0)
  50. {
  51. cout << "hostAddr bind failed!" << endl;
  52. return 0;
  53. }
  54. err = listen(listenScok, 3);
  55. if (err != 0)
  56. {
  57. cout << "listen socket listen failed!" << endl;
  58. return 0;
  59. }
  60. cout << "listening..." << endl;
  61. int no = 1;
  62. while (true)
  63. {
  64. sockaddr_in clientAddr;
  65. int len = sizeof(struct sockaddr);//必须指定长度,否则会导致accept返回10014错误
  66. //accept会循环等待客户端连接
  67. SOCKET clientSock = accept(listenScok, (struct sockaddr*)&clientAddr, &len);
  68. if (clientSock == INVALID_SOCKET)
  69. {
  70. cout << "accept failed!" << endl;
  71. cout << WSAGetLastError() << endl;
  72. system("pause");
  73. return 0;
  74. }
  75. char buf[1024] = "\0";
  76. int buflen = recv(clientSock, buf, 1024, 0);
  77. if (buflen == SOCKET_ERROR)
  78. {
  79. cout << "recv failed!" << endl;
  80. return 0;
  81. }
  82. cout << "recieve data" << no++ << ": " << buf << endl;
  83. err = shutdown(clientSock, 2);
  84. if (err == SOCKET_ERROR)
  85. {
  86. cout << "shutdown failed!" << endl;
  87. return 0;
  88. }
  89. }
  90. err = shutdown(listenScok, 2);
  91. if (err == SOCKET_ERROR)
  92. {
  93. cout << "shutdown failed!" << endl;
  94. return 0;
  95. }
  96. err = closesocket(listenScok);
  97. if (err == SOCKET_ERROR)
  98. {
  99. cout << "closesocket failed!" << endl;
  100. return 0;
  101. }
  102. if (WSACleanup() != 0)
  103. {
  104. cout << "WSACleanup failed!" << endl;
  105. return 0;
  106. }
  107. system("pause");
  108. return 0;
  109. }



客户端代码:

  1. // TCPclient.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <WinSock2.h>
  7. #include <WS2tcpip.h>
  8. #pragma comment(lib,"ws2_32.lib")
  9. using namespace std;
  10. #define SERVERIP "192.168.3.23"
  11. #define SERVERPORT 5050
  12. int main()
  13. {
  14. WORD sockVersion = MAKEWORD(2, 2);
  15. WSADATA initData;
  16. int err = WSAStartup(sockVersion, &initData);
  17. if (err != 0)
  18. cout << "init Failed!" << endl;
  19. while (true)
  20. {
  21. //创建一个套接字,参数列表(地址族TCP、UDP;套接字协议类型TCP;套接字使用的特定协议0自动指定)
  22. SOCKET mysocket = socket(AF_INET, SOCK_STREAM, 0);
  23. if (mysocket == INVALID_SOCKET)
  24. {
  25. cout << "create socket failed!" << endl;
  26. return 0;
  27. }
  28. sockaddr_in hostAddr;
  29. hostAddr.sin_family = AF_INET;
  30. hostAddr.sin_port = htons(SERVERPORT);
  31. in_addr addr;
  32. inet_pton(AF_INET, SERVERIP, (void*)&addr);
  33. hostAddr.sin_addr = addr;
  34. cout << "ip:" << addr.S_un.S_addr << endl;
  35. SOCKET conSock = socket(AF_INET, SOCK_STREAM, 0);
  36. if (conSock == INVALID_SOCKET)
  37. {
  38. cout << "conSock failed" << endl;
  39. system("pause");
  40. return 0;
  41. }
  42. err = connect(conSock, (sockaddr*)&hostAddr, sizeof(sockaddr));
  43. if (err == INVALID_SOCKET)
  44. {
  45. cout << "connect failed!" << endl;
  46. system("pause");
  47. return 0;
  48. }
  49. char buf[1024] = "\0";
  50. cout << "input data: ";
  51. cin >> buf;
  52. err = send(conSock, buf, strlen(buf), 0);
  53. if (err == SOCKET_ERROR)
  54. {
  55. cout << "send failed!" << endl;
  56. system("pause");
  57. return 0;
  58. }
  59. if (closesocket(conSock) != 0)
  60. {
  61. cout << "closesocket failed!" << endl;
  62. system("pause");
  63. return 0;
  64. }
  65. }
  66. WSACleanup();
  67. system("pause");
  68. return 0;
  69. }



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

闽ICP备14008679号