Windows Sockets: Socket notification

Source: Internet
Author: User

This article describes the notification functions in the socket class. These member functions are all callback functions, and the Framework calls them to notify the socket object of important events. Notification functions include:

  • OnReceive: Call Receive to notify the socket buffer of data to be retrieved.
  • OnSend: Call Send to notify this socket that data can be sent now.
  • OnAccept: Call Accept to notify this listening socket to Accept pending connection requests.
  • OnConnect: notifies this connection socket that its connection attempt has been completed: It may be successful or there may be errors.
  • OnClose: indicates that the Socket connected to this socket has been closed.

Note:An additional notification function is OnOutOfBandData. This notification tells the receiving socket to send "out-of-band" data. The out-of-band data is a logically independent channel that is associated with each pair of connected streaming sockets. Out-of-band channels are usually used to send "urgent" data. MFC supports out-of-band data. Advanced users using the CAsyncSocket class may need to use an out-of-band channel, but users of the CSocket class should not use it. A simpler way is to create another socket to pass the data. For more information about out-of-band data, see the Windows Sockets specification in Platform SDK.

IfCAsyncSocketClass, the notification function must be rewritten for the network events that the application is interested in. IfCSocketClass, you can choose whether to override the notification function of interest. You can also useCSocketIn this case, the notification function does not perform any operations by default.

These functions are rewritable callback functions.CAsyncSocketAndCSocketConvert messages to notifications, but if you want to use the notification function, you must implement their response methods. When a socket is notified of an event of interest (for example, data to be read), the notification function is called.

The number of MFC call notifications allows you to customize the behavior when the socket is notified. For example, you mayOnReceiveNotification function callReceive, That is, you can callReceiveRead the data. This method is not necessary, but it is an effective solution. Another way is to use the notification function to track progress and printTRACEMessages.

You can use these notifications by overwriting the notification function in the derived socket class and providing implementation. For implementation examples, see the notification function rewriting in the MFC example CHATTER and CHATSRVR.

In the process of receiving or sending data,CSocketThe object is synchronized. In the synchronization status, the current socket waits for the notification it wants, and the notifications corresponding to other sockets are queued. (For exampleReceiveDuring the call, the socket wants to read the notification ). Once the socket completes its synchronization operation and changes to asynchronous again, other sockets can start to receive queued notifications.

Note:InCSocketAnd never callOnConnectNotification function. For a connection, callConnect, It will return (success or error) when the connection is complete ). How to handle connection notifications is detailed in the implementation of MFC.

For more information about each notification function, see "MFC reference ".CAsyncSocketClass. For the source code and information of the MFC example, see the MFC example.

For more information, see:

  • Windows Sockets: CAsyncSocket class
  • Windows Sockets: Derived from the socket class
  • Windows Sockets: working with an archived socket
  • Windows Sockets: Blocking
  • Windows Sockets: byte sorting
  • Windows Sockets: Convert strings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.