locked
StreamSocket Sample with fixed IP address/port number endpoint

    Question

  • I would like to determine if a Metro app can listen on a particular endpoint. To do that, I have modified the Win8 Consumer Preview Sample app called StreamSocket. Here are my code changes:

    Line 75 was:

    StreamSocketListener^ listener = ref new StreamSocketListener();

    which defaults to localhost.

    A COM error occurs, however:

    What does 'A method was called at an unexpected time' mean, and more importantly, how does one resolve such an error?


    RT

    Wednesday, April 11, 2012 5:41 PM

Answers

  • add_ConnectionReceived does a check for if the endpoint is already bound or binding. Put the  'listener->ConnectionReceived += connectionReceived' before the 'listener->BindEndpointAsync(..)' and you should eliminate the runtime error.

    -Steve

    • Marked as answer by rtischer8277 Tuesday, April 17, 2012 5:37 PM
    Tuesday, April 17, 2012 1:12 AM
    Moderator

All replies

  • Hello,

    I follow your codes and build a project without error. Maybe you get the error at the run time. Please follow these codes to catch the runtime error.

    	auto streamSocket= ref new Windows::Networking::Sockets::StreamSocket();
    	auto localHostName= ref new Windows::Networking::HostName("98.175.98.105");
    	String^ localSeiceName("49000");
    
        StreamSocketListener^ listener = ref new StreamSocketListener();
    	task<void>(listener->BindEndpointAsync(localHostName,localSeiceName)).then([this,&listener](task<void> previousTask){
    		try{
    
    			previousTask.get();
    
    		}catch(Exception^ exception)
    		{
    			String^ error= exception->Message;
    		}
    		listener->ConnectionReceived += ref new TypedEventHandler<StreamSocketListener^, StreamSocketListenerConnectionReceivedEventArgs^>(this, &ScenarioInput1::OnConnection);
    
    	});


    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, April 12, 2012 8:38 AM
  • Actually, the exception occurs on the next statement:

    listener->ConnectionReceived += ref new TypedEventHandler<StreamSocketListener^, StreamSocketListenerConnectionReceivedEventArgs^>(this, &ScenarioInput1::OnConnection);
    How do I wrap a continuation task around that? Especially since it has the += subscribe to a WinRT event syntax in it. Do I treat the whole statement as the object of a task<...> to be continued with a .then?

    RT

    Saturday, April 14, 2012 9:13 PM
  • I split the statement up into two steps so the code now becomes:

        auto connectionReceived = ref new TypedEventHandler<StreamSocketListener^, StreamSocketListenerConnectionReceivedEventArgs^>(this, &ScenarioInput1::OnConnection);
        listener->ConnectionReceived += connectionReceived;
    

    The += says to subscribe to a COM event for events. But I get this runtime error:

    Platform::COMException ^ at memory location 0x02FCE9D0. HRESULT:0x8000000E A method was called at an unexpected time.

    The only difference between my code and the StreamSocket sample code is calling the bind statement with explicit host and service names:

    StreamSocketListener^ listener = ref new StreamSocketListener();
    listener->BindEndpointAsync( localHostName, localServiceName );
    


    RT

    Sunday, April 15, 2012 12:42 PM
  • Clarification: the exception occurs on the second statement:

    listener->ConnectionReceived += connectionReceived;


    RT

    Monday, April 16, 2012 11:41 AM
  • add_ConnectionReceived does a check for if the endpoint is already bound or binding. Put the  'listener->ConnectionReceived += connectionReceived' before the 'listener->BindEndpointAsync(..)' and you should eliminate the runtime error.

    -Steve

    • Marked as answer by rtischer8277 Tuesday, April 17, 2012 5:37 PM
    Tuesday, April 17, 2012 1:12 AM
    Moderator
  • Thanks. Works fine now.

    RT

    Tuesday, April 17, 2012 5:37 PM