通信_网络编程_WINDOWS下网络编程_SOCKET编程_VC中WINSOCK2.H头文件SOCKET

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} table.MsoTableGrid {mso-style-name:网格型; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; border:solid windowtext 1.0pt; mso-border-alt:solid windowtext .5pt; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-border-insideh:.5pt solid windowtext; mso-border-insidev:.5pt solid windowtext; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

通信 _ 网络编程 _WINDOWS 下网络编程 _SOCKET 编程 _VCWINSOCK2.H 头文件 SOCKET.doc

 

通信 网络编程 WINDOWS SOCKET 编程 VC WINSOCK2.H 头文件

 

Socket ,形象点可以把它理解为插槽,就像电源插座上面的插孔(不过电源插座上面的插孔有一孔的,还有两孔的,甚至还有三孔的,不知道还有没有四孔的,到目前为止我还没有看过有这么多孔的插座)。电器设备通过一条电源线,一端通过插头插入到能够输送电源的另一端的插座接入另一端,自己的一端也通过插头插入到自己的一个插孔上,将自己和能够输送电源的另一端建立一条物理连接。通过这样的一条物理连接,输送电源的另一端便可以将电源源源不断的输送过来,为电器设备提供电源,电器设备才可以借电力运转。

 

Socket 就类似这种情况,应用程序之间需要通信,就需要建立连接(这里的连接不是逻辑上的连接,而是物理上的连接,无线在这里也暂且把它归为是物理上的连接,只是连接的媒介不同而已)。现在假设连接的线有了,两端的节点也有了,并且连接的线两端也都套了一个能插入的插头,要实现通信,通信的应用程序还需要分别提供一个插槽以便将两头的插头都插入到插槽,这就是 SOCKET

 

以上只是举了些例子以便更形象的理解 Socket ,它只是逻辑上的概念, Sockets 也有专门的协议规范来标准化。在 rfc1928.txt 中,根据 OSI 模型, SOCKET 在概念上是一种位于应用层与传输层之间的中间层的网络传输协议。在实际实现上,例如在不同平台上的 Socket 的实现也有些差别。

 

WINDOWS 平台上, Socket 实现是在动态链接库 ws2_32.dll 中, VC 中的头文件 WINSOCK2.HWINSOCK.H 有定义。

 

查看头文件定义,在 WINSOCK2.H 头文件中, SOCKET 由一个简单的类型定义将 SOCKET 定义为一个 u_int 类型,而 u_int 类型其实就是一个 unsigned int 类型:

 

/*

  * The new type to be used in all

  * instances which refer to sockets.

  */

typedef u_int           SOCKET;

 

 

typedef unsigned int    u_int;

 

 

 

创建 SOCKET

#if INCL_WINSOCK_API_PROTOTYPES

WINSOCK_API_LINKAGE

SOCKET

WSAAPI

socket(

    int af,

    int type,

    int protocol

    );

#endif // INCL_WINSOCK_API_PROTOTYPES

 

INCL_WINSOCK_API_PROTOTYPES 定义

 

#ifndef INCL_WINSOCK_API_PROTOTYPES

#define INCL_WINSOCK_API_PROTOTYPES 1

#endif

 

#ifndef INCL_WINSOCK_API_TYPEDEFS

#define INCL_WINSOCK_API_TYPEDEFS 0

#endif

 

 

WINSOCK_API_LINKAGE 定义

 

#ifndef WINSOCK_API_LINKAGE

#ifdef DECLSPEC_IMPORT

#define WINSOCK_API_LINKAGE DECLSPEC_IMPORT

#else

#define WINSOCK_API_LINKAGE

#endif

#endif

 

DECLSPEC_IMPORT 定义(在 WINNT.H 头文件中)

#if (defined(_M_MRX000) || defined(_M_IX86) || defined(_M_ALPHA) || defined(_M_PPC) || defined(_M_IA64)) && !defined(MIDL_PASS)

#define DECLSPEC_IMPORT __declspec(dllimport)

#else

#define DECLSPEC_IMPORT

#endif

 

WSAAPI 定义

#ifdef WIN32

 

#define WSAAPI                  FAR PASCAL

。。。

。。。

#else /* WIN16 */

 

#define WSAAPI                  FAR PASCAL

。。。

。。。

#endif  /* WIN32 */

 

创建 SOCKET 函数 socket 返回创建的 SOCKET ,类型如上定义。此函数传入三个参数:

 

int af,

int type,

int protocol

 

第一个参数 af 传入的是地址家族( address family, 也就是地址簇或者协议簇。

该参数在不同的系统下参数不一定相同,另外注意不同的版本可以使用的参数也不相同,可以参考相关文档。这里列出了所有要求的参数可用值。

 

参数 a. 地址族(与 TCP/IP 协议下的协议族等价)可以使用的参数如下

#define AF_UNIX         1               /* local to host (pipes, portals) */

#define AF_INET           2                 /* internetwork: UDP, TCP, etc. */

#define AF_IMPLINK      3               /* arpanet imp addresses */

#define AF_PUP          4               /* pup protocols: e.g. BSP */

#define AF_CHAOS        5               /* mit CHAOS protocols */

#define AF_NS           6               /* XEROX NS protocols */

#define AF_IPX          AF_NS           /* IPX protocols: IPX, SPX, etc. */

#define AF_ISO          7               /* ISO protocols */

#define AF_OSI          AF_ISO          /* OSI is ISO */

#define AF_ECMA         8               /* european computer manufacturers */

#define AF_DATAKIT      9               /* datakit protocols */

#define AF_CCITT        10              /* CCITT protocols, X.25 etc */

#define AF_SNA          11              /* IBM SNA */

#define AF_DECnet       12               /* DECnet */

#define AF_DLI          13              /* Direct data link interface */

#define AF_LAT          14              /* LAT */

#define AF_HYLINK       15              /* NSC Hyperchannel */

#define AF_APPLETALK    16              /* AppleTalk */

#define AF_NETBIOS      17              /* NetBios-style addresses */

#define AF_VOICEVIEW    18              /* VoiceView */

#define AF_FIREFOX      19              /* Protocols from Firefox */

#define AF_UNKNOWN1     20              /* Somebody is using this! */

#define AF_BAN          21              /* Banyan */

#define AF_ATM          22              /* Native ATM Services */

#define AF_INET6        23              /* Internetwork Version 6 */

#define AF_CLUSTER      24              /* Microsoft Wolfpack */

#define AF_12844        25              /* IEEE 1284.4 WG AF */

#define AF_MAX          26

AF_UNIX // 表示 Unix 内部协议

AF_NS // 表示使用的是 Xerox NS 协议族

AF_IMPLINK// 表示 IMP 连接层

另外 AF_LOCAL 是用于 Unix/Linux 系统中本机进程间通信

 

 

第二个参数传入的是 SOCKET 类型,可传入的 SOCKET 类型如下:

 

参数 b.Socket 类型

可以取如下的一些值:

SOCK_STREAM 流套接字

SOCK_DGRAM 数据报套接字

SOCK_RAW 未加工套接字(可以用它来接收原始的数据包,即不经过传输层的,常用来抓包)

SOCK_SEQPACKET 顺序包套接字

#define SOCK_STREAM     1               /* stream socket */

#define SOCK_DGRAM      2               /* datagram socket */

#define SOCK_RAW        3               /* raw-protocol interface */

#define SOCK_RDM        4               /* reliably-delivered message */

#define SOCK_SEQPACKET  5               /* sequenced packet stream */

 

第三个参数传入的是协议类型,可传入的协议类型如下:

 

参数 c.Socket 使用的协议类型

通常将此设为 0 IPPROTO_IP ,是因为协议类型可以根据 Socket 的类型来确定,比如 Sock_STREAM 就是使用 TCP 协议,而 SOCK_DGRAM 就是使用 UDP 协议。

其他的类型还有 :

#define IPPROTO_ICMP            1               /* control message protocol */

#define IPPROTO_IGMP            2               /* internet group management protocol */

#define IPPROTO_GGP             3               /* gateway^2 (deprecated) */

#define IPPROTO_TCP             6               /* tcp */

#define IPPROTO_PUP             12              /* pup */

#define IPPROTO_UDP             17              /* user datagram protocol */

#define IPPROTO_IDP             22              /* xns idp */

#define IPPROTO_ND              77              /* UNOFFICIAL net disk proto */

#define IPPROTO_RAW             255             /* raw IP packet */

#define IPPROTO_MAX             256

 

 

有关 SOCKET 资料可参考:

维基百科 http://zh.wikipedia.org/wiki/SOCKS

IETF RFC 1928, rfc1928.txt, SOCKS Protocol Version 5

IETF RFC 1929, rfc1929.txt, Username/Password Authentication for SOCKS V5

各平台( LinuxUnixWindows 等)对 SOCKET 的实现

互联网传输协议的性能优化: http://shop.zte.com.cn/main/include/showemagazinearticle.jsp?articleId=369&catalogId=12165

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值