none
stremsocketlistener的connectionreceived RRS feed

  • 问题

  • 如下所示,分两种方法实现listener的connectionreceived,为何第二种方式不工作,没有监听成功

    方法1:
    listener->ConnectionReceived += ref new TypedEventHandler<StreamSocketListener^, StreamSocketListenerConnectionReceivedEventArgs^>
    ([this](StreamSocketListener^ op, StreamSocketListenerConnectionReceivedEventArgs^ evt)
    {
    DataReader^ reader = ref new DataReader(evt->Socket->InputStream);
    ReceiveStringLoop(reader, evt->Socket);
    });

    方法2:
    auto onConnectionReceived = ref new TypedEventHandler<StreamSocketListener^, StreamSocketListenerConnectionReceivedEventArgs^>
    (this, &CSocketManager::OnConnection);
    listener->ConnectionReceived += onConnectionReceived;
    void CSocketManager::OnConnection(StreamSocketListener^ sender, StreamSocketListenerConnectionReceivedEventArgs^ e)
    {
    DataReader^ reader = ref new DataReader(e->Socket->InputStream);
    ReceiveStringLoop(reader, e->Socket);
    }


    2014年6月9日 8:29

答案

  • Hi,

    我测试了下你的代码,方法一和方法二都没有问题。你的程序也许是其他地方有问题。你试试看将方法二换成下面的代码看是否还有问题:

    listener->ConnectionReceived += ref new TypedEventHandler<StreamSocketListener^, StreamSocketListenerConnectionReceivedEventArgs^>
    		(this, &CSocketManager::OnConnection);
    	void CSocketManager::OnConnection(StreamSocketListener^ sender, StreamSocketListenerConnectionReceivedEventArgs^ e)
    	{
    		DataReader^ reader = ref new DataReader(e->Socket->InputStream);
    		ReceiveStringLoop(reader, e->Socket);
    	}
    		
    

    你可以提供给我们一个能重现此问题的project,这样我们可以测试问题出在哪里!

    祝您好运!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年6月11日 1:17
    版主