Android Socket Connectio Refused
<p>I am having trouble connecting two android phones via socket i am using android debug bridge to connect the phones to my PC from which i can launch the emulator on the phones.</p>

<p>I launch the server on one phone and try to connect with the other however i get the following error on my client side:</p>

<pre><code> W/System.err: java.net.ConnectException: failed to connect to /192.168.49.1 (port 8080): connect failed: ECONNREFUSED
</code></pre>

<p>Server</p>

<pre><code>private class SocketServerThread extends Thread {
    static final int SocketServerPORT = 8080;

@Override
public void run() {
    try {
        serverSocket = new ServerSocket(SocketServerPORT);
        Log.d("Quiz", "Server creation connection success");
        Log.d("Quiz", "Server local port "+serverSocket.getLocalPort());
        Log.d("Quiz", "Server local port "+serverSocket.getInetAddress());

        HostingScreen.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
            }
        });

        while (true) {
            Socket socket = serverSocket.accept();
            count++;
            message += "#" + count + " from " + socket.getInetAddress()
                    + ":" + socket.getPort() + "\n";

            HostingScreen.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    msg.setText(message);
                }
            });

            SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(
                    socket, count);
            socketServerReplyThread.run();

        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
</code></pre>

<p>Client</p>

<pre><code> @Override
protected Object doInBackground(Object[] params) {
    Socket socket = null;
    try {
        socket = new Socket(host,port);
        Log.d("Quiz", "Client socket success");
        connected = true;
        while (connected) {
            try {
                Log.d("Quiz", "Sending command");
                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                        .getOutputStream())), true);
                // where you issue the commands
            } catch (Exception e) {
            }
        }
        socket.close();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        if(socket != null){
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
</code></pre>

<p>The port is hard coded to port 8080 for both the server and the client for the time being, the host address is found when i make a connection between the phones by using the following code:</p>

<p>Connection</p>

<pre><code>        private void onConnectionChanged(Intent intent) {
        final NetworkInfo netInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
        final WifiP2pInfo p2pInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO);
        isConnected = netInfo.isConnected();
        if (isConnected) {
            //Get host connection info
            wifiMgr.requestConnectionInfo(channel, new ConnectionInfoListener() {

                @Override
                public void onConnectionInfoAvailable(final WifiP2pInfo info) {
                    wifiMgr.requestGroupInfo(channel, new GroupInfoListener() {
                        @Override
                        public void onGroupInfoAvailable(WifiP2pGroup group) {
                            if (group == null)
                                return;
                            WiFiP2P.this.group = group;
                            Log.d(TAG, "Wifi p2p connection group is  " + group.getNetworkName());
                            Log.d(TAG, "Group size " + group.getClientList().size());
                            fireOnConnectionSucceed(group.getNetworkName(), group.getPassphrase());
                            //create client if not host
                            if(info.isGroupOwner) {
                                Client client = new Client(8080, p2pInfo.groupOwnerAddress.getHostAddress());
                                client.execute();
                                Log.d(TAG, "Client launch success");
                                Log.d(TAG, "Host address " + p2pInfo.groupOwnerAddress.getHostAddress());
                            }
                        }
                    });
                    if (isConnected &amp;&amp; !info.isGroupOwner) {
                    } else {
                        startDiscovery();
                    }
                }
            });

        } else {
            group = null;
            fireOnConnectionLost();
        }
    }
</code></pre>

<p>I check to see if the person connected is the host or not, if not i launch the client passing the port and host address to the client which is an Aysnc task where the socket to connect is created.</p>

<p>The error occurs in the client when </p>

<pre><code> socket = new Socket(host,port);
</code></pre>

<p>is used causing the error stated.</p>

<p>Any ideas as to what the problem could be? They both connect to each other over WiFi but when i try to connect to the server socket it fails. </p>

<p>Thanks in advance.</p>

<p>Edit:
To clear somethings up, i use the adb to get the app from my computer onto my phone from which i can launch the app. </p>

<p>I am connecting both phones using wifimanager i need to find which of the phones created the wifi group and then that person is the host of the server to which i can connect the problem arises when i try to launch a socket to connect to the host using the host address from the connect info.</p>