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

zhaozj2021-02-08  227

Use Winsock to implement the VC programming of the chat room

First, Socket Introduction

In the early 1980s, the US government's senior research engineering (ARPA) gave California

Learn Berkeley Branch provides funding, let them implement TCP / IP association under UNIX operating systems

. In this project, researchers have developed an API for TCP / IP network communication.

Program interface). This API is called a Socket interface (socket). Today, Socket interface

It is the most common API of TCP / IP network, and it is also the most public development on the Internet.

Use the API.

At the beginning of the 1990s, Microsoft combined with several other companies together.

Developed a network programming interface under Windows, which is the WindowsSockets specification. it is

The important expansion of BerkeleySockets is mainly to increase some asynchronous functions and increase

Network event asynchronous selection mechanism in accordance with Windows Message Drive Features. Windowssockets

The specification is an open and supports network programming interfaces under Windows of multiple protocols. From

From the 1991 version by version 2.0.8 in 1995, it has been continuously improved and in Intel.

With the full support of Microsoft, Sun, SGI, Informix, Novell, etc.

The factual standard for Windows network programming. Currently, in practical applications

The WindowsSokcets specification mainly has from 1.1 and version 2.0. The most important difference between the two is version 1.1

Only the TCP / IP protocol is supported, and the version 2.0 can support multi-protocols. 2.0 has a good backward compatibility

Sex, any source code, binary files, binary files, applications can not be modified

Use under 2.0 specification.

Socket actually provides a communication port in the computer, which can pass this port

Communication with any computer with a socket interface. Applications are transmitted on the network, pick up

The information received is implemented through this socket interface. Just use the file sentence in application development

Like the handle, the Socket handle can be read and written.

Second, the application development introduction based on Windows Socket.

There are a lot of programming languages ​​for WINSOCK development in Windows95 / 98, WINDOWSNT.

VC , Java, Delphi, VB, etc. The most common use of VC, and the best in combination with Winsock

close-knit. And VC has a series of packages for the original WindowsSockets library function.

Following CasyNsocket, Csocket, CsocketFile, etc.

The various functions of socket are more simpler programming. But if you are a

The first martial arts programmed by Winsock, then I suggest you to learn Winsock's most basic at the beginning.

The API function programming, which can greatly deepen the understanding of Winsock, which is very good for future

Place.

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, point it as a project

The non-default connection library contains 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:

You can see, Winsock. DLL is located between TCP / IP protocol stacks and applications. Also

It is said that Winsock management interfaces with TCP / IP protocols. When you start Winsock, you don't have to know the TCP / IP protocol. But if you want to be a

The masters of online programming must be very clear about the next level.

The most common scheme in network program is the client / server model. In this scenario

The client application requests service to the server program. A service program is usually in a public

Knowing the address monitoring to the service, 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 process

The order is "awakened" and serve customers - the appropriate response to the customer's request. although

However, the service based on the connection protocol (flow sleeve) is when designing client / server applications.

Standard, but some services can also be provided by no connection protocols (data sheets).

Its programming model is as follows:

Socket programming model for connection protocols

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

Socket programming model without connection protocol

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

Generally in use, the Socket programming model for connection protocols is the most widely used.

Provide a series of data error correction capabilities for the connection protocol, which can be transmitted over the network

The data is promptly, and it will arrive at each other.

In general, when using the socket interface (or connected or connected), network communication,

Must be processed as simple as follows:

1. The program must establish an Socket.

2, the program must be configured as required to configure this socket. In other words, the program either

Socket is connected to the remote host, or give this socket to specify one

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 has written a chat room function using the Winsock API.

Applications can be used as a reference for learning Winsock programming. Winsock API package

Including a lot of functions, but most common, including in the articles attached to:

Note: Just a brief description of the function, specific rules, explanation, please see VC Help and

Winsock specification.

Function name function description

WSAStartup ()

Connect the first function of the application with the Windows Sockets DLL

This function is the first one in the application calls the Windows Sockets DLL function, and only this function call is successful before calling the function of other Windows Sockets DLLs. WSacleanup ()

Ending the use of Windows Sockets DLL When the application is no longer needed to use the Windows Sockets DLL, you must call this function to log out in order to release the resources they occupy.

Socket () Create Socket This function is used to create a socket description word and build the resources used for this Socket.

CloseSocket () Close a socket This function is used to close some socket

Bind () connects a local address with a socket description word. This function is used on the service program. It is a function that calls the listening function listen () must call.

Listen () Sets Socket to the listening status, ready to be connected to this function on the service program to set the socket to enter the listening status, and set up to how many of the number of connections that are not truly completed before the connection. (At present, the maximum limit is 5, the minimum is 1)

Accept () accepts a socket connection requirement to complete the connection-oriented client socket connection request 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 pass information between the server and the client, and the original socket can still receive connection requirements for other clients.

Connect () requires this function to connect to a Socket to the specified network to serve 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.

Recv () Receive information from the connection-oriented Socket This function is used to receive information from the connected socket

Send () Use the Connected Socket Send Information This function is used to send information from the connected socket

WSAAsyncselect () requires a Socket event (event) notifying the user when this function is used to request a message with the Windows Sockets DLL to the window handle - no matter where it detects the 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 value listed in the table below.

FD_READ wants to receive a read ready-to-have notice

FD_WRITE wants to receive a write ready

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 close

This function can be considered as the most important in the Winsock API.

A function. To use this function, you must drive Windows programming events

There is a clear understanding with the message delivery.

4: Design instructions for chat room applications:

Software function:

A service called IRC can be provided on the Internet. Users pass the client's program

On the IRC server, you can talk to customers who log in to the same IRC server, which is usually

The 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 server on a computer, then you can be on the same network

Other computers run the client program, 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 received reception connection customers), M_HServersocket is used in the specified port (> 1000)

Listening, if there is a client request connection, 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, 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 controlled by the client, and the server side is only simple receiver.

Interest and "Broadcast" this information, not the name check, that is, you can have the same name customer to log in to the server. This one

Although the program design is simple, the most basic function of the chat room has been provided.

The program is compiled under VC 6.0, and Windows 95/98 using TCP / IP protocol

The peer network and the Windows NT LANs using the TCP / IP protocol are well running well.

Use Winsock to implement the VC programming of the chat room

Luo Jie

(School of Computer Software and Theory, Guizhou University)

Summary: Winsock is an API interface programming in Windows, which is also

The fact standard for Windows network programming. The most common scheme in network programming is customer

Machine / server model. This article proposes to implement Winsock in a client / server model

Solutions for chat room software in the Internet.

Keywords: socket, Winsock, client / server, network programming

First, Socket Introduction

In the early 1980s, the US government's senior research engineering (ARPA) gave California

Learn Berkeley Branch provides funding, let them implement TCP / IP association under UNIX operating systems

. In this project, researchers have developed an API for TCP / IP network communication.

Program interface). This API is called a Socket interface (socket). Today, Socket interface

It is the most common API of TCP / IP network, and it is also the most public development on the Internet.

Use the API.

At the beginning of the 1990s, Microsoft combined with several other companies together.

Developed a network programming interface under Windows, which is the WindowsSockets specification. it is

The important expansion of BerkeleySockets is mainly to increase some asynchronous functions and increase

Network event asynchronous selection mechanism in accordance with Windows Message Drive Features. Windowssockets

The specification is an open and supports network programming interfaces under Windows of multiple protocols. From

From the 1991 version by version 2.0.8 in 1995, it has been continuously improved and in Intel.

With the full support of Microsoft, Sun, SGI, Informix, Novell, etc.

The factual standard for Windows network programming. Currently, in practical applications

The WindowsSokcets specification mainly has from 1.1 and version 2.0. The most important difference between the two is version 1.1

Only the TCP / IP protocol is supported, and the version 2.0 can support multi-protocols. 2.0 has a good backward compatibility

Sex, any source code, binary files, and applications can be used without modifying it in 2.0 specification.

Socket actually provides a communication port in the computer, which can pass this port

Communication with any computer with a socket interface. Applications are transmitted on the network, pick up

The information received is implemented through this socket interface. Just use the file sentence in application development

Like the handle, the Socket handle can be read and written.

Second, the application development introduction based on Windows Socket.

There are a lot of programming languages ​​for WINSOCK development in Windows95 / 98, WINDOWSNT.

VC , Java, Delphi, VB, etc. The most common use of VC, and the best in combination with Winsock

close-knit. And VC has a series of packages for the original WindowsSockets library function.

Following CasyNsocket, Csocket, CsocketFile, etc.

The various functions of socket are more simpler programming. But if you are a

The first martial arts programmed by Winsock, then I suggest you to learn Winsock's most basic at the beginning.

The API function programming, which can greatly deepen the understanding of Winsock, which is very good for future

Place.

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, point it as a project

The non-default connection library contains 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:

You can see, Winsock. DLL is located between TCP / IP protocol stacks and applications. Also

It is said that Winsock management interfaces with TCP / IP protocols. At the beginning, Winsock should have open

When you send, you don't have to have a deep understanding of the TCP / IP protocol. But if you want to be a

The masters of online programming must be very clear about the next level.

The most common scheme in network program is the client / server model. In this scenario

The client application requests service to the server program. A service program is usually in a public

Knowing the address monitoring to the service, 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 process

The order is "awakened" and serve customers - the appropriate response to the customer's request. although

However, the service based on the connection protocol (flow sleeve) is when designing client / server applications.

Standard, but some services can also be provided by no connection protocols (data sheets).

Its programming model is as follows:

Socket programming model for connection protocols

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

Socket programming model without connection protocol

============================================================================================================================================================================================================= ==== General in use, the Socket programming model for the connection protocol is the most widely used.

Provide a series of data error correction capabilities for the connection protocol, which can be transmitted over the network

The data is promptly, and it will arrive at each other.

In general, when using the socket interface (or connected or connected), network communication,

Must be processed as simple as follows:

1. The program must establish an Socket.

2, the program must be configured as required to configure this socket. In other words, the program either

Socket is connected to the remote host, or give this socket to specify one

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 has written a chat room function using the Winsock API.

Applications can be used as a reference for learning Winsock programming. Winsock API package

Including a lot of functions, but most common, including in the articles attached to:

Note: Just a brief description of the function, specific rules, explanation, please see VC Help and

Winsock specification.

Function name function description

WSAStartup ()

Connect the first function of the application with the Windows Sockets DLL

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

WSacleanup ()

Ending the use of Windows Sockets DLL When the application is no longer needed to use the Windows Sockets DLL, you must call this function to log out in order to release the resources they occupy.

Socket () Create Socket This function is used to create a socket description word and build the resources used for this Socket.

CloseSocket () Close a socket This function is used to close some socket

Bind () connects a local address with a socket description word. This function is used on the service program. It is a function that calls the listening function listen () must call.

Listen () Sets Socket to the listening status, ready to be connected to this function on the service program to set the socket to enter the listening status, and set up to how many of the number of connections that are not truly completed before the connection. (At present, the maximum limit is 5, the minimum is 1)

Accept () accepts a socket connection requirement to complete the connection-oriented client socket connection request 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 pass information between the server and the client, and the original socket can still receive connection requirements for other clients.

Connect () requires this function to connect to a Socket to the specified network to serve 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.

Recv () Receive Information from Connected Socket This function is used to send information from the connection-oriented Socket to the connection-oriented Socket.

WSAAsyncselect () requires a Socket event (event) notifying the user when this function is used to request a message with the Windows Sockets DLL to the window handle - no matter where it detects the 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 value listed in the table below.

FD_READ wants to receive a read ready-to-have notice

FD_WRITE wants to receive a write ready

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 close

This function can be considered as the most important in the Winsock API.

A function. To use this function, you must drive Windows programming events

There is a clear understanding with the message delivery.

4: Design instructions for chat room applications:

Software function:

A service called IRC can be provided on the Internet. Users pass the client's program

On the IRC server, you can talk to customers who log in to the same IRC server, which is usually

The 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 server on a computer, then you can be on the same network

Other computers run the client program, 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 received connection customers), M_HServersocket is used in the specified port (> 1000)

Listening, if there is a client request connection, 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, 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 controlled by the client, and the server side is only simple receiver.

Interest and "Broadcast" this information, not the name check, that is, you can have the same name customer to log in to the server. This one

Although the program design is simple, the most basic function of the chat room has been provided.

The program is compiled under VC 6.0, and Windows 95/98 using TCP / IP protocol

The peer network and the Windows NT LANs using the TCP / IP protocol are well running well.

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

New Post(0)