The CSocket class is an inheritance of CAsyncSocket, so it also encapsulates Windows API functions. In contrast to the CAsyncSocket class, CSocket is a higher level of abstraction of the Windows Socket API (experience: In other words, easier to use, but difficult to perform as CAsyncSocket precise communication control). CSocket class and CSocketFile, CArchive class together to implement the data sent and received.
CAsyncSocket is only supported in non-blocking mode, while the CSocket class only supports blocking mode. At the same time, unlike the CAsyncSocket class, CSocket is the process of sending and receiving data with the CArchive class and the Carchivefile class.
The CSocket class provides its required blocking by the CArchive class. Its action function send,sendto,accept,listen,connect,accept from does not send Wasewouldblock errors to CSocket, but waits for the operation to end. If, during the operation of these functions, (the control thread) invokes cancleblockcal, they send a wasewouldblock error to the CSocket class, that is, cancleblockcal blocks the blocking of the wasewouldblock.
How to use the CSocket object:
1, calling the constructor of the CSocket class.
2, call the CREATE function of the CSocket object to create a WINDWOS socket handle (the default is a stream-oriented socket, TCP, which specifically specifies the socket to create UDP). If you are working with a CArchive object, you can bind the socket to a fixed port by specifying the parameters and create a socket on the server.
3, at the client, the Connect function that invokes the CSocket object connects to the server, and on the server side, uses accept to accept the connection.
4, creates a connection to the CSocket object with the CSocketFile object while creating the CSocketFile object by using the CSocket object as an input parameter to the constructor of the CSocketFile object.
5, the connection between CSocketFile objects and CArchive objects can be established by using the CSocketFile object as an input parameter to the constructor of the CArchive object.
6, after completing the communication, REVOKE (Destroy) CArchive, CSocketFile, and CSocket objects.