VC network programming based on Winsock API

Source: Internet
Author: User
Tags socket

With the rapid development of computer information technology, the Internet has become more and more closely related to the work and life of human society, and it has been an important way and means for people to acquire and exchange information. So currently, network programming is an essential skill for developers. This example details how to use the Winsock API to write a network application.

One, the realization method

The most common scenario in network programming is the Client/server (client/server) model. In this scenario, the client application requests services from the server program. A service program listens to requests for services at a well-known address, that is, the service process is dormant until a client requests a connection to the address of the service. At this point, the service program is "awakened" and provides service to the customer-respond appropriately to the customer's request.

To facilitate the network programming of this client/server model, Microsoft joined several other companies in the early 90 to develop a set of Windows network programming interfaces, the Windows Sockets specification, which is not a network protocol, but an open , a network programming interface under Windows that supports multiple protocols. Now the Winsock has basically implemented the Protocol independent, you can use Winsock to invoke the functionality of a variety of protocols, but the more commonly used is the TCP/IP protocol. The socket actually provides a communication port on the computer that can communicate with any computer with a socket interface through this port. The application is transmitted over the network and the received information is implemented through this socket interface.

Microsoft has defined Winsock classes such as CAsyncSocket classes and CSocket classes derived from CAsyncSocket for Visual C + +, and they are easy to use, and readers of course have access to these classes to implement their own web programs, but in order to better understand the Winsock API Programming Technology, we discuss how to use the low-level API functions to achieve a simple Winsock network application design, explain how to operate the Socket on the server and client side, to achieve data transfer based on TCP/IP, and finally give the relevant source code.

VC in the Winsock API programming development, you need to use the following three files in the project, or there will be a compilation error.

1. WINSOCK. H: This is the header file for the Winsock API and needs to be included in the project.

2. WSOCK32. Lib:winsock API Connection library file. In use, be sure to include it in the project file as a Non-default connection library for your project.

3. WINSOCK. The Dll:winsock Dynamic Connection library, located in the Windows installation directory.

Server-side operation socket (socket)

1. Call WSAStartup at initialization stage ()

This function initializes the Windows Sockets DLL in the application, and the application can then invoke the API functions in other Windows Sockets DLLs only after this function call succeeds. The function is called in the program in the following form: WSAStartup (WORD) (1<<8|1), (Lpwsadata) &wsadata), where (1<<8|1) It means that we are using the WinSocket1.1 version, Wsaata is used to store information about the WinSocket that the system is sending back.

2. Establish socket

After initializing the Winsock dynamic Connection library, you need to establish a listening socket on the server side, for which you can invoke the socket () function to establish the listening socket and define the communication protocol used by the socket. This function call successfully returns the SOCKET object, and the failure returns Invalid_socket (the call to WSAGetLastError () is known for the reason that all WinSocket API functions can use to obtain the cause of the failure).

Socket PASCAL FAR socket (int af, int type, int protocol)

Parameters: AF: Currently only provides pf_inet (AF_INET);

Type of Type:socket (Sock_stream, SOCK_DGRAM);

Protocol: Communication Agreement (set to 0 if the user does not specify);

If you are establishing a socket that complies with the TCP/IP protocol, the second parameter type should be sock_stream, such as a UDP (datagram) socket, which should be sock_dgram.

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.