Search code examples
androidtcpwifi

How to communicate via TCP/IP through local Wi-Fi (Android)


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.

The setup:

In Android Studio, on my PC, I have a simple TCP/IP server running on my PC.

Code below:

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()
    }
}

In another Android Studio window, I have a simple TCP/IP client.

Code below:

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<EditText>(R.id.editText_data)
        val sendButton = findViewById<Button>(R.id.button_send)

        sendButton.setOnClickListener {
            val text = dataTextField.text.toString()


            thread {
                //val socket = Socket("xxx.xxxx.xxx.xxx", 9999)        // PC's IP address, as seen from outside my house
                //val socket = Socket("127.0.0.1", 9999)            // local host (within emulator)
                //val socket = Socket("10.0.2.2", 9999)     // This is bridged to my local machine
                val socket = Socket("xxx.xxx.xxx.xxxx", 9999)     // Wi-Fi IPv4 address (from ipconfig cmd)
                socket.getOutputStream().write(text.toByteArray())
                socket.close()
            }
        }
    }
}

What works so far:

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.

What doesn’t work yet:

Now I want to run the client app on my Galaxy tablet, not in the emulator.

To do this, I need to communicate using Wi-Fi, so I can no longer use the 10.0.2.2 IP address.

My client's AndroidManifest.xml includes the following permissions:

Is it just a matter of changing the IP address to get this working over Wi-Fi?

I have tried various IP addresses:

I googled "What's my IP address" and used that - no success.

I used ipconfig to get Wi-Fi IP address, which gave me information in the following format:

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

I used the IPv4 Address - no success.

Question:

Can anyone point me in the right direction here please?

I am guessing there are a few more steps to communicating via TCP/IP over Wi-Fi from the Android tablet to my PC.

Thanks in advance

Garrett


Solution

  • Update:

    I solved this myself.

    I downloaded PingTools on my Android device and discovered that I could not ping my PC (using the IPv4 address: 192.168.1.76) on the same network.

    Both the PC and the Android device were on the same Wi-Fi network.

    I then changed the Network Profile Type for the Wi-Fi setting from Public to Private.

    enter image description here

    Then my Android device could ping my PC :)

    And of course, then, my TCP Client was successfully able to send data to my TCP Server.