Windows Network Programming

Source: Internet
Author: User
ArticleDirectory
    • I. Winsock Initialization
    • Ii. IPv4 struct
    • 3. Create a socket
    • Iv. Functions and steps to be called by the server
    • 5. Functions and steps to be called by the client
I. Winsock Initialization

Each WinsockProgramThe appropriate Winsock DLL versions must be loaded. Currently, winsock1 and Winsock2 are supported. Before calling WinSock, you must load the corresponding Winsock library.
The API used for loading is wsastartup.
When the application ends, the disabled API is wsacleanup.

Wsastartup requires a version number and a struct of wsadata as the return value of the function. Its prototype is as follows:

Int Wsastartup (word wversionrequested,
Lpwsadata );

Next, let's take a look at what information the wsadata struct describes:
Typedef Struct Wsadata
{
Word wversion; // Version Number
Word whighversion; // Max version of existing Winsock
Char Szdescription [...]; // No practical effect
Char Szsystemstatus [...]; // No practical effect
Unsigned Short Imaxsocket; // Maximum number of sockets that can be opened simultaneously
Unsigned Short Imaxudpdg; // Maximum datagram Length
Char Far * Lpvendorinfo; // Reserved Field
} Wsadata * Lpwsadata

SpecificCode:

# Include " Winsock2.h "
Int Main ()
{
Wsadata;
Int RET = Wsastartup (makeword ( 2 , 2 ), & Wsadata );
If (Ret ! =   0 )
{
Cout<"Unable to initialize Winsock."<Endl;
}
Else
Cout < " Winsock initialization successful " < Endl;

// ..
 
If (Wsacleanup () = Socket_error)
{
Cout<"Wsadata cannot be cleanup.";
}
Return   0 ;
}

 

Ii. IPv4 struct

When a computer passes through TCP or UDP, the IP address and port number of the application may be the IP address of the host. How can this information be expressed? In WinSock, you can specify the IP address and service port information through a struct. The struct is as follows:

Struct Sockaddr_in
{
Short Sin_family; // Af_inet must be specified
U_short sin_port; // Port Number
Struct In_addr sin_addr; // IP address Representation
Char Sin_zero [ 8 ]; // Fill fields to be compatible with the sockaddr struct
} ;

There are also some other functions that can easily convert the IP address to the string form, 32-bit integer form of in_addr struct form. In addition, you can convert the local byte sequence to the network byte sequence.
1. convert a vertex IP address into a 32-bit unsigned long integer:
Unsigned long inet_addr (const char far * CP );
2. Convert host byte sequence to network byte sequence
U_long htonl (u_long hostlong );
Int wsahtonl (socket S, u_long hostlong, u_long far * lpnetlong );
U_short htons (u_short hostshort );
Int wsahtons (socket S, u_short hostshort, u_short far * lpnetshort );
The difference is that some bytes need to be converted to the long type, such as the IP address, and some only need the short type, such as the port number.
The inverse functions are as follows:
U_long ntohl (u_long netlong );
Int wsantohl (socket S, u_long netlong, u_long far * lphostlong );
// For the short type

3. Create socket 4. functions to be called by the server and Step 5. functions to be called by the client and Step 6. Data Transmission and receiving
Related Article

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.