title: How to communicate via TCP/IP through local Wi-Fi (Android)
body: <p>I am trying to get a TCP/IP client on my Android tablet to communicate with a TCP/IP server on my laptop.
  The tablet needs to use the local Wi-Fi.</p>
  <p><strong>The setup:</strong></p>
  <p>In Android Studio, on my PC, I have a simple TCP/IP server running on my PC.</p>
  <p>Code below:</p>
  <pre><code>package com.example.tcp_server_01
  
  import java.net.ServerSocket
  
  fun main()
  {
      val server = ServerSocket(9999)
      while (true)
      {
          val socket = server.accept()
          println(String(socket.getInputStream().readBytes()))
          socket.close()
      }
  }
  </code></pre>
  <p>In another Android Studio window, I have a simple TCP/IP client.</p>
  <p>Code below:</p>
  <pre><code>package com.example.tcp_client_01
  
  import androidx.appcompat.app.AppCompatActivity
  import android.os.Bundle
  import android.widget.Button
  import android.widget.EditText
  import java.net.Socket
  import kotlin.concurrent.thread
  
  class MainActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          val dataTextField = findViewById&lt;EditText&gt;(R.id.editText_data)
          val sendButton = findViewById&lt;Button&gt;(R.id.button_send)
  
          sendButton.setOnClickListener {
              val text = dataTextField.text.toString()
  
  
              thread {
                  //val socket = Socket(&quot;xxx.xxxx.xxx.xxx&quot;, 9999)        // PC's IP address, as seen from outside my house
                  //val socket = Socket(&quot;127.0.0.1&quot;, 9999)            // local host (within emulator)
                  //val socket = Socket(&quot;10.0.2.2&quot;, 9999)     // This is bridged to my local machine
                  val socket = Socket(&quot;xxx.xxx.xxx.xxxx&quot;, 9999)     // Wi-Fi IPv4 address (from ipconfig cmd)
                  socket.getOutputStream().write(text.toByteArray())
                  socket.close()
              }
          }
      }
  }
  </code></pre>
  <p><strong>What works so far:</strong></p>
  <p>Running the client in the Android Studio's emulator works.
  The client uses IP address 10.0.2.2 to connect to the server, since both server and client are running on the same machine (and I read somewhere that the 10.0.2.2 is bridged from the emulator to the local machine).
  So, in the Client app, I can enter text in an EditText field, and when the button is pressed, the data is sent to, and received successfully by, the server.
  All good so far.</p>
  <p><strong>What doesn’t work yet:</strong></p>
  <p>Now I want to run the client app on my Galaxy tablet, not in the emulator.</p>
  <p>To do this, I need to communicate using Wi-Fi, so I can no longer use the 10.0.2.2 IP address.</p>
  <p>My client's AndroidManifest.xml includes the following permissions:</p>
  
  
  <p>Is it just a matter of changing the IP address to get this working over Wi-Fi?</p>
  <p>I have tried various IP addresses:</p>
  <p>I googled &quot;What's my IP address&quot; and used that - no success.</p>
  <p>I used ipconfig to get Wi-Fi IP address, which gave me information in the following format:</p>
  <pre><code>Wireless LAN adapter Wi-Fi:
    Connection-sepcific DNS Suffix: xxxx:xxxx:xxxx:xxxx:xxxx%yy
    Link-local IPv6 Address: xxx.xxx.xxx.xxx
    IPv4 Address: xxx.xxx.xxx.xxx
    Subnet Mask: xxx.xxx.xxx.xxx
    Default Gateway: xxx.xxx.xxx.xxx
  </code></pre>
  <p>I used the IPv4 Address - no success.</p>
  <p><strong>Question:</strong></p>
  <p>Can anyone point me in the right direction here please?</p>
  <p>I am guessing there are a few more steps to communicating via TCP/IP over Wi-Fi from the Android tablet to my PC.</p>
  <p>Thanks in advance</p>
  <p>Garrett</p>