Windows Socket 编程, WIN32_LEAN_AND_MEAN 的用法

2024-03-14 01:08

本文主要是介绍Windows Socket 编程, WIN32_LEAN_AND_MEAN 的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、基本Socket 调用

原文地址:Getting Started with Winsock

注意关于windows.h 与 winsock2.h 一起使用时的问题:

The Winsock2.h header file internally includes core elements from theWindows.h header file, so there is not usually an #include line for theWindows.h header file in Winsock applications. If an #include line is needed for theWindows.h header file, this should be preceded with the #define WIN32_LEAN_AND_MEAN macro. For historical reasons, theWindows.h header defaults to including theWinsock.h header file for Windows Sockets 1.1. The declarations in theWinsock.h header file will conflict with the declarations in theWinsock2.h header file required by Windows Sockets 2.0. The WIN32_LEAN_AND_MEAN macro prevents theWinsock.h from being included by theWindows.h header. An example illustrating this is shown below.

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>#pragma comment(lib, "Ws2_32.lib")int main() {return 0;
}

二、高级调用及线程管理

1. 多线程

原文地址:C++ Socket Class for Windows 

A simple echo server:

#include "Socket.h"
#include <process.h>
#include <string>unsigned __stdcall Answer(void* a) {Socket* s = (Socket*) a;while (1) {std::string r = s->ReceiveLine();if (r.empty()) break;s->SendLine(r);}delete s;return 0;
}int main(int argc, char* argv[]) {SocketServer in(2000,5);while (1) {Socket* s=in.Accept();unsigned ret;_beginthreadex(0,0,Answer,(void*) s,0,&ret);}return 0;
}

2. 从已建立连接的socket 取得对方IP 地址

	SOCKET ClientSocket = accept(ListenSocket, NULL, NULL);sockaddr_in remoteName;int  remNameLen=sizeof(remoteName);memset((void *)& remoteName, 0, remNameLen);int iResult = getpeername(ClientSocket, (sockaddr *) & remoteName,& remNameLen);printf("Remote IP: %s\n", inet_ntoa(remoteName.sin_addr));





这篇关于Windows Socket 编程, WIN32_LEAN_AND_MEAN 的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/806806

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

Windows设置nginx启动端口的方法

《Windows设置nginx启动端口的方法》在服务器配置与开发过程中,nginx作为一款高效的HTTP和反向代理服务器,被广泛应用,而在Windows系统中,合理设置nginx的启动端口,是确保其正... 目录一、为什么要设置 nginx 启动端口二、设置步骤三、常见问题及解决一、为什么要设置 nginx

C#多线程编程中导致死锁的常见陷阱和避免方法

《C#多线程编程中导致死锁的常见陷阱和避免方法》在C#多线程编程中,死锁(Deadlock)是一种常见的、令人头疼的错误,死锁通常发生在多个线程试图获取多个资源的锁时,导致相互等待对方释放资源,最终形... 目录引言1. 什么是死锁?死锁的典型条件:2. 导致死锁的常见原因2.1 锁的顺序问题错误示例:不同

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

在 Windows 上安装 DeepSeek 的完整指南(最新推荐)

《在Windows上安装DeepSeek的完整指南(最新推荐)》在Windows上安装DeepSeek的完整指南,包括下载和安装Ollama、下载DeepSeekRXNUMX模型、运行Deep... 目录在www.chinasem.cn Windows 上安装 DeepSeek 的完整指南步骤 1:下载并安装

PyCharm接入DeepSeek实现AI编程的操作流程

《PyCharm接入DeepSeek实现AI编程的操作流程》DeepSeek是一家专注于人工智能技术研发的公司,致力于开发高性能、低成本的AI模型,接下来,我们把DeepSeek接入到PyCharm中... 目录引言效果演示创建API key在PyCharm中下载Continue插件配置Continue引言

windows系统下shutdown重启关机命令超详细教程

《windows系统下shutdown重启关机命令超详细教程》shutdown命令是一个强大的工具,允许你通过命令行快速完成关机、重启或注销操作,本文将为你详细解析shutdown命令的使用方法,并提... 目录一、shutdown 命令简介二、shutdown 命令的基本用法三、远程关机与重启四、实际应用