Creating and Using a CAsyncSocket Object to use CAsyncSocket

CK CK 3788 
Created at  
473 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 Facebook X
Comments 0
Similar posts
  1. UDP Send and Receive Using CAsyncSocket
    1,204
  2. MFC based World Wide Web HTTP Server Source Code
    460
  3. Fast and Good Keyboard/Mouse Test without the message handler
    982
  4. Simple CGI Programming in C
    545
  5. C++ CGI Example working on Apache
    486
  6. Double Linked List based in C++
    796
  7. Binary Search Sample Code
    407
  8. How to run shell command by MFC ?
    439
  9. How to search file on certain directory ?
    630
  10. How to call SetTimer function on MFC CDialog class ?
    651
  11. Makefile to automate the build process for C++
    280
  12. How to load HTML resource on MFC ?
    842
  13. URL Encode, Decode function for MFC
    605
  14. KMP String Matching Algorithm
    674
  1. Finding process name on OS
    409
  2. Convert System.DateTime to UNIX timestamp
    381
  3. Retrieve system information
    495
  4. How to rename(change file name) in C# ?
    1,080
  5. Find Files in certain directory
    928
  6. Implementing C#'s foreach loop in Delphi 8
    460
  7. How to detect resource leaks in a Form
    940
  8. The best way to enable and disable buttons, menu items and toolbar buttons
    667
  9. How to exit from a console application
    812
  10. How to restrict a program to a single instance
    729
  11. Compress and decompress strings in C#
    972
  12. Graphics in C# - Irregular Forms
    910
  13. Extracting the Country from IP Address
    469
  14. String Replace Function For C#
    1,550
  15. How to print character in C# by ASCII code
    477
  16. Using the shell to receive notification of removable media being inserted or removed
    681
  17. Adding Custom Paper Sizes To Named Printers
    649
  18. Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
    972
  19. A Customizable Printing Text Class
    433
  20. Simplified .NET Printing In C#
    693
  21. How To Print Text In C#
    455
  22. C# Lists
    1,152
  23. Simple Example of IF/THEN using C#
    1,266
  24. Simple C# CGI working on Apache
    481
  25. How to send binary data through C# CGI app?
    996
  26. Interacting With TinyPic From C#
    1,307
  27. Creating A Watched Folder With Assigned Events
    1,663
  28. Calling Your Main Thread From A Worker Thread In C#
    547
  29. HashTable Tutorial In C#
    1,789
  30. Stack Tutorial In C#
    669
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. How do I create animated background for Google Meet?
    21,044
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    252
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    71
  11. Frequency Cap Architecture Overview
    515
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  16. TPU & GPU Cluster Management Tools
    245
  17. The Concept of Scaffolding in AI Agents
    234
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    222
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    221
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    257
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  30. Understanding Data Lineage
    263