Creating and Using a CAsyncSocket Object to use CAsyncSocket | |||
| |||
Followings are the procedure to send/receive UDP packet by CAsyncSocket. STEP 1. ConstructionConstruct a CAsyncSocket object and use the object to create the underlying SOCKET handle.Creation of a socket follows the MFC pattern of two-stage construction. For example: CAsyncSocket sock; sock.Create( ); // Use the default parameters -or- CAsyncSocket* pSocket = new CAsyncSocket; int nPort = 27; pSocket->Create( nPort, SOCK_DGRAM ); The first constructor above creates a CAsyncSocket object on the stack. The second constructor creates a CAsyncSocket on the heap. The first Create call above uses the default parameters to create a stream socket. The second Create call creates a datagram socket with a specified port and address. (You can use either Create version with either construction method.) The parameters to Create are: * A "port": a short integer. For a server socket, you must specify a port. For a client socket, you typically accept the default value for this parameter, which lets Windows Sockets select a port. * A socket type: SOCK_STREAM (the default) or SOCK_DGRAM. * A socket "address," such as "ftp.microsoft.com" or "128.56.22.8". This is your Internet Protocol (IP) address on the network. You will probably always rely on the default value for this parameter. The terms "port" and "socket address" are explained in Windows Sockets: Ports and Socket Addresses. STEP 2. ConnectionIf the socket is a client, connect the socket object to a server socket, using CAsyncSocket::Connect.-or- If the socket is a server, set the socket to begin listening (with CAsyncSocket::Listen) for connect attempts from a client. Upon receiving a connection request, accept it with CAsyncSocket::Accept. After accepting a connection, you can perform such tasks as validating passwords. NoteThe Accept member function takes a reference to a new, empty CSocket object as its parameter. You must construct this object before you call Accept. If this socket object goes out of scope, the connection closes. Do not call Create for this new socket object. For an example, see the article Windows Sockets: Sequence of Operations.STEP 3. Communication (Send/Receive)Carry out communications with other sockets by calling the CAsyncSocket object's member functions that encapsulate the Windows Sockets API functions.See the Windows Sockets specification and class CAsyncSocket in the MFC Reference. STEP 4. Destruction (Termination)Destroy the CAsyncSocket object.If you created the socket object on the stack, its destructor is called when the containing function goes out of scope. If you created the socket object on the heap, using the new operator, you are responsible for using the delete operator to destroy the object. The destructor calls the object's Close member function before destroying the object. Tags: C++ Datagram HTTP Daemon SOCK_DGRAM UDP UDP Receive | |||
| |||
| |||
Login for comment |
SIMILAR POSTS UDP Send and Receive Using CAsyncSocket |