Use Winsock to implement the VC ++ programming of the chat room

zhaozj2021-02-16  43

One: Socket Introduction

At the beginning of the 1980s, the US government's senior research engineering (ARPA) provided funds to the University of California, providing them to implement TCP / IP protocols under UNIX operating systems. In this project, the researchers developed an API (application interface) for TCP / IP network communication. This API is called a Socket interface (socket). Today, the Socket interface is the most general API of the TCP / IP network, and is also the most common API on the Internet.

In the early 1990s, Microsoft combined with several other companies to develop a network programming interface under Windows, which is the WindowsSockets specification. It is an important expansion of BerkeleySockets, mainly to increase some asynchronous functions and add a network event asynchronous selection mechanism that meets Windows messaging characteristics. The WindowsSockets specification is an open, supported network programming interfaces under Windows that support multiple protocols. From version 1.0 from 1995 to version 2.0.8, it has become a factual standard for Windows network programming under the full support of Intel, Microsoft, Sun, SGI, Informix, Novell, etc. Currently, the WindowsSokcets specification in practical applications mainly include 1.1 and version 2.0. The most important difference between the two is the 1.1 version only support TCP / IP protocol, and version 2.0 can support multi-protocols. 2.0 has good backward compatibility, any source code, binary files, binary files, and applications can be used without modifying it in 2.0 specification.

Socket actually provides a communication port in your computer, which can communicate with any computer with a socket interface via this port. The application is transmitted on the network, and the received information is implemented by this socket interface. In the application development, just like the file handle, you can read, write operations.

2: Introduction to application development based on Windows socket.

There are many programming languages ​​for WINSOCK development in Windows95 / 98, Winsock development, VC , Java, Delphi, VB, etc. The most common use of VC is the most common in combination with Winsock. And VC has a series of packages for the original WindowsSockets library function, which has created CasyNsocket, Csocket, CSocketFile, etc., which encapsulates the various functions of sockets, which are more simple. But if you are a beginner of Winsock programming, it is recommended that you program the most basic API functions at the beginning or learn Winsock, which can greatly deepen the understanding of Winsock and is very good for future.

In the VC, Winsock's API programming is developed, and the following three files are needed:

1 Winsock.h: This is the header file of the Winsock API.

2 WSOCK32.LIB: Winsock API connection library file. In use, a point to contain it as a non-default connection library to the project file.

3 Winsock.dll: Winsock's dynamic connection library, located in the installation directory of Windows.

The location of the Winsock interface in the Windows programming environment is shown below:

==================

================== can be seen, Winsock. DLL is located between TCP / IP protocol stacks and applications. That is, Winsock management interfaces with TCP / IP protocols. When you start Winsock, you don't have to know the TCP / IP protocol. However, if you want to be a master that is programmed for network, you must know it clearly.

The most common scheme in network program is the client / server model. In this program, the client application requests services to the server program. A service program is usually a well-known address to listen to the service request, that is, the service process has been in a sleep state until a customer puts a connection request for the address of this service. At this moment, the service program is "awakened" and provides customers with services - the appropriate response to the client's request. Although the service based on the connection protocol (streamlined text) is a standard when designing a client / server application, some services can also be provided by no connection protocols (Data Supply Settings). Its programming model is as follows:

Socket programming model for connection protocol =================================

Socket programming model without connection protocol ================================= Generally in use, Socket programming for connection protocols The model application is the most extensive because the connection protocol provides a series of data error correction capabilities, which guarantees that the data transmitted on the network is timely, and it reaches the other party. In general, when using the socket interface (connected or not connected) to network communication, you must process the following simple four steps: 1, the program must establish a socket. 2, the program must be configured as required to configure this socket. That is, the program either connects this socket to the remote host, or give this socket to specify a local protocol port.

3. The program must send and receive data as required by this Socket.

4, the program must turn off this socket.

Three: Introduction to the main function of Winsock API

The author uses the Winsock API to write an application with a chat room, which can be used as a reference for learning Winsock programming. The Winsock API includes a lot of functions, but most common, including in the articles attached to:

Note: It is just a brief description of the function, specific rules, and see VC Help and Winsock specification.

1. WSAStartup (): Connect the first function of the application and the Windows Sockets DLL.

Description: This function is the first of the application calls the Windows Sockets DLL function, and only this function call is successful before calling a function of other Windows Sockets DLLs.

2, wsacleanup (): End the use of Windows Sockets DLL.

Note: This function is required to cancel the use in order to release the resources used in the application no longer need to use the Windows Sockets DLL. 3, Socket (): Establish Socket.

Description: This function is used to create a socket description word and build the resources used for this socket.

4, CloseSocket (): Close a socket.

Note: This one is used to close some socket.

5, bind (): Connect a local address with a Socket description.

Note: This function is used on the service program, which is the function that calls the listening function listen () must call.

6, listen (): Set the socket to the listening state, ready to be connected.

Description: This function is used on the service program to set the socket to enter the listening state, and set up to how many of the number of connections that do not really complete the connection before the connection. (The current maximum limit is 5, the most

Small value is 1)

7, accept (): Accepting a Socket connection requirement to complete the connection request for the connectionable client Socket.

Description: The server application calls this function to accept the client socket connection request, the return value of the accept () function is a new socket, the new socket can be used to transfer the information between the server and the client, and the original Socket still receives connection requirements for other clients.

8, Connect (): Requires a Socket to the specified network server.

Note: This function is used in the client, used to establish a connection to the server. When the connection is completed, the client can use this socket to communicate with the server.

9, RECV (): Receive information from the connected socket.

Description: This function is used to receive information from the connected socket.

10, send (): Send information with the connected socket.

Description: This function is used to send information from the connection-oriented Socket.

11, wsaasyncselect (): Requires a Socket has events (event) notified when the user occurs.

Note: This function is used to request a message for Windows Sockets DLL for the window handle - no matter where it detects a network event indicated by the Levent parameter. The message to be sent is indicated by the WMSG parameter. The socket being notified is identified by the s. This function automatically sets the socket to non-blocking mode.

The LEVENT parameter consists of the values ​​listed in the following table.

Value meaning

FD_READ wants to receive a read ready-to-have notification.

FD_WRITE wants to receive a write-ready notification.

FD_OOB wants to receive notifications from the boundary data arrival.

FD_ACCEPT wants to receive notifications to be connected.

FD_Connect wants to receive a good notification.

FD_CLOSE wants to receive a notification of the socket closure.

This function can be considered as the most important function in the Winsock API. To use this function, you must have a clear understanding of the event drivers and messaging of Windows programming.

4: Design instructions for chat room applications:

Software function:

A service called IRC can be provided on the Internet. The user can log in to the IRC server through the client program, you can talk to the customers who log in to the same IRC server, which is the usual chat room. Here, a program for implementing an IRC service on a network running a TCP / IP protocol is given.

Software instructions:

First, run the server program on a computer, then run the client program on the same network, log in to the server, and you can chat between each customer.

Software design points:

1, the server

Core Code In the CSERVIWE class, there is a socket variable m_hserversocket and socket array m_aclientsocket [maxclient] (maxclient: the maximum number of receiving connection customers), M_HServersocket is used to listen in the specified port (> 1000), if there is The client requests to connect, then find an empty socket in the m_aclientsocket array, give the client's address to this socket. Whenever a ClientSocket receives information, a message will be issued to the window. After the program receives this message, send the received information to each ClientSocket.

2, client

The client is relatively simple, the core code is in the CClientDLG class. There is only one socket variable M_HSocket, which is connected to the server. After the connection is established, this Socket is sent and received information.

In order to simplify the design, the username is on the client control, the server side is only simple receiving information and "broadcast" this information, which is not the name check, that is, can have the same name customer to log in to the server. Although this program is designed, the most basic function of the chat room has been provided.

The program is compiled under VC 6.0, and the WINDOWS 95/98 peer network using the TCP / IP protocol is working well on the Windows NT LAN with TCP / IP protocol.

转载请注明原文地址:https://www.9cbs.com/read-19348.html

New Post(0)