Creating and Using a CAsyncSocket Object to use CAsyncSocket

CK1820 
Created at Aug 28, 2007 01:56:05 
103   0   0   0  

Followings are the procedure to send/receive UDP packet by CAsyncSocket.

STEP 1. Construction

Construct 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. Connection

If 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.

Note

The 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 Share on Facebook Share on X

◀ PREVIOUS
UDP Send and Receive Using CAsyncSocket
▶ NEXT
MFC based World Wide Web HTTP Server Source Code
  Comments 0
Login for comment
SIMILAR POSTS

UDP Send and Receive Using CAsyncSocket (created at Aug 28, 2007)

MFC based World Wide Web HTTP Server Source Code (created at Aug 28, 2007)

Using the shell to receive notification of removable media being inserted or removed (created at Aug 28, 2007)

Fast and Good Keyboard/Mouse Test without the message handler (created at Aug 27, 2007)

Simple CGI Programming in C (created at Aug 27, 2007)

C++ CGI Example working on Apache (created at Aug 27, 2007)

Double Linked List based in C++ (created at Sep 08, 2007)

Binary Search Sample Code (created at Sep 08, 2007)

How to run shell command by MFC ? (created at Sep 09, 2007)

How to search file on certain directory ? (created at Jun 15, 2008)

How to call SetTimer function on MFC CDialog class ? (created at Jul 30, 2008)

How to load HTML resource on MFC ? (updated at Dec 17, 2023)

URL Encode, Decode function for MFC (updated at Dec 20, 2023)

KMP String Matching Algorithm (created at Jul 21, 2010)

OTHER POSTS IN THE SAME CATEGORY

Find Files in certain directory (created at Feb 25, 2008)

Implementing C#'s foreach loop in Delphi 8 (created at Oct 03, 2007)

How to detect resource leaks in a Form (created at Sep 25, 2007)

The best way to enable and disable buttons, menu items and toolbar buttons (created at Sep 25, 2007)

How to exit from a console application (created at Sep 25, 2007)

How to restrict a program to a single instance (created at Sep 25, 2007)

Compress and decompress strings in C# (created at Sep 25, 2007)

Graphics in C# - Irregular Forms (created at Sep 25, 2007)

Extracting the Country from IP Address (created at Sep 25, 2007)

How to run shell command by MFC ? (created at Sep 09, 2007)

Binary Search Sample Code (created at Sep 08, 2007)

Double Linked List based in C++ (created at Sep 08, 2007)

String Replace Function For C# (created at Aug 28, 2007)

How to print character in C# by ASCII code (created at Aug 28, 2007)

MFC based World Wide Web HTTP Server Source Code (created at Aug 28, 2007)

UDP Send and Receive Using CAsyncSocket (created at Aug 28, 2007)

Using the shell to receive notification of removable media being inserted or removed (created at Aug 28, 2007)

Adding Custom Paper Sizes To Named Printers (created at Aug 27, 2007)

Print HTML In C# With Or Without The Web Browser Control And The Print Dialog (created at Aug 27, 2007)

A Customizable Printing Text Class (created at Aug 27, 2007)

Simplified .NET Printing In C# (created at Aug 27, 2007)

How To Print Text In C# (created at Aug 27, 2007)

C Sharp Lists (created at Aug 27, 2007)

Simple Example Of IF/THEN Using C# (created at Aug 27, 2007)

Fast and Good Keyboard/Mouse Test without the message handler (created at Aug 27, 2007)

Simple CGI Programming in C (created at Aug 27, 2007)

C++ CGI Example working on Apache (created at Aug 27, 2007)

Simple C# CGI working on Apache (created at Aug 27, 2007)

How to send binary data through C# CGI app? (created at Aug 27, 2007)

Interacting With TinyPic From C# (created at Aug 26, 2007)

UPDATES

Creating a Pinterest-Style Card Layout with Bootstrap and Masonry (created at Apr 24, 2024)

Mastering Excel Data Importation in PHP (updated at Apr 24, 2024)

JSON format control in PHP (updated at Apr 24, 2024)

Equal Height Blocks in Bootstrap with JavaScript (created at Apr 22, 2024)

How to convert integer to text string ? (updated at Apr 22, 2024)

Checking similarity between two strings in PHP (updated at Apr 21, 2024)

Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file (updated at Apr 21, 2024)

How do I determine the client IP type (IPv4/IPv6) in PHP (updated at Apr 16, 2024)

How do I determine the client IP type in Python - IPv4 or IPv6 (updated at Apr 13, 2024)

Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network (updated at Apr 09, 2024)

Predicting Buyer Preferences with PyTorch: A Deep Learning Approach (updated at Apr 09, 2024)

Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction (created at Apr 09, 2024)

PyTorch example to Forcast Stock Price based on 10 days Dataset (created at Apr 09, 2024)

Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python (created at Apr 08, 2024)

Harnessing the Power of Random Forest Algorithm in Python (created at Apr 08, 2024)

Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python (created at Apr 08, 2024)

Forecasting with Linear Regression and KNN Regression in Python (updated at Apr 07, 2024)

What is 302 Found Redirection in HTTP 1.1? (created at Apr 04, 2024)

Mastering Random Forest Regression: A Comprehensive Guide with Python Examples (updated at Apr 01, 2024)

Python Implementation of Linear Regression (updated at Apr 01, 2024)

Mastering Supervised Machine Learning with Python: A Comprehensive Guide (created at Apr 01, 2024)

Mastering AI: A Beginner's Guide to Python Programming and Beyond (created at Apr 01, 2024)

How do I create animated background for Google Meet? (updated at Mar 28, 2024)

Building a Simple DNS Server in Delphi with TTL Support (created at Mar 16, 2024)

How to force cookies, disable php sessid in URL ? (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)

Building a Basic DNS Server in PHP/Python: A Beginner's Guide (updated at Mar 15, 2024)

Dynamic DNS Made Easy: Building a Python-Based Solution (created at Mar 15, 2024)

Exploring the Depths of Data Transfer: sendfile vs. kTLS (created at Mar 15, 2024)