Windows Sockets: Socket Notifications

本文介绍了 Windows Sockets 中的几种通知机制,包括 OnReceive、OnSend、OnAccept、OnConnect 和 OnClose 等,这些机制允许应用程序通过回调函数的形式接收套接字的重要事件通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Windows Sockets: Socket Notifications

Home |  OverviewHow Do ISample

This article describes the notification functions in the socket classes. These member functions are callback functions that the framework calls to notify your socket object of important events. The notification functions are:

  • OnReceive: Notifies this socket that there is data in the buffer for it to retrieve by calling Receive.
  • OnSend: Notifies this socket that it can now send data by calling Send.
  • OnAccept: Notifies this listening socket that it can accept pending connection requests by calling Accept.
  • OnConnect: Notifies this connecting socket that its connection attempt completed: perhaps successfully or perhaps in error.
  • OnClose: Notifies this socket that the socket it is connected to has closed.

Note   An additional notification function is OnOutOfBandData. This notification tells the receiving socket that the sending socket has “out-of-band” data to send. Out-of-band data is a logically independent channel associated with each pair of connected stream sockets. The out-of-band channel is typically used to send “urgent” data. MFC supports out-of-band data. Advanced users working with class CAsyncSocket might need to use the out-of-band channel, but users of class CSocket are discouraged from using it. The easier way is to create a second socket for passing such data. For more information about out-of-band data, see the Windows Sockets specification, available in the Win32 SDK.

If you derive from class CAsyncSocket, you must override the notification functions for those network events of interest to your application. If you derive a class from class CSocket, it’s your choice whether to override the notification functions of interest. You can also use CSocket itself, in which case the notification functions default to doing nothing.

These functions are overridable callback functions. CAsyncSocket and CSocket convert messages to notifications, but you must implement how the notification functions respond if you wish to use them. The notification functions are called at the time your socket is notified of an event of interest, such as the presence of data to be read.

MFC calls the notification functions to let you customize your socket’s behavior at the time it is notified. For example, you might call Receive from your OnReceive notification function. That is, on being notified that there is data to read, you call Receive to read it. This approach isn’t necessary, but it is a valid scenario. As an alternative, you might use your notification function to track progress, print TRACE messages, and so on.

You can take advantage of these notifications by overriding the notification functions in a derived socket class and providing an implementation. For an example implementation, see the notification function overrides in the MFC Advanced Concepts samples CHATTER and CHATSRVR.

During an operation such as receiving or sending data, a CSocket object becomes synchronous. During the synchronous state, any notifications meant for other sockets are queued while the current socket waits for the notification it wants. (For example, during a Receive call, the socket wants a notification to read.) Once the socket completes its synchronous operation and becomes asynchronous again, other sockets can begin receiving the queued notifications.

Important   In CSocket, the OnConnect notification function is never called. For connections, you simply call Connect, which will return when the connection is completed (either successfully or in error). How connection notifications are handled is an MFC implementation detail.

For details about each notification function see the function under class CAsyncSocket in the Class Library Reference. For source code and information about MFC samples, see MFC Samples.

What do you want to know more about?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值