locked
TCP connection issue RRS feed

  • Question

  • Hi,

    I am new to Windows mobile programming. I am trying to write an TCP client that executes on windows emulator and Server running on my local host.I started the server on local machine and deployed the client code on emulator. I am giving the client code below.
    I am getting "Client connection failed".

    Do I need to access Activesync to communicate with local host? Please help me.


    #include "stdafx.h"
    #include "WinClient.h"
    #include <winsock2.h>
    #include <ws2tcpip.h>

    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
    {
        HRESULT hr = E_FAIL;
        WSADATA wsaData;
        SOCKET mySocket;

        INT bytesSent = SOCKET_ERROR;
        INT bytesRecv = SOCKET_ERROR;
        INT iRet = 0;

        CHAR recvbuf[10] = "Dummy";
        CHAR sendBuff[10] = "Hello";

        // Initialize Winsock.
        iRet = WSAStartup(MAKEWORD(2,2), &wsaData);
        if(NO_ERROR != iRet)
        {
            hr = E_FAIL;
            goto Exit;
        }

        BOOL fSockInitialized = TRUE;

        // Create a socket.
        mySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(INVALID_SOCKET == mySocket)
        {
            iRet = WSAGetLastError();
            hr = HRESULT_FROM_WIN32(iRet);
            goto Exit;
        }

        sockaddr_in clientService;
        clientService.sin_family = AF_INET;
        clientService.sin_addr.s_addr = inet_addr("10.0.81.95");
        clientService.sin_port = htons( 5555 );

        // Connect to the Server
        iRet = connect(mySocket, (SOCKADDR*) &clientService, sizeof(clientService));
        if(SOCKET_ERROR == iRet)
        {
            iRet = WSAGetLastError();
            MessageBox(NULL,_T("Client connection failed"),_T(""),MB_OK);
            hr = HRESULT_FROM_WIN32(iRet);
            goto Exit;
        }

        MessageBox(NULL,_T("Connection Succeeded.. Sending data"),_T(""),MB_OK);
       
        bytesSent = send(mySocket, sendBuff, sizeof(sendBuff), 0);
        if(SOCKET_ERROR == bytesSent)
        {
            iRet = WSAGetLastError();
            MessageBox(NULL,_T("Unable to send the data"),_T(""),MB_OK);
            hr = HRESULT_FROM_WIN32(iRet);
            goto Exit;
        }
        MessageBox(NULL,_T("Message sent.. Waiting to listen"),_T(""),MB_OK);
        // receive a 10bytes buffer including the string "Done"
        bytesRecv = recv(mySocket, recvbuf, sizeof(recvbuf), 0);
        if(SOCKET_ERROR == bytesRecv)
        {
            iRet = WSAGetLastError();
            hr = HRESULT_FROM_WIN32(iRet);
            goto Exit;
        }
        LPCWSTR string = _T("MyText");
        MessageBox(NULL,string,_T(""),MB_OK);
        Exit:
        if (mySocket != NULL)
        {
            closesocket(mySocket);
        }
       
        if (fSockInitialized)
        {
            WSACleanup();
        }
        return SUCCEEDED(hr);
    }



    Tuesday, December 2, 2008 7:48 AM

Answers

  • This forums deals with issues directly related to the .NET Compact Framework (.NET CF). Since your question is not related to .NET CF, it's off-topic here.

     

    Please select a proper forum based on the platform and the language you are using.  In this case I woud suggest you ask the quesion in the native C++ forum for better response. This thread is closed as off-topic.

     

    Friday, December 5, 2008 7:44 AM

All replies

  • I was able to connect to my local server. Howevre I am experiencing problem in sending the data from emulator to local host. Send call from the client is succeding but the server is not getting the data. Please help me in this.
    Wednesday, December 3, 2008 5:30 AM
  • This forums deals with issues directly related to the .NET Compact Framework (.NET CF). Since your question is not related to .NET CF, it's off-topic here.

     

    Please select a proper forum based on the platform and the language you are using.  In this case I woud suggest you ask the quesion in the native C++ forum for better response. This thread is closed as off-topic.

     

    Friday, December 5, 2008 7:44 AM