Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) Bidirectional Sockets (TLS or non-TLS, simultaneous reading and writing a connection)This example demonstrates how to simultaneously read/write on a single socket connection.
#include <CkSocket.h> #include <CkTask.h> void ChilkatSample(void) { CkString strOut; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkSocket tlsRead; // We'll just use an HTTPS server for this example... bool bUseTls = true; int maxWaitMs = 5000; bool success = tlsRead.Connect("www.chilkatsoft.com",443,bUseTls,maxWaitMs); if (success != true) { strOut.append(tlsRead.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Chilkat classes are thread-safe. This means that only one method call can be active // at a time for a given object instance. It would seem that this would prevent the possibility // to simultaneously read/write a given connection because it would require two method calls // to be simultaneously active: one for reading and one for writing. // // There's a trick to doing it... // // The CloneSocket method is provided to get a new object instance that shares the same socket // connection. This allows for the coarse-grained object-level thread safety to be maintained, // while finer-grained thread-safety mechanisms keep things kosher internally. // One object will be used for reading, and the cloned socket is used for writing. // It doesn't matter which -- you can use the cloned socket for reading or the original for writing. // However.. if you try to read simultneously from both the original and cloned objects at the same // time, then one will block until the other finishes. (This is because of the finer-grained thread // safety internally.) The same is true if you try to write both socket objects simultaneously. CkSocket *tlsWrite = tlsRead.CloneSocket(); // Let's start an async read on the socket. Nothing will be arriving until we actually send the GET // request and the server responds. This will read until the end of the HTTP response header. CkTask *task = tlsRead.ReceiveUntilMatchAsync("\r\n\r\n"); task->Run(); // Now send the request. This should not block because the read is happening on the tlsRead object. const char *httpGetReq = "GET / HTTP/1.1\r\nHost: www.chilkatsoft.com\r\n\r\n"; success = tlsWrite->SendString(httpGetReq); // Assuming success for the example... // Wait for the read task to finish. // The true/false returned by Wait applies to the Wait method call, not the task. maxWaitMs = 5000; success = task->Wait(maxWaitMs); if (!success || (task->get_StatusInt() != 7) || (task->get_TaskSuccess() != true)) { if (!success) { // The task.LastErrorText applies to the Wait method call. strOut.append(task->lastErrorText()); strOut.append("\r\n"); } else { // The ResultErrorText applies to the underlying task method call (i.e. the Connect) strOut.append(task->status()); strOut.append("\r\n"); strOut.append(task->resultErrorText()); strOut.append("\r\n"); } delete task; delete tlsWrite; SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Examine the received HTTP response header: strOut.append("HTTP response header:"); strOut.append("\r\n"); strOut.append(task->getResultString()); strOut.append("\r\n"); // We should get a response that looks like this: // HTTP response header: // HTTP/1.1 200 OK // Cache-Control: private // Content-Length: 7477 // Content-Type: text/html // Server: Microsoft-IIS/8.5 // Set-Cookie: ASPSESSIONIDSWDSTRTQ=BBNMIKGCHFJNILFFPLDIOGDE; secure; path=/ // X-Powered-By: ASP.NET // X-Powered-By-Plesk: PleskWin // Date: Thu, 06 Apr 2017 12:03:30 GMT delete task; delete tlsWrite; // Forget about the remainder of the HTTP response... The example was only to demonstrate // simultaneous reading/writing.. maxWaitMs = 20; tlsRead.Close(maxWaitMs); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.