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版本不足要求\n"); 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)); //客戶端連接服務器