locked
How to bind StreamSocket with ControlChannelTrigger in server side?

    Question

  • In "ControlChannelTrigger StreamSocket sample", I found that only client side StreamSocket bind with a ControlChannelTrigger. I try to modify the code to support StreamSockets of both server & client side can bind with a ControlChannelTrigger, so when any side received a message, a toast will be showed. 

    But there is a mistake in my code, a error message is show: "A method was called at an unexpected time".

    Below is my code:

    bool CommModule::AcceptConnection(String^ serviceName)
    {
        // Create and store a streamsocketlistener in the class. This way, new connections
        // can be automatically accepted.
        if (serverListener == nullptr)
        {
            serverListener = ref new StreamSocketListener();
        }

        serverListener->ConnectionReceived += 
            ref new TypedEventHandler<StreamSocketListener^, StreamSocketListenerConnectionReceivedEventArgs^>([this] (StreamSocketListener^ op, StreamSocketListenerConnectionReceivedEventArgs^ evt)
        {
            critical_section::scoped_lock slock(lock);
            
            // For simplicity, the server can talk to only one client at a time.
            serverSocket = evt->Socket;

            if (writePacket != nullptr)
            {
                delete writePacket;
                writePacket=nullptr;
            }

    create_task([this]()
    {
    DebugLog::MarshalLogWithTime("Calling UsingTransport() ...");
    try 
    {
    channel->UsingTransport(serverSocket);
    }
    catch (Exception^ e)
    {
    DebugLog::MarshalLogWithTime("Error: " + e->Message);
    }
    });

            DebugLog::MarshalLogWithTime("Connection Received!");      
        });

        create_task(serverListener->BindServiceNameAsync(serviceName)).then([](task<void> previousTask)
        {
            try
            {
                previousTask.get();
            }
            catch(Exception^ e)
            {
                DebugLog::MarshalLogWithTime("Unable to start listener: " + e->Message);
            }
        });

        return true;
    }

    Is any problem in my code?

    Wednesday, January 30, 2013 9:02 AM

Answers

  • Hello,

    The scenario of using real-time notifications for a socket based server is not a supported scenario. The error you are getting is because you are trying to setup the ControlChannelTrigger after the socket has been created/ connected, but the UsingTransport API requires that only the socket handle is created and the connection is not yet created. http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.controlchanneltrigger.usingtransport.aspx

    "An app must call the UsingTransport method after the transport object (a StreamSocket                  instance, for example) has been created, but before a network connection is established. "

    For a server based app, when the server "accepts" the incoming connection, you get notified once the connection is established but not when the socket handle is created which explains the error.

    But the summary is that ControlChannelTrigger functionality is not supported for a server based app (using StreamSocketListener).

    Can you explain your scenario and provide more details on why are connect real-time notifications for a "server" important to you?

    Thanks,

    Prashant.

    Monday, March 4, 2013 11:17 PM
    Moderator

All replies

  • Hello,

     

     I will involve more experts to investigate it.

     

    Best regards,

    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 1, 2013 8:35 AM
  • Hi Focse.LT,

    If I just use the above code and replace the AcceptConnection function of the "ControlChannelTrigger StreamSocket sample" with your code, I get a NULL pointer exception because the 'channel' has been declared & initialized somewhere else.

    For reproducing the issue you have mentioned, can you zip and upload your entire solution to a SkyDrive location so that I can help you further?

    Thanks,

    Prashant.

    Friday, February 1, 2013 11:36 PM
    Moderator
  • Hello,

    We have not heard back on this thread, so if you need further assistance with your specific scenario or if you cannot determine your answer here or on your own, consider opening a support case with us. Visit this link to see the various support options that are available to better meet your needs:  http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone or you can use your developer account to open a case with Microsoft support.

    Thanks,

    Prashant.

    Wednesday, February 6, 2013 11:17 PM
    Moderator
  • Sorry, i'm late for your response!

    I have upload my source code which is derived from  "ControlChannelTrigger StreamSocket sample".

    Please check my code and give me some advices.

    Thanks,

    LT.

    Monday, February 18, 2013 5:36 AM
  • And where exactly is your source code?
    Monday, February 18, 2013 6:06 PM
    Moderator
  • http://sdrv.ms/156A1se
    Thursday, February 21, 2013 8:24 AM
  • Hello,

    The scenario of using real-time notifications for a socket based server is not a supported scenario. The error you are getting is because you are trying to setup the ControlChannelTrigger after the socket has been created/ connected, but the UsingTransport API requires that only the socket handle is created and the connection is not yet created. http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.controlchanneltrigger.usingtransport.aspx

    "An app must call the UsingTransport method after the transport object (a StreamSocket                  instance, for example) has been created, but before a network connection is established. "

    For a server based app, when the server "accepts" the incoming connection, you get notified once the connection is established but not when the socket handle is created which explains the error.

    But the summary is that ControlChannelTrigger functionality is not supported for a server based app (using StreamSocketListener).

    Can you explain your scenario and provide more details on why are connect real-time notifications for a "server" important to you?

    Thanks,

    Prashant.

    Monday, March 4, 2013 11:17 PM
    Moderator