Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

MFC Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(MFC) Socket Select for Reading

Demonstrates how the Chilkat socket object can become a "socket set" that contains other connected socket objects, and this can be used to "select" on multiple sockets for reading. The SelectForReading method waits until one or more sockets in the set have incoming data ready and available to read.

Chilkat C/C++ Library Downloads

MS Visual C/C++ Libs

See Also: Using MFC CString in Chilkat

#include <CkSocket.h>
#include <CkStringArray.h>

void ChilkatSample(void)
    {
    CkString strOut;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This will be the socket object that will contain other 
    // connected sockets.
    CkSocket socketSet;
    bool success;

    // Before we begin, create a few StringArray objects holding
    // domain names, URLs, and output filenames to be used
    // in this example.
    CkStringArray saDomains;
    CkStringArray saUrls;
    CkStringArray saFilenames;

    saDomains.Append("www.chilkatsoft.com");
    saDomains.Append("www.cknotes.com");
    saDomains.Append("www.google.com");
    saDomains.Append("www.example-code.com");
    saDomains.Append("www.yahoo.com");

    saUrls.Append("http://www.chilkatsoft.com/");
    saUrls.Append("http://www.cknotes.com/");
    saUrls.Append("http://www.google.com/");
    saUrls.Append("http://www.example-code.com/");
    saUrls.Append("http://www.yahoo.com/");

    saFilenames.Append("chilkatsoft.out");
    saFilenames.Append("cknotes.out");
    saFilenames.Append("google.out");
    saFilenames.Append("example-code.out");
    saFilenames.Append("yahoo.out");

    int numConnections = 5;

    // This example will connect to 5 different web servers,
    // send an HTTP request to each, and read the HTTP response
    // form each.  It will call the SelectForReading method to wait
    // until data arrives from any of the connected sockets.

    // NOTE: As you'll see later, communicating with HTTP servers
    // is better left to the Chilkat HTTP component/library because
    // of many HTTP protocol issues such as redirects, GZIP responses,
    // chunked responses, etc.  We're only communicating with
    // HTTP servers here as a convenient way of demonstrating
    // the technique of waiting for data to arrive from a set of 
    // connected sockets.

    // First, connect to each of the web servers:
    int i;
    bool useSsl = false;
    int maxWaitMs = 5000;
    int port = 80;
    const char *domain = 0;
    const char *getReq = 0;

    for (i = 0; i <= numConnections - 1; i++) {
        CkSocket sock;

        domain = saDomains.getString(i);

        success = sock.Connect(domain,port,useSsl,maxWaitMs);
        if (success != true) {
            strOut.append(sock.lastErrorText());
            strOut.append("\r\n");
            SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
            return;
        }

        // Save a filename in the socket object's UserData.  We'll use this
        // later when saving the data read in the HTTP response.
        sock.put_UserData(saFilenames.getString(i));

        // Display the remote IP address of the connected socket:
        strOut.append("connected to ");
        strOut.append(sock.remoteIpAddress());
        strOut.append("\r\n");

        // Build the HTTP GET request to be sent to the web server.
        getReq = sock.buildHttpGetRequest(saUrls.getString(i));

        // Send the HTTP GET request to the web server.
        success = sock.SendString(getReq);
        if (success != true) {
            strOut.append(sock.lastErrorText());
            strOut.append("\r\n");
            SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
            return;
        }

        // Tell the socketSet object to take ownership of the connected socket.
        // This adds the connection to the internal set of sockets contained
        // within socketSet.   
        success = socketSet.TakeSocket(sock);
        if (success != true) {
            // The TakeSocket method shouldn't fail.  The only reason it might
            // is if we try to give it an unconnected socket -- but we already 
            // know it's connected at this point..
            strOut.append(socketSet.lastErrorText());
            strOut.append("\r\n");
            SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
            return;
        }

    }

    // At this point we have 5 TCP/IP connections contained within the socketSet object.
    // Each of the web servers have received a GET request and will be
    // sending a response.  We now want to write a loop that waits for data
    // to arrive from any of the 5 connections, reads incoming data, and
    // saves the data to files.

    // We'll use the StartTiming method and ElapsedSeconds property
    // to wait a maximum of 10 seconds.  The reason we do this is because
    // different web servers will behave differently w.r.t. idle connections.
    // Some web servers close the connections quickly while others do not.
    // The socket component is not "HTTP aware", meaning that it is not parsing
    // the HTTP response and therefore does not know when it has received
    // the full response.  Because of this, we might receive the full response
    // and then wait a long time before the server decides to close its side of
    // the connection.  
    // The ElapsedSeconds property will return the number of seconds that
    // have elapsed since the last call to StartTiming (on the same object instance).
    socketSet.StartTiming();

    while (((socketSet.get_NumSocketsInSet() > 0) && (socketSet.get_ElapsedSeconds() < 10))) {

        maxWaitMs = 300;

        int numReady;
        // Wait for data to arrive on any of the sockets.  Waits a maximum
        // of 300 milliseconds.  If maxWaitMs = 0, then it is effectively a poll.
        // If no sockets have data available for reading, then a value of 0 is 
        // returned.  A value of -1 indicates an error condition.
        // Note: when the remote peer (in this case the web server) disconnects,
        // the socket will appear as if it has data available.  A "ready" socket
        // is one where either data is available for reading or the socket has
        // become disconnected.
        numReady = socketSet.SelectForReading(maxWaitMs);
        if (numReady < 0) {
            strOut.append(socketSet.lastErrorText());
            strOut.append("\r\n");
            SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
            return;
        }

        // Consume data from each of the ready sockets, and show those
        // that become disconnected.   
        for (i = 0; i <= numReady - 1; i++) {

            // To make method calls on the "ready" socket, or to get/set property
            // values, set the SelectorReadIndex.  
            socketSet.put_SelectorReadIndex(i);

            // Print information that identifies the socket that is ready for reading:
            strOut.append("ready: IP address = ");
            strOut.append(socketSet.remoteIpAddress());
            strOut.append(" filename = ");
            strOut.append(socketSet.userData());
            strOut.append("\r\n");

            // Read the selected socket and send the data directly to a file (appending
            // to the file).
            success = socketSet.ReceiveBytesToFile(socketSet.userData());

            // The success/failure of a socket read should normally be checked.
            // In this case it is not necessary.  If the socket became ready for
            // reading because the remote peer disconnected and there is no
            // data available, then nothing will be read.  The socketSet object
            // will automatically remove a disconnected socket from its internal set of sockets
            // the next time SelectForReading or SelectForWriting is called.
            // We can check to see if the socket is still connected by examining
            // the IsConnected property:
            if (socketSet.get_IsConnected() != true) {
                strOut.append("disconnected: ");
                strOut.append(socketSet.userData());
                strOut.append("\r\n");
            }

        }

        // Set the SelectorReadIndex = -1 so that method calls
        // and property gets/sets are not routed to one of the socketSet's
        // contained sockets.  This is necessary because we want to
        // check the number of elapsed seconds from when we called
        // StartTiming.  
        socketSet.put_SelectorReadIndex(-1);

    }

    strOut.append("Finished.");
    strOut.append("\r\n");

    // Some final notes:
    // If you examine the output files, you'll see that the HTTP protocol is a little
    // more complex than you may have originally thought.
    // 1) HTTP responses are MIME -- there is a MIME header followed by a MIME body.
    //     In most cases, the MIME body is the HTML document itself.
    // 2) Some HTTP responses return gzip compressed data (which is binary).  It must
    //      be decompressed to get the HTML.
    // 3) Some HTTP responses arrive in "chunked" form.  You'll see lines containing nothing but
    //      a hexidecimal number indicating the size of the chunk to follow.  It ends with
    //      a 0-sized chunk.
    // 4) You'll find that some HTTP responses might be redirects to other URL's (such as a 302 redirect).

    // For the above reasons, and many more, you would typically use the Chilkat HTTP component
    // to handle the complexities of HTTP.  

    // The reason HTTP was chosen for this example is because:
    // 1) It is easy to establish multiple connections with existing servers that are not typically not blocked by firewalls.
    // 2) It is easy to send a request and receive a response.
    // 3) There is variability  in the amount of time before a server decides to disconnect from an idle client


    SetDlgItemText(IDC_EDIT1,strOut.getUnicode());

    }

 

© 2000-2022 Chilkat Software, Inc. All Rights Reserved.