Android TCP智能手机到PC,带USB,无wifi

Android TCP智能手机到PC,带USB,无wifi,android,Android,嗨,我管理了一些代码,但它不工作 我想通过USB控制从PC向Android发送消息,而不是通过WIFI 我试过这个密码 但这给了我一个错误 07-13 20:08:28.530: W/System.err(8990): java.net.ConnectException: localhost/127.0.0.1:383 - Connection refused 我已通过亚洲开发银行完成港口转运 用于桌面 import java.io.DataInputStream; import java.

嗨,我管理了一些代码,但它不工作

我想通过USB控制从PC向Android发送消息,而不是通过WIFI

我试过这个密码

但这给了我一个错误

07-13 20:08:28.530: W/System.err(8990): java.net.ConnectException: localhost/127.0.0.1:383 - Connection refused
我已通过亚洲开发银行完成港口转运

用于桌面

 import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class comunicate {

 public static void main(String[] args){
  ServerSocket serverSocket = null;
  Socket socket = null;
  DataInputStream dataInputStream = null;
  DataOutputStream dataOutputStream = null;

  try {
   serverSocket = new ServerSocket(383);
   System.out.println("Listening :8888");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  while(true){
   try {
    socket = serverSocket.accept();
    dataInputStream = new DataInputStream(socket.getInputStream());
    dataOutputStream = new DataOutputStream(socket.getOutputStream());
    System.out.println("ip: " + socket.getInetAddress());
    System.out.println("message: " + dataInputStream.readUTF());
    dataOutputStream.writeUTF("Hello!");
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   finally{
    if( socket!= null){
     try {
      socket.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }

    if( dataInputStream!= null){
     try {
      dataInputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }

    if( dataOutputStream!= null){
     try {
      dataOutputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }
  }
 }
}
移动电话

package demo.app.org;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class DemoAppActivity extends Activity {

EditText textOut;
TextView textIn;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     textOut = (EditText)findViewById(R.id.textout);
     Button buttonSend = (Button)findViewById(R.id.send);
     textIn = (TextView)findViewById(R.id.textin);
     buttonSend.setOnClickListener(buttonSendOnClickListener);
 }

 Button.OnClickListener buttonSendOnClickListener
 = new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
 // TODO Auto-generated method stub
 Socket socket = null;
 DataOutputStream dataOutputStream = null;
 DataInputStream dataInputStream = null;

 try {
  socket = new Socket("localhost", 383);
  dataOutputStream = new DataOutputStream(socket.getOutputStream());
  dataInputStream = new DataInputStream(socket.getInputStream());
  dataOutputStream.writeUTF(textOut.getText().toString());
  textIn.setText(dataInputStream.readUTF());
 } catch (UnknownHostException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 finally{
  if (socket != null){
   try {
    socket.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  if (dataOutputStream != null){
   try {
    dataOutputStream.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  if (dataInputStream != null){
   try {
    dataInputStream.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}};
}

这不是通过wifi进行tcp通信的代码吗?我认为除了logcat之外,您不能通过USB与android通信。

这不是通过wifi进行tcp通信的代码吗?我认为除了logcat,你不能通过USB与android通信。

是的,我正在尝试使用USB使其工作,我认为没有网络连接WIFI,或网络我们无法通信,但我在一些论坛上读到,通过ADB端口转发是可以的,是的,我正在尝试使用USB使其工作,我认为没有网络连接WIFI,或者网络我们无法沟通,但我在一些论坛上读到有可能通过ADB端口转发保留1024以下的端口。您是否查看了
ADB帮助
以获取任何线索?是的,但ADB-h和帮助,但目前没有解决方案顺便问一下,是否可以通过USB进行沟通,或者您可以给我一个没有wifi或net
ADB USB的示例-重新启动在USB上侦听的adbd守护进程,或…`adb forward-forward socket connections forward规范是以下规范之一:tcp:localabstract:localreserved:localfilesystem:`保留1024以下的端口。您检查了吗
adb帮助
有什么线索吗?是的,但是adb-h和帮助,但是目前没有解决方案。顺便问一下,是否可以与USB通信,或者您可以举一个没有wifi或网络的例子
adb USB-重新启动adbd守护进程,监听USB
,或者……`adb转发-转发套接字连接转发规范是以下规范之一:tcp:localabstract:localreserved:localfilesystem:`