Establish a network program with a callback function and CSocket

zhaozj2021-02-17  71

Some of the experiences in using CSocket, the special offer is shared with you.

First use VC to establish a project (single document, multi-document, or dialog box), followed by a single document project as an example.

In the engineering of the CSocket class inherited CServersock. CMyView * m_pView added in CServerSocket class constructor modified CServerSocket CServerSocket :: CServerSocket (CMyView * pView) {m_pView = pView}, listen for client connection class; adding CClientSock class inherits from the CSocket same, the same modifications as CClientSock The constructor; the server starts to connect to the code as follows:

CMYVIEW class (error code is implemented

CserverVersock * m_pserver;

CClientSock * m_pclient;

WSADATA WSA;

AfXSocketinit (& WSA);

M_Pserver = New Cserversock (this);

M_Pserver-> Create (my_port, sock_stream, my_ipadd);

m_pserver-> listen (my_maxnumber);

Overload virtual functions on the CserverVersock class onaccept

m_pview-> accept (); // Toned the Accept function in the CMYVIEW class;

CMYVIEW Class New Function ACCEPT

m_pserver-> accept (m_pclient)

Overload virtual functions in CClientSock class Onreceive

m_pview-> rv (); // callback the RECV function in the CMYVIEW class;

CMYVIEW Class New Function Recv

m_pclient-> receive (buf, bufsize);

To this server is ready

Add a CClientSock class in the client, also modify the constructor, overreload the virtual function onRecEive

M_PView-> Recv (); // The RECV () function in the CMYVIEW is accepting the message;

The client's connection is

CClientSock * m_pclient;

WSADATA WSA;

AfXSocketinit (& WSA);

M_pclient = new cclientsock (this);

m_pclient-> connect (Server_ADD, Server_Port);

It is now successfully connected to the server, which can be communicated.

Please be ax, welcome to communicate with me scxy78@yeah.net

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

New Post(0)