how to create the server & client connection between android mobile and PC
<p>I have my client socket code in my Target(android HTC mobile),
and my server socket code in PC(C++).
The server- client socket is connected and able to send messages in emulator(AVD).
but the same code does not wort in my target(android mobile).</p>

<p>Can anyone please help me?
thanks in advance.</p>

<h2>Client code:</h2>

<pre><code>public void run() { 

    Log.d("Test_socket-connection", "Connecting..."); 
    try { 
        //"10.51.02.98", 7101, "0"
        //"127.0.0.1"  80.239.242.691 "10.0.2.2"        Socket socket = new Socket("10.51.02.98", 3333); //"10.51.02.98" is my PC IP
        if(socket != null)
            Log.d("server socket not", "created");
        //socket.setSoTimeout(3000);
        try { 
            Log.d("Test_socket-connection", "sending..."); 
            PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); 
            out.println("Hi To server");    
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String str = in.readLine(); 
            if(str.length() == 0)
            {
                Toast.makeText(appcontext, "No data was received frm server", 10);
            }
            Log.d("Test_socket-connection", "received" + str);

        }
        catch(Exception e) { 
        Log.e("Test_socket-connection", "sending error:" + e); 
        Toast.makeText(appcontext, "TCP-C S: Error " + e, 10);
        } 
        finally { 
        socket.close(); 
        Toast.makeText(appcontext, "socket.close()", 10);
        } 
    }
    catch (Exception e) { 
        Log.e("Test_socket-connection", "connection error" + e);
        Toast.makeText(appcontext,"TCP-CC: Error " + e, 10);
    } 
    }   
}
</code></pre>

<h2>server code in C++ :</h2>

<pre><code>bool server()
{


        SOCKET  s, sa;
    int     b, l, on, setsock ,shtdn;
    int     bytesRecv, bytesSent;
    char    recvbuf[512];
    char    sendbuf[512] = "sent frm server";
    int     err;
    //---- Build address structure to bind to socket.--------  
    sockaddr_in channel;
    memset(&amp;channel, 0, sizeof(channel));// zerochannel 

    channel.sin_family = AF_INET; 
    channel.sin_addr.s_addr = htonl(INADDR_ANY) ;
    channel.sin_port = htons(3333);
    //--------------------------------------------------------
     WORD wVersionRequested;
         WSADATA wsaData;

    /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
        wVersionRequested = MAKEWORD(2, 2);

    err = WSAStartup(wVersionRequested, &amp;wsaData);
    if (err != 0) {
        /* Tell the user that we could not find a usable */
        /* Winsock DLL.                                  */
        printf("WSAStartup failed with error: %d\n", err);
        return false;
    }


    // ---- create SOCKET--------------------------------------
    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);    
    if (s &lt; 0) {
        printf("socket error %ld",WSAGetLastError() );
        //WSACleanup();
        return false;
    }

    setsock = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &amp;on, sizeof(on)); 
    if(setsock!=0)
    {
        printf("setsockopt error %ld", WSAGetLastError() ); 
        WSACleanup();
        return false;
    }
    //---------------------------------------------------------

    //---- BIND socket ----------------------------------------
    b = bind(s, (struct sockaddr *) &amp;channel, sizeof(channel)); 
    if (b &lt; 0) {
        printf("bind error %ld", WSAGetLastError() ); 
        WSACleanup();
        return false;
    }
    //----------------------------------------------------------

    //---- LISTEN socket ----------------------------------------
    l = listen(s, QUEUE_SIZE);                 // specify queue size 
    if (l &lt; 0) {
        printf("listen error %ld",WSAGetLastError() );
        WSACleanup();
        return false;
    }
    //-----------------------------------------------------------

    while (1) {
    //---- ACCEPT connection ------------------------------------
    sa = accept(s, 0, 0);                  // block for connection request  
    if (sa &lt; 0) {
        printf("accept error %ld ", WSAGetLastError() ); 
        shtdn = shutdown( s,SD_BOTH);
        if(shtdn &lt; 0)
        {
            printf("shutdown error %d ", WSAGetLastError() ); 
        }
        WSACleanup();
        return false;
    }
    else {
        printf("connection accepted");
    }
    //------------------------------------------------------------
    // Socket is now set up and bound. Wait for connection and process it. 

    //---- RECV bytes --------------------------------------------
    bytesRecv = recv( sa, recvbuf, 512, 0 );
    err = WSAGetLastError( );// 10057 = A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) 
    if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
      printf( "Connection Closed.\n");
      shtdn = shutdown( s,SD_BOTH);
      if(shtdn &lt; 0)
      {
        printf("shutdown error %d ", WSAGetLastError() ); 
      }
      WSACleanup();
    }
    printf( " Bytes Recv: %s \n ", recvbuf );
    bytesSent = send (sa, sendbuf, 512, 0 ); 
    if(bytesSent &lt; 0)
    {
        printf("send data error %ld ", WSAGetLastError() ); 
        WSACleanup();
        shtdn = shutdown( s,SD_BOTH);
        if(shtdn &lt; 0)
        {
           printf("shutdown error %d ", WSAGetLastError() ); 
        }
        return false;
    }

    shtdn = shutdown( s,SD_BOTH);
    if(shtdn &lt; 0)
    {
        printf("shutdown error %d ", WSAGetLastError() ); 
    }

    //Update AMDI state variable
    //SetAmdiState(atoi(recvbuf));

    closesocket( sa );
    //-------------------------------------------------------------
    }

    closesocket( s );
    WSACleanup();
    //getche();
    return 0;
}
</code></pre>
