{"id":121,"date":"2009-02-03T08:19:04","date_gmt":"2009-02-03T15:19:04","guid":{"rendered":"http:\/\/cknotes.com\/?p=121"},"modified":"2009-02-03T08:24:02","modified_gmt":"2009-02-03T15:24:02","slug":"asynchronous-sockets","status":"publish","type":"post","link":"https:\/\/cknotes.com\/asynchronous-sockets\/","title":{"rendered":"Asynchronous Sockets"},"content":{"rendered":"<p>This blog post is an attempt to explain the concepts of asynchronous socket programming using the Chilkat Socket class\/component.\u00a0\u00a0 There are five types of socket operations that may occur asynchronously:<\/p>\n<ol>\n<li>Socket read.<\/li>\n<li>Socket write.<\/li>\n<li>Connect to remote hostname:port<\/li>\n<li>Accept connection from client<\/li>\n<li>DNS lookup<\/li>\n<\/ol>\n<p>A synchronous socket operation is easy to understand.\u00a0 If you call ReceiveBytes, the method returns only after it has received data on the socket.\u00a0 If you call Connect, the method returns only after a connection has been established.\u00a0 If you call AcceptNextConnection, the method returns after an incoming connection has arrived and has been accepted.<\/p>\n<p>Asynchronous methods allow your application to start a socket operation in a background thread.\u00a0 Your application is then free to do other things while the background thread is working on the socket operation.\u00a0 For example, your application might do database queries, or update the user interface, etc.\u00a0 Your application can periodically check to see if the asynchronous operation has completed.\u00a0 It does this by checking a component property such as AsyncReceivedFinished, AsyncSendFinished, AsyncAcceptFinished, etc.\u00a0 When the socket operation is finished, your application can get the status via properties such as AsyncReceiveSuccess, AsyncSendSuccess, etc.\u00a0 Other properties provide the data recieved or the socket object for an accepted connection.<\/p>\n<p>IMPORTANT:\u00a0 The purpose of the asynchronous socket methods is NOT for queueing up a bunch of socket operations.\u00a0 For example, if your application calls AsyncReceiveString to initiate a socket receive operation in the background, you cannot call AsyncReceiveString again until the first asynchronous receive completes.\u00a0\u00a0 But think about it:\u00a0 Why would you have more than one asynchronous read oustanding at a time on the same socket?\u00a0 It doesn&#8217;t really make sense.\u00a0 If Chilkat were to automatically start another read, where would the data go?\u00a0 When an AsyncReceiveString completes, the received string is available in the AsyncReceivedString property.\u00a0 Your application can capture or use the received data and initiate the next asynchronous read.\u00a0 (You may say: &#8220;But I want my application to continue reading for performance reasons.&#8221;\u00a0 The answer is:\u00a0 The operating system is already doing that for you in the background.\u00a0 The operating system manages connected sockets.\u00a0 When you receive data on a socket, in most cases you&#8217;re simply getting the data that the underlying operating system has already received and has waiting for the application in the underlying socket buffers.)<\/p>\n<p>Regarding asynchronous sending:\u00a0 Don&#8217;t forget that the operating system buffers socket operations.\u00a0 Unless the outgoing OS buffers are full, or the amount of data you&#8217;re sending in a single call is very large, a synchronous send will more-or-less return immediately.\u00a0 (Did you ever notice when using an FTP program such as WS-FTP, or CuteFTP, that when you upload a file to an FTP server, the upload begins very quickly but then slows down?\u00a0 It&#8217;s because the first amounts of data sent on the socket are buffered by the OS.\u00a0 The &#8220;send&#8221; completes while the operating system continues to work (in the background) to send the data in the outgoing socket buffer.\u00a0 The application is free to continue (in other words, the operating system says &#8220;thanks, I have the data and I&#8217;ll send it, you may continue with what you&#8217;re doing&#8221;.)\u00a0 A socket send operation will only block when the underlying socket send buffers are full.<\/p>\n<p>Back to the main point:\u00a0 Your application cannot start another asynchronous send operation until the outstanding send completes.\u00a0 The same goes for Connect and Accept operations.<\/p>\n<p>IMPORTANT:\u00a0 The Chilkat Socket component is full-duplex, which means:\u00a0 You may have simultaneous asynchronous receive, send, connect, and accept operations.\u00a0\u00a0 You cannot start more than one asynchronous operation of the same type.\u00a0 For example, if you start an asynchronous receive, you may also start an asynchronous send even though the asynchronous read has not yet completed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post is an attempt to explain the concepts of asynchronous socket programming using the Chilkat Socket class\/component.\u00a0\u00a0 There are five types of socket operations that may occur asynchronously: Socket read. Socket write. Connect to remote hostname:port Accept connection from client DNS lookup A synchronous socket operation is easy to understand.\u00a0 If you call [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[494,488],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-asynchronous","tag-socket"],"_links":{"self":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":0,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"wp:attachment":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}