Network communication based on Winsocket

zhaozj2021-02-08  237

Network communication realization based on Winsocket [1999-08-09] Chen Guangli

In VC , the MFC programming supports two programming modes that utilize Windows Sockets for network communication. These two modes are CSocket classes with CasyncSocket classes and derived in CasyncSocket. * CasyncSocket classes encapsulated Windows Sockets API functions, providing lower-level, WINDOWS Sockets dialog interface, generally suitable for use with considerable network programming base, easy to carry out the underlying network event notification and information callback control, etc. operating. * CSocket is derived from CasyncSocket, which inherits some of the commonly used easy-to-understand Windows Sockets API functions, and processes some of the hard-controlled some API functions or member functions of the bottom layer of CasyncSocket, which is based on the MFC CARCHIVE object. Dental operations such that network transmission is like using the MFC document serial load protocol (Serialization Protocol), simple and easy. At the same time, it supports modular background information processing, solves the multi-threaded process that is difficult to overcome in CasyncSocket. The establishment and use of Socket is a server, you need to have a SOCKET dedicated to answering, so that you can receive information sent from the customer at any time, and the Listen of the CSocket class supports multi-thread, so you don't have to open new threads. Steps to establish a Windows Sockets See Table 1.

 Server Socket Establishment and Use * Socket Global Variables in Server Applications, and make it in listening, etc. to wait for the client to establish a connection: clisteningsocket  m-pstent; m-psocket = new clisteningsocket (this); if (M-PSocket-> Create (Dialog.M-NPORT 700)) {if (m-psocket-> listen ()) Return True;} * Call the document class reception when receiving the active connection to the client New customer handle function: void clistenings {csocket :: onaccept (nerrorcode); m-pdoc-> processpendingAccept (); // call document class member function to generate new conversation thread} * server Socket online data transmission and reception operation: void cclientsocket :: Sendmsg (cmsg PMSG) {if (m-parchiveout! = Null) {PMSG-> Serialize (M-parchiveout); // call Press Protocol Custom Information Class (CMSG) function m-parchiveout-> flush (); // serialize for online information transmission operation}} void cclientsocket :: removeMsg (cmsg PMSG) {PMSG-> Serialize (M-parchivein);} // call Upload information of online information by protocol custom information class (CMSG) to receive an operation client to establish a connection and communication implementation * New Customer and Server Establishment Connection: BOOL CCHATDOC :: ConnectSocket (LPCTSTSTSTSZHANDLE, LPCTSTSTSZZADDRESS, UINT NPORT) {m-strhandle = lpszhandle; m-psocket = new cchatsocket (this); if (! m-psocket-> create ()) {...} // Create a new Socket failed to handle while (! m-psocket-> connect (LPSZADDRESS, NPORT 700)) {...} // Treatment after connection failure with the server M-pfile = new csocketfile (m-psocket); m-parchivein = new carchive (m-pfile, carchive :: load); M- Parchiveout = new carchive (m-pfile, carchive :: store); // Establish a communication flow object Return True;} * Client read online information from the stream: void cchatdoc :: removemsg () {cmsg msg; try {msg .Serialize (M-parchivein); call the function while (! Msg.m-msglist.isempty ()) // serialize (... Catch) CFILEEXCEPTION, E) {...} end-catch // error handling if (msg.m-bclose) {...} // closes the connection of the connection} * Client via the stream online send information: Void CCHATDOC: : Sendmsg (CString &)

Strtext, int index, int i) {if (m-parchiveout! = null) {cmsg msg; msg = assememsg (index, i); // assembly information try {msg-> serialize (M-parchiveout); / / Call the function m-parchiveout-> flush (); // serialize, e) {... // error handling END (CFILEEXCEPTION, E) -Catch}} Table 1 Server Note Client 1csocket Socksrvr; Constructs a Socket Object Csocket SockClient; 2SockSrvr.create (NPORT); Create SocketSockClient.create (NPORT); 3SockSrvr.Listen (); Listening and other connections with the server to establish a connection SOCKCLIENT .connect (straddr, nport); 4csocket sockrecv; socksrvr.accept (sockrecv); Constructs a new socket object to receive client connection 5csocketfile file (& sockrecv); configured a file object csocketfile file (& sockrecv); 6carchive arin (& file, CARCHIVE :: Load; CARCHIVE AROUT (& File, CARCHIVE :: Store); Technical Sworthy Carchive Arin; CARCHIVE AROUT (& File, Carchive :: Store); 7arin >> DwValue; arout << DWValue; Transmission of data in the stream arin >> DWValue; arout << dwvalue;

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

New Post(0)