window_c++_socket编程_winsock2.h

1.初始化动态链接库

WSAStartup: The WSAStartup function initiates use of the Winsock DLL by a process.

WSAStartup函数使用一个进程来初始化Winsock动态链接库ws2_32.dll。

int WSAAPI WSAStartup(
  WORD      wVersionRequested,  //用于协商winsock dll的最高版本
  LPWSADATA lpWSAData  
  //指向WSADATA数据结构的指针,该结构用于接收Windows套接字实现的详细信息 );

初始化成功返回0,否则返回错误码。

WSAStartup函数必须是应用程序或DLL调用的第一个Windows套接字函数。它允许应用程序或DLL指定所需的Windows套接字版本,并检索特定Windows套接字实现的详细信息。应用程序或DLL只能在成功调用WSAStartup后再发出Windows套接字函数。

int SocketInit()
{
    WORD wVersionRequested; //用 2 bytes 的字来表示
    WSADATA wsaData;
    int err;
    wVersionRequested = MAKEWORD(2, 2);  //DLL版本
    err = WSAStartup(wVersionRequested, &wsaData);
    if (err != 0)
    {
        printf("WinSock DLL版本不足要求
");
        return 0;
    }
    if (LOBYTE(wsaData.wVersion) != 2 ||
        HIBYTE(wsaData.wVersion) != 2)
    {
        WSACleanup();
        return 0;
    }
    return 1;
}

其中WORD类型是微软SDK中的类型,WORD的意思为字,是2byte的无符号整数,表示范围0~65535.

2.创建socket

socket:The socket function creates a socket that is bound to a specific transport service provider.

SOCKET WSAAPI socket(
  int af, //地址族规范。地址族的可能值在Winsock2.h头文件中定义。
  int type,
  int protocol
);

af:

在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),因此可以使用任意一个常量。

type:

新套接字的类型规范。

套接字类型的可能值在Winsock2.h头文件中定义。

SOCK_STREAM  1  

A socket type that provides sequenced, reliable, two-way, connection-based byte streams with an OOB data transmission mechanism. This socket type uses the Transmission Control Protocol (TCP) for the Internet address family (AF_INET or AF_INET6).

SOCK_DGRAM2  2

A socket type that supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. This socket type uses the User Datagram Protocol (UDP) for the Internet address family (AF_INET or AF_INET6).

......

protocol: 协议类型。

返回值:If no error occurs, socket returns a descriptor referencing the new socket. Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.

3.SOCKADDR_IN

typedef struct sockaddr_in {
  short          sin_family;
  u_short        sin_port;
  struct in_addr sin_addr;
  char           sin_zero[8];
} SOCKADDR_IN, *PSOCKADDR_IN, *LPSOCKADDR_IN;

4.connect

The connect function establishes a connection to a specified socket.

int WSAAPI connect(
  SOCKET         s,  //A descriptor identifying an unconnected socket.
  const sockaddr *name,  //A pointer to the sockaddr structure to which the connection should be established.
  int            namelen //The length, in bytes, of the sockaddr structure pointed to by the name parameter.
);
if (SOCKET_ERROR == SocketInit())
        return -1;

SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
//addrSrv.sin_addr.S_un.S_addr = inet_pton(SeverIp);
inet_pton(AF_INET, SeverIp, &addrSrv.sin_addr); //设置addrSrv的ip
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6666);  
//设置端口号,convert an IP port number in host byte order to the IP port number in network byte order connect(sockClient, (SOCKADDR
*)&addrSrv, sizeof(SOCKADDR)); //客户端连接服务器
原文地址:https://www.cnblogs.com/jasonlixuetao/p/10482088.html