WinSock network programming BASICS (1) and winsock network programming Basics

Source: Internet
Author: User

WinSock network programming BASICS (1) and winsock network programming Basics

Record the problems encountered during windows Network Programming and related notes

 

Basic concepts:

 

Socket:

Socket originated from UNIX. Socket is an intermediate software abstraction layer for communications between the application layer and the TCP/IP protocol family. It is a set of interfaces. Based on the "open-read/write-Close" mode, WinSock is one of the variants.

 

The following is a typical overview of using Socket interfaces in C/S:

 

WinSock:

WinSock is a network programming interface on the Windows platform. It is developed based on BSD Socket in Unix and is a programming interface unrelated to the network protocol.

Currently, WinSock has two versions:

 

Winsock 1: supported by Windows CE platform; header file: WinSock. h; lib Library: wsock32.lib

 

Winsock 2: not supported by some platforms. The prefix WSA can be used to distinguish it from Winsock 1. Some functions, such as WSAStartup, WSACleanup, WSARecvEx, and WSAGetLastError, all belong to the Winsock 1.1 Standard functions. header file: WinSock2.h; lib Library: ws2_32.lib

 

Initialize WinSock: All programs that need to call the WinSock function must initialize WinSock and ensure that winsock supports this system.

#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(){WSADATA wsaData;int iResult;iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);if (iResult != 0){printf("WSAStartup failed: %d\n", iResult);return 1;}WSACleanup();return 0;}

 

The WSAStartup () function uses the appropriate winsock dynamic link library. If the connection succeeds, 0 is returned.

int WSAStartup(  _In_  WORD      wVersionRequested,  _Out_ LPWSADATA lpWSAData);

 

WVersionRequested specifies the Winsock version you want to load. The high byte specifies the secondary version number, while the low byte specifies the primary version number. You can use macro MAKEWORD (x, y) to specify the version number. Here, x represents the main version, and y represents the next version. LpWSAData is a pointer to the WSAData structure. WSAStartup will fill the structure with the Winsock dynamic link library it loads

 

WSADATA structure:

typedef struct WSAData {  WORD           wVersion;  WORD           wHighVersion;  char           szDescription[WSADESCRIPTION_LEN+1];  char           szSystemStatus[WSASYS_STATUS_LEN+1];  unsigned short iMaxSockets;  unsigned short iMaxUdpDg;  char FAR       *lpVendorInfo;} WSADATA, *LPWSADATA;

WVersion indicates the Winsock version you will use. wHighVersion indicates the maximum version supported by the loaded Winsock dynamic library. Its high bytes represent the next version, and the low bytes represent the main version.

 

SzDescription and szSystemStatus are set by Winsock of a specific version.

 

IMaxSockets indicates the maximum number of concurrent Sockets. Its value depends on available hardware resources.


IMaxUdpDg indicates the maximum length of a datagram. However, to obtain the maximum length of a datagram, you must use WSAEnumProtocols to query the protocol.


LpVendorInfo is the manufacturer information retained for Winsock implementation, which is of no use on Windows platform.

 

Windows 95 and later versions support Winsock 2.2.

 

WSACleanup (): releases the occupied resources. If this function fails to be called, the Operating System Automatically releases it. Each WSAStartup call should have a WSACleanup call.

Link: http://www.bugcoding.com/entry/9

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.