You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无netcat环境下,测试adb创建的TCP通道并实现安卓向主机传文本求助

Got it, let's work through how to send text from your Android device to your host machine without relying on netcat. Given your existing port forwarding setup (adb forward tcp:5000 tcp:6000 and adb reverse tcp:6000 tcp:7000), here are two practical, easy-to-implement solutions:

1. Use Python Socket Scripts (No Extra Tools Required)

Python is cross-platform, so it's a perfect stand-in for netcat here. You can run scripts on both your host and Android (via Termux if you don't have a dev environment set up).

Host Side: Set Up a Receiver

Create a file named host_receiver.py with this code—this will listen on port 7000 (your reverse-forwarded port) for incoming messages:

import socket

HOST = '0.0.0.0'  # Listen on all network interfaces
PORT = 7000

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    print(f"Waiting for Android messages on port {PORT}...")
    conn, addr = s.accept()
    with conn:
        print(f"Connected to Android device at {addr}")
        while True:
            data = conn.recv(1024)
            if not data:
                break
            print(f"Received: {data.decode('utf-8').strip()}")

Run it with:

python host_receiver.py

Android Side: Send Messages

If you have Termux installed on your Android device:

  1. First install Python if you haven't:
    pkg install python -y
    
  2. Create a file named android_sender.py:
    import socket
    
    HOST = '127.0.0.1'  # Local device address
    PORT = 6000  # Maps to host's port 7000 via adb reverse
    
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        print("Connected to host—type messages to send (enter 'exit' to quit):")
        while True:
            message = input("> ")
            if message.lower() == 'exit':
                break
            s.sendall(message.encode('utf-8') + b'\n')
    
  3. Run the script:
    python android_sender.py
    

Now type any message in the Termux terminal, and it'll show up in your host's terminal running the receiver script.

2. Android App Code (For App Developers)

If you're building an Android app, you can add a simple TCP client to send messages directly from your app. Here's a Kotlin example:

import java.io.OutputStream
import java.net.Socket

fun sendMessageToHost(message: String) {
    // Run network operations on a background thread (required for Android)
    Thread {
        try {
            // Connect to local port 6000—adb reverse forwards this to host's 7000
            val socket = Socket("127.0.0.1", 6000)
            val outputStream: OutputStream = socket.getOutputStream()
            // Send the message (add a newline for readability on the host)
            outputStream.write((message + "\n").toByteArray())
            outputStream.flush()
            socket.close()
        } catch (e: Exception) {
            e.printStackTrace()
            // Handle errors (e.g., no adb connection, port issues)
        }
    }.start()
}

Call this function with your desired message, and make sure your host is running the Python receiver script from the first solution.

Quick Notes to Avoid Issues

  • Double-check your adb forwarding commands are active (re-run them if needed):
    adb forward tcp:5000 tcp:6000
    adb reverse tcp:6000 tcp:7000
    
  • Ensure your Android device has adb debugging enabled and is connected to your host.
  • If you hit port conflicts, swap out the port numbers in both the commands and scripts.

内容的提问来源于stack exchange,提问作者user3122648

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:18:39