#pragma comment(lib, "ws2_32.lib ") using namespace std;DWORD WINAPI ClientThread(LPVOID sockCli_windows socket= socket(af_inet, sock_stream, ipproto_tcp);">
当前位置:   article > 正文

windows之socket编程示例_windows socket= socket(af_inet, sock_stream, ippro

windows socket= socket(af_inet, sock_stream, ipproto_tcp);

1. tcp服务器端

  1. #include "stdafx.h"
  2. #include <WINSOCK2.H>
  3. #include <windows.h>
  4. #include <iostream>
  5. #include <string>
  6. #pragma comment(lib, "ws2_32.lib ")
  7. using namespace std;
  8. DWORD WINAPI ClientThread(LPVOID sockCliet);
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11. // 0 --- 加载套接字库
  12. WSAData wsaData;
  13. WSAStartup(MAKEWORD(2,2), &wsaData);
  14. // 1 --- 创建套接字
  15. SOCKET serSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  16. // 2 --- 绑定套接字
  17. SOCKADDR_IN addrService;
  18. addrService.sin_family = AF_INET;
  19. addrService.sin_addr.s_addr = inet_addr("127.0.0.1");
  20. addrService.sin_port = htons(6000);
  21. bind(serSock, (sockaddr *)&addrService, sizeof(addrService));
  22. // 3 --- 指定最大同时连接数,开始监听
  23. listen(serSock, 10);
  24. // 4 --- 接受新的连接,创建线程处理本次连接
  25. while (1)
  26. {
  27. SOCKADDR_IN addrClient;
  28. int n = sizeof(addrClien
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/147194
推荐阅读
相关标签
  

闽ICP备14008679号