Windows Socket 编程, WIN32_LEAN_AND_MEAN 的用法
来源:互联网 发布:spss clementine mac 编辑:程序博客网 时间:2024/06/05 18:14
一、基本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 的用法
- windows的SOCKET编程
- windows socket编程:select 用法 例子
- windows socket编程:select 用法 例子
- #define WIN32_LEAN_AND_MEAN 的作用
- WIN32_LEAN_AND_MEAN 的作用
- #define WIN32_LEAN_AND_MEAN 的作用
- WIN32_LEAN_AND_MEAN 的作用
- #define WIN32_LEAN_AND_MEAN 的作用
- #define WIN32_LEAN_AND_MEAN 的作用
- WIN32_LEAN_AND_MEAN
- WIN32_LEAN_AND_MEAN
- WIN32_LEAN_AND_MEAN
- WIN32_LEAN_AND_MEAN
- Windows Socket编程的基本思路
- Windows 下的socket编程
- 基于Windows的Socket编程
- windows socket编程和linux socket编程的异同
- msysgit hooks using lua
- 杂文:这两年来
- 程序员要走的路
- 四盘汉诺塔的与或图
- 获取通讯录所有字段
- Windows Socket 编程, WIN32_LEAN_AND_MEAN 的用法
- Java 基础一些代码练习笔记(LinkedList)
- iphone-common-codes-ccteam源代码 CCMapAnnotation.m
- 2011, 一个实习生的自白
- iphone-common-codes-ccteam源代码 CCMemory.h
- iphone-common-codes-ccteam源代码 CCMemory.m
- iphone-common-codes-ccteam源代码 CCNSArray.h
- 2318
- 第六章堆排序之“保持堆的性质MAX-HEAPIFY”(递归版)