socket编程:socket()函数详解 您所在的位置:网站首页 小品剧本四人简单搞笑幽默六一 socket编程:socket()函数详解

socket编程:socket()函数详解

2023-09-03 08:39| 来源: 网络整理| 查看: 265

文章目录 1. 什么是socket?1.1 意义1.2 本质1.3 应用1.4 结论 2. socket() 函数参数介绍2.1 参数1:int af2.2 参数2:int type2.3 参数3:int protocol 3. socket()函数返回值介绍

1. 什么是socket?

套接字函数创建一个绑定到特定传输服务提供者的套接字。

将底层复杂的协议体系,执行流程,进行了封装,封装完的结果,就是一个SOCKET了,也就是说,SOCKET是我们调用协议进行通信的操作接口。

1.1 意义

将复杂的协议过程与我们编程人员分开,我们直接操作一个简单SOCKET就行了,对于底层的协议过程细节,完全不用知道,这就大大方便了我们。

网络编程难就难在协议本身的复杂性,简单就简单在我们编程层面完全不用考虑那些。

1.2 本质

本质就是一种数据类型,转定义下看类型

就是一个整数(unsigned int)

image-20210418120441491image-20210418120451792image-20210418120532046

但是这个数是唯一的

标识着我们当前的应用程序,协议特点等信息ID,门牌号 1.3 应用

我们网络通信的函数,全部都要使用SOCKET

逻辑:

每个客户端有一个SOCKET,服务器有一个SOCKET,通信时候,就需要这个SOCKET做参数,给谁通信,就要传递谁的SOCKET。

1.4 结论

网络编程,理论层面SOCKET是网络封装的精华,代码层面就是不停的使用SOCKET这个变量,所以又叫SOCKET编程。

2. socket() 函数参数介绍 SOCKET WSAAPI socket( int af, //地址族规范。 地址族的可能值在Winsock2.h头文件中定义。 int type,//新套接字的类型规范。 int protocol//要使用的协议。 );

SOCKET socketServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

2.1 参数1:int af

地址族规范。 地址族的可能值在Winsock2.h头文件中定义。

在为Windows Vista和更高版本发布的Windows SDK上,头文件的组织已更改,并且地址族的可能值在Ws2def.h头文件中定义。 请注意,Ws2def.h头文件自动包含在Winsock2.h中,并且绝对不能直接使用。

当前支持的值为AF_INET或AF_INET6,这是IPv4和IPv6的Internet地址族格式。 如果安装了地址族的Windows套接字服务提供程序,则支持地址族的其他选项(例如,与NetBIOS一起使用的AF_NETBIOS)。 请注意,AF_地址族和PF_协议族常量的值是相同的(例如,AF_INET和PF_INET),因此可以使用任何一个常量。

下表列出了地址系列的常用值,尽管许多其他值也是可能的。

AfMeaningAF_UNSPEC 0地址族未指定。AF_INET 2Internet协议版本4(IPv4)地址族。AF_IPX 6IPX / SPX地址族。 仅当安装了NWLink IPX / SPX NetBIOS兼容传输协议时,才支持此地址系列。WindowsVista和更高版本不支持此地址系列。AF_APPLETALK 16AppleTalk地址族。 仅当安装了AppleTalk协议时才支持此地址系列。WindowsVista和更高版本不支持此地址系列。AF_NETBIOS 17NetBIOS地址族。 仅当安装了用于NetBIOS的Windows套接字提供程序时,才支持此地址系列。在32位版本的Windows上支持用于NetBIOS的Windows套接字提供程序。 默认情况下,此提供程序安装在32位版本的Windows上.NetBIOS的Windows套接字提供程序在64位版本的Windows(包括Windows 7,Windows Server 2008,Windows Vista,Windows Server 2003或Windows XP)上不受支持。 用于NetBIOS的Windows套接字提供程序仅支持将* type 参数设置为* SOCK_DGRAM **的套接字。用于NetBIOS的Windows套接字提供程序与[NetBIOS]不直接相关(https://docs.microsoft.com/en -us / previous-versions / windows / desktop / netbios / portal)编程界面。 Windows Vista,Windows Server 2008和更高版本不支持NetBIOS编程接口。AF_INET6 23Internet协议版本6(IPv6)地址族。AF_IRDA 26红外数据协会(IrDA)地址族。仅当计算机安装了红外端口和驱动程序时,才支持此地址族。AF_BTH 32蓝牙地址系列。如果计算机安装了蓝牙适配器和驱动程序,则Windows XP SP2或更高版本支持此地址系列。 2.2 参数2:int type

新套接字的类型规范。 套接字类型的可能值在Winsock2.h头文件中定义。 下表列出了Windows套接字2支持的type参数的可能值:

TypeMeaningSOCK_STREAM 1一种套接字类型,可通过OOB数据传输机制提供顺序的,可靠的,双向的,基于连接的字节流。 此套接字类型将传输控制协议(TCP)用于Internet地址系列(AF_INET或AF_INET6)。SOCK_DGRAM2一种支持数据报的套接字类型,这些数据报是无连接的,不可靠的最大长度固定(通常很小)的缓冲区。 此套接字类型对Internet地址系列(AF_INET或AF_INET6)使用用户数据报协议(UDP)。SOCK_RAW3提供原始套接字的套接字类型,该套接字允许应用程序操作下一个上层协议标头。 要操作IPv4标头,必须在套接字上设置IP_HDRINCL套接字选项。 要操作IPv6标头,必须在套接字上设置IPV6_HDRINCL套接字选项。SOCK_RDM4提供可靠消息数据报的套接字类型。 这种类型的一个示例是Windows中的实用通用多播(PGM)多播协议实现,通常称为可靠多播编程。仅当安装了可靠多播协议时才支持此* type *值。SOCK_SEQPACKET5一种套接字类型,可根据数据报提供伪流数据包。

在Windows套接字2中,引入了新的套接字类型。 应用程序可以通过WSAEnumProtocols函数动态发现每个可用传输协议的属性。 因此,应用程序可以确定地址族的可能套接字类型和协议选项,并在指定此参数时使用此信息。 随着新套接字类型,地址族和协议的定义,Winsock2.h和Ws2def.h头文件中的套接字类型定义将定期更新。

在Windows套接字1.1中,唯一可能的套接字类型是SOCK_DGRAM和SOCK_STREAM。

2.3 参数3:int protocol

要使用的协议。 protocol参数的可能选项特定于指定的地址族和套接字类型。 在Winsock2.h和Wsrm.h头文件中定义了协议的可能值。

在为Windows Vista和更高版本发布的Windows SDK上,头文件的组织已更改,并且此参数可以是Ws2def.h头文件中定义的IPPROTO枚举类型的值之一。 请注意,Ws2def.h头文件自动包含在Winsock2.h中,并且绝对不能直接使用。

如果指定的值为0,则调用者不希望指定协议,服务提供商将选择要使用的协议。

当af参数为AF_INET或AF_INET6且类型为SOCK_RAW时,在IPv6或IPv4数据包头的协议字段中设置为协议指定的值。

下表列出了该协议的通用值,尽管许多其他值也是可能的。

protocolMeaningIPPROTO_ICMP1The Internet Control Message Protocol (ICMP). This is a possible value when the af parameter is AF_UNSPEC, AF_INET, or AF_INET6 and the type parameter is SOCK_RAW or unspecified.This protocol value is supported on Windows XP and later.IPPROTO_IGMP2The Internet Group Management Protocol (IGMP). This is a possible value when the af parameter is AF_UNSPEC, AF_INET, or AF_INET6 and the type parameter is SOCK_RAW or unspecified.This protocol value is supported on Windows XP and later.BTHPROTO_RFCOMM3The Bluetooth Radio Frequency Communications (Bluetooth RFCOMM) protocol. This is a possible value when the af parameter is AF_BTH and the type parameter is SOCK_STREAM.This protocol value is supported on Windows XP with SP2 or later.IPPROTO_TCP6The Transmission Control Protocol (TCP). This is a possible value when the af parameter is AF_INET or AF_INET6 and the type parameter is SOCK_STREAM.IPPROTO_UDP17The User Datagram Protocol (UDP). This is a possible value when the af parameter is AF_INET or AF_INET6 and the type parameter is SOCK_DGRAM.IPPROTO_ICMPV658The Internet Control Message Protocol Version 6 (ICMPv6). This is a possible value when the af parameter is AF_UNSPEC, AF_INET, or AF_INET6 and the type parameter is SOCK_RAW or unspecified.This protocol value is supported on Windows XP and later.IPPROTO_RM113The PGM protocol for reliable multicast. This is a possible value when the af parameter is AF_INET and the type parameter is SOCK_RDM. On the Windows SDK released for Windows Vista and later, this protocol is also called IPPROTO_PGM.This protocol value is only supported if the Reliable Multicast Protocol is installed. 3. socket()函数返回值介绍 SOCKET socketServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (INVALID_SOCKET == socketServer) { int a = WSAGetLastError(); printf("socket function failed with error = %d\n", a); //清理网络库 WSACleanup(); return 1; } closesocket(socketServer);

如果没有发生错误,socket将返回一个引用新socket的描述符。否则,将返回INVALID_SOCKET 的值,并且可以通过调用WSAGetLastError检索特定的错误代码。

Error codeMeaningWSANOTINITIALISEDA successful WSAStartup call must occur before using this function.WSAENETDOWNThe network subsystem or the associated service provider has failed.WSAEAFNOSUPPORTThe specified address family is not supported. For example, an application tried to create a socket for the AF_IRDA address family but an infrared adapter and device driver is not installed on the local computer.WSAEINPROGRESSA blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.WSAEMFILENo more socket descriptors are available.WSAEINVALAn invalid argument was supplied. This error is returned if the af parameter is set to AF_UNSPEC and the type and protocol parameter are unspecified.WSAEINVALIDPROVIDERThe service provider returned a version other than 2.2.WSAEINVALIDPROCTABLEThe service provider returned an invalid or incomplete procedure table to the WSPStartup.WSAENOBUFSNo buffer space is available. The socket cannot be created.WSAEPROTONOSUPPORTThe specified protocol is not supported.WSAEPROTOTYPEThe specified protocol is the wrong type for this socket.WSAEPROVIDERFAILEDINITThe service provider failed to initialize. This error is returned if a layered service provider (LSP) or namespace provider was improperly installed or the provider fails to operate correctly.WSAESOCKTNOSUPPORTThe specified socket type is not supported in this address family.


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有