Android: "Socket Not Connected" Exception
<p>I'm having some issues with sending data from an Android client to a desktop server over TCP via wifi. The emulator works fine, but on the actual phone, the connection cannot be established. A "socket not connected" exception was thrown.</p>

<p>I have attached my code below. Any help please? Many thanks!</p>

<p>// CODE inside an Activity</p>

<pre><code>public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
   case R.id.send:
           sendMessage();
           return true;
   /// ...other items
   }
}

private void sendMessage() {
   String serverAddr = "18.xxx.xx.xxx";
   Socket socket = null;
   try {
           socket = new Socket(serverAddr, 4444);  // EXCEPTION HAPPENS HERE
   } catch (Exception e) {
           //show exception on screen
   }

   String message = "some message";
   try {
           PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
           out.println(message);
   } catch (Exception e) {
           //show exception on screen
   } finally {
           socket.close();
   }
}
</code></pre>
