如何捕获C ++ / CX中断开连接的StreamSocket引发的COMException?

时间:2016-05-19 15:04:47

标签: sockets windows-runtime c++-cx

我正在编写一个应用程序,该应用程序应该将从蓝牙LE设备接收的一些数据重新转换为连接到它的所有TCP客户端。该平台是Windows 10.目前它是具有C ++ / CX启用的控制台应用程序,因为蓝牙LE API仅可从WinRT获得。 BLE部分已准备就绪但我无法使用WinRT制作正确的TCP服务器。

我正在使用StreamSocketListener创建TCP套接字服务器并将所有StreamSocket对象存储在std :: vector中。在循环中迭代向量并将数据发送到所有连接的客户端。在这一点上,一切都很好。但是,如果一个客户端断开连接并且服务器在尝试将数据发送到断开连接的套接字并且抛出COMException并且无法捕获它时崩溃。

Visual Studio 2015崩溃消息:Exception thrown at 0x752ADAE8 in SensoCLI.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x02EFE1C0.将堆栈指向auto sent = writeTask.get();

以下是与我的问题相对应的应用的最小列表:

#include "pch.h"
#using <platform.winmd>
#using <Windows.winmd>

using namespace std;
using namespace Windows::Foundation;
using namespace Platform;
using namespace concurrency;
namespace WFC = Windows::Foundation::Collections;
namespace WNS = Windows::Networking::Sockets;
namespace WSS = Windows::Storage::Streams;

bool shouldStop = false;

// TCP socket server
WNS::StreamSocketListener ^tcpListener;
vector<WNS::StreamSocket ^> *tcpClients;

void OnSocketConnectionReceived(WNS::StreamSocketListener ^aListener, WNS::StreamSocketListenerConnectionReceivedEventArgs ^args);

int main(Array<String ^> ^args)
{
    tcpClients = new vector<WNS::StreamSocket ^>();
    tcpListener = ref new WNS::StreamSocketListener();
    tcpListener->ConnectionReceived += ref new TypedEventHandler<WNS::StreamSocketListener ^, WNS::StreamSocketListenerConnectionReceivedEventArgs ^>(&OnSocketConnectionReceived);
    auto listenTask = create_task(tcpListener->BindServiceNameAsync("53450"));
    listenTask.wait();

    while (!shouldStop)
    {
        if (tcpClients->size() > 0)
        {
            auto netDataStream = ref new WSS::InMemoryRandomAccessStream();
            auto writer = ref new WSS::DataWriter(netDataStream);
            auto reader = ref new WSS::DataReader(netDataStream->GetInputStreamAt(0));
            String^ stringToSend("Hello");
            writer->WriteUInt32(writer->MeasureString(stringToSend));
            writer->WriteString(stringToSend);
            auto aTask = create_task(writer->StoreAsync());
            unsigned int bytesStored = aTask.get();
            aTask = create_task(reader->LoadAsync(bytesStored));
            aTask.wait();
            auto netData = reader->ReadBuffer(bytesStored);

            for (auto iter = tcpClients->begin(); iter != tcpClients->end(); ++iter)
            {
                create_task((*iter)->OutputStream->WriteAsync(netData)).then([](task<unsigned int> writeTask) {
                    try
                    {
                        // Try getting an exception.
                        auto sent = writeTask.get();
                        wcout << L"Sent: " << sent << endl;
                    }
                    catch (Exception^ exception)
                    {
                        wcout << L"Send failed with error: " << exception->Message->Data() << " " << endl;
                    }
                });
            }
            Sleep(1000);
        }
    }

}

void OnSocketConnectionReceived(WNS::StreamSocketListener ^aListener, WNS::StreamSocketListenerConnectionReceivedEventArgs ^args)
{
    auto sock = args->Socket;
    tcpClients->push_back(sock);
}

如何正确捕获异常并处理断开连接的StreamSocket?

0 个答案:

没有答案