locked
Fail to make socket connection from Desktop App to server in Metro App

    Question

  • I meet a problem, i can send data from Metro App to Desktop app, but while I try to connect from a Desktop app to Metro App, it failed, GetLastError function return 0x0000274c.

    Desktop code is, error portion is marked bold style:

    void CSocketServerDlg::OnBnClickedCancel3()
    {
        // TODO: Add your control notification handler code here
        SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);

        SOCKADDR_IN addrSrv;
        addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
        addrSrv.sin_family=AF_INET;
        addrSrv.sin_port=htons(CONNECT_PORT);
        int n = connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
        if (n == SOCKET_ERROR)
        {
            TCHAR szBuffer[100];
            wsprintf(szBuffer, _T("Socket connection failed. Error Value = 0x%08x"), GetLastError());
            AfxMessageBox(szBuffer);
        }

        char szBuffer[100] = {0};
        szBuffer[0] = 0x03;
        if (send(sockClient,szBuffer, 1,0) == SOCKET_ERROR)
            AfxMessageBox(_T("Socket send failed."));

        closesocket(sockClient);
    }

    The code fragment on Metro App is:

    void SockClient::MainPage::ButtonListener_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
        StreamSocketListener ^server;
        server = gcnew StreamSocketListener("127.0.0.1");
        server->ServiceName = "12345";
        auto op = server->AcceptAsync();

        op->Completed = ref new AsyncOperationCompletedHandler<Windows::Networking::Sockets::StreamSocket^>([=](IAsyncOperation<StreamSocket^>^ a)
           {
               if (op->Status == AsyncStatus::Completed)
               {
                   StreamSocket ^ stream = op->GetResults();
                   DataReader ^reader = gcnew DataReader(stream->InputStream);
                   DataReaderLoadOperation ^ rop = reader->LoadAsync(1);
                   rop->Completed = gcnew AsyncOperationCompletedHandler<unsigned int>([=](IAsyncOperation<unsigned int>^ a)
                   {
                      int value = reader->ReadByte();
                      textbox_RenderList1->Text = value.ToString();
                      a->Close();
                   });
                   rop->Start();
               }
              
               a->Close();
           });
        op->Start();
    }

     

     

    Saturday, October 08, 2011 12:40 PM

Answers

  • finally, we can ignore this problem. A fact is client in Desktop software can not connect server in Metro app. But because TCP socket is bi-directional, so we can use a socket(Metro to connect Desktop) to communicate.
    • Marked as answer by Ever Chen Thursday, October 27, 2011 6:50 AM
    Thursday, October 13, 2011 4:51 AM

All replies

  • Have you been able to verify at all whether your server (the Metro style app) is accepting connections at all?

    Thanks

    Raman

    Wednesday, October 12, 2011 5:05 PM
  • You may want to take a look at the example here:

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

    This provied an example of a server written in JavaScript and will tell you the corresponding things to do in C++.

    Thanks

    Raman Sharma, Visual C++

    Wednesday, October 12, 2011 5:15 PM
  • Hello,

     

    I have duped the issue.   I tried using the checknetisolation tool but the send desktop application still throws the error "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."

    If I look at netstat -a it is listening on the designated port.

    I noticed if I turn on Win32 exceptions (debug | exceptions), I get a lot of access denied exception when setting up the callback on the Metro side. 

    In any case, please check out

    http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/895cf468-31e0-4e2f-aab1-ae90933610d1

    and

    http://stackoverflow.com/questions/7465517/how-can-a-metro-app-in-windows-8-communicate-with-a-backend-desktop-app-on-the-sa

    for the current status on IPC.

     

    thanks!

    mike

     

    Wednesday, October 12, 2011 10:27 PM
    Moderator
  • Yes, if client and server is in a metro app, they can connect for local host.
    Thursday, October 13, 2011 4:49 AM
  • finally, we can ignore this problem. A fact is client in Desktop software can not connect server in Metro app. But because TCP socket is bi-directional, so we can use a socket(Metro to connect Desktop) to communicate.
    • Marked as answer by Ever Chen Thursday, October 27, 2011 6:50 AM
    Thursday, October 13, 2011 4:51 AM
  • Hello,

    How were you able to connect to a desktop app from metro ? Wll you please elaborate.

    I tried desktop app server & desktop windows service server, But my metro app wasnt able to connect to any of them.


    • Edited by Zuberul Saturday, November 19, 2011 8:48 AM
    Saturday, November 19, 2011 5:01 AM