Windows platform VC + + 6.0 under the Network programming Learning-Simple test Winsock.h header file

Source: Internet
Author: User
Tags html tags

Recently learning data structures and algorithms have been a little tired (seemingly not so tired ...) )... Looking for this network programming turned over as a small basis, and intend to continue to learn the data structure side also look at the network programming related ...

The simple first attempt, the general comb the Reading + own understanding of things.

1. First, a little introduction to Winsock: Winsock is a standard API (application programming interface) that is used primarily for data communication in the network, which allows two applications to communicate with each other on the same machine or over the network. It is important to note that Winsock is not protocol independent, and the Winsock programming interface allows applications to establish communication through common network protocols such asTCP/IP(network protocol) or IPX(Internet Packet Exchange Protocol) protocol.

2. Then the basic creation of the project of network programming under vc++6.0, first put a piece of code:

#include <winsock2.h>#include<stdio.h>voidMainvoid) {wsadata wsadata;  Note 1 intRet; //Initialize Winsock version 2.2    if(Ret = WSAStartup (Makeword (2,2), &wsadata)! =0)    {        //If Winsock is not loaded, calling the WSAStartup () function will return a SOCKET_ERROR error codeprintf"WSAStartup failed with error%d\n", Ret); return ; }    if(WSACleanup () = =socket_error) The { //wsacleanup () function frees all resources allocated by Winsock and cancels the application pending Winsock calls to printf ("WSACleanup failed with error%d\n", WSAGetLastError ()); }}

Since the main function entry for the program is main (), the Win32 Console application should be selected when building the project, and then we are writing the winsock2.h application, which needs to be linked to Ws2_32.lib If you are winsock.h , you need to use WSOCK32. LIB; if it is mswsock. H(This is for Microsoft-specific programming extensions), you also need to link mswsock. DLL. (All above except DLL files are added in the Object/library modules: Bar in vc++6.0, project, setting, link)

The above code has a note 1, a little explanation:

When you initialize Winsock (that is, call function WSAStartup () load Winsock) The structure is populated with information about the loaded version and a pointer to the WSADATA structure:

struct wsadata {    WORD            wversion;    WORD            whighversion;     Char            1 ];     Char            1 ];      Short   imaxsockets;      Short   IMAXUDPDG;     Char FAR *      * lpwsadata;

We don't care much about the specifics of this structure, but at least we should know how to call a function to populate it. If you want to know the meaning and function of the members in the WSADATA structure, you can read the book, "Windows network Programming," The first chapter has a very detailed explanation.

Finally, the first network programming learning is summed up in this way, I mainly use a piece of error-handling code to test the header file and learn how to start programming the network program, with a simple introduction will be much clearer. Personal feeling this summary still has a little gain. (Spit a bit of personal learning front-end and Windows programming feeling, learning for a few days, progress is very fast, but a period of time do not touch, do not want to touch ... The properties and parameters that lead to HTML tags are forgotten, and the details of some functions in Windows programming may be forgotten ... Tasted

Windows platform VC + + 6.0 under the Network programming Learning-Simple test Winsock.h header file

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.