locked
StreamsocketListerner to wait for a connection

    Question

  • Hi,

    I am working on a Metro App in C++.

    I need a streamsocketlistener to always listen to the client. Is there any simple script available.

    Monday, December 02, 2013 10:15 AM

Answers

All replies

  • Hi AnjuR,

    Welcome here!

    Something like this:

    oid Scenario1::StartListener_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
        // Overriding the listener here is safe as it will be deleted once all references to it are gone. However, in many cases this
        // is a dangerous pattern to override data semi-randomly (each time user clicked the button) so we block it here.
        if (CoreApplication::Properties->HasKey("listener"))
        {
            rootPage->NotifyUser("This step has already been executed. Please move to the next one.", NotifyType::ErrorMessage);
            return;
        }
    
        if (ServiceNameForListener->Text == nullptr)
        {
            rootPage->NotifyUser("Please provide a service name.", NotifyType::ErrorMessage);
            return;
        }
    
        StreamSocketListener^ listener = ref new StreamSocketListener();
        ListenerContext^ listenerContext = ref new ListenerContext(rootPage, listener);
        listener->ConnectionReceived += ref new TypedEventHandler<StreamSocketListener^, StreamSocketListenerConnectionReceivedEventArgs^>(listenerContext, &ListenerContext::OnConnection);
    
        // Events cannot be hooked up directly to the ScenarioInput1 object, as the object can fall out-of-scope and be deleted. This would render
        // any event hooked up to the object ineffective. The ListenerContext guarantees that both the listener and object that serves it's events have the same life time.
        CoreApplication::Properties->Insert("listener", listenerContext);
    
        // Start listen operation.
        task<void>(listener->BindServiceNameAsync(ServiceNameForListener->Text)).then([this] (task<void> previousTask)
        {
            try
            {
                // Try getting all exceptions from the continuation chain above this point.
                previousTask.get();
                rootPage->NotifyUser("Listening", NotifyType::StatusMessage);
            }
            catch (Exception^ exception)
            {
                CoreApplication::Properties->Remove("listener");
                rootPage->NotifyUser("Start listening failed with error: " + exception->Message, NotifyType::ErrorMessage);
            }
        });
    }
    

    You could get it from here:

    http://code.msdn.microsoft.com/windowsapps/StreamSocket-Sample-8c573931

    This sample demonstrates the basics of the StreamSocket class using the networking features provided by the Windows Runtime. The client component of the sample creates a TCP socket to make a network connection, uses the socket to send data, and closes the socket. The server component of the sample creates a TCP socket to listen for and accept network connections, accepts connections from the incoming socket, uses the socket to receive data from the client, and closes the socket.

    Regards!

    Xiaoliang


    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.

    Tuesday, December 03, 2013 3:48 AM
    Moderator
  • Actually, I need a socket server in App and a socket client in desktop for testing purpose. It will not go into store. I tried both in c# but connection is not established. what's the best way to do this?
    Tuesday, December 03, 2013 11:46 AM
  • Hi AnjuR,

    Welcome back!

    Have you tried the sample? I think the sample has explained how to do what you want to do very clearly.

    Regards!


    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.

    Wednesday, December 04, 2013 5:15 AM
    Moderator
  • Server is running but the client is not getting connected from desktop. 

    What should be the socket class in Desktop mode to connect to this streamsocket listener?

    I don't see streamsocket class in desktop mode c#.

    I tried with Socket class but not connecting.

    Thursday, December 05, 2013 10:26 AM
  • Hi AnjuR,

    Welcome back!

    If you want to connect to the streamsocket listener, you need to understand How socket works and which class could help you to do so.

    Please have a look at http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.streamsocket.aspx

    You could use it to connect to your streamsocket listener.

    I think you need to understand How socket works.

    Regards!


    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.

    Friday, December 06, 2013 1:52 AM
    Moderator
  • Bear in mind that connections to/from processes running on the same machine are restricted for WinRT apps. It is possible to locally lift this restriction on a WinRT app for development/debugging purposes. Take a look at the "Enable loopback for network access" section in this article: http://msdn.microsoft.com/en-us/library/windows/apps/Hh780593.aspx
    Friday, December 13, 2013 6:22 PM