udp通过USB连接android真实设备

时间:2014-09-29 09:23:24

标签: android networking udp usb pc

我尝试通过USB线与我的Android平板电脑(nexus 10)和我的PC与UDP建立连接。对于这个项目,我使用这个代码:'

public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button connectButton = (Button) findViewById(R.id.connect_button);
    connectButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            /* Kickoff the Server, it will
             * be 'listening' for one client packet */
            new Thread(new Server()).start();
            /* GIve the Server some time for startup */
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) { }


        }           
    });     
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

服务器代码:

public class Server implements Runnable {
public static final String SERVERIP = "127.0.0.1"; // 'Within' the emulator!
public static final int SERVERPORT = 4444;

@Override
public void run() {
    try {
        /* Retrieve the ServerName */
        InetAddress serverAddr = InetAddress.getByName(SERVERIP);
        Log.d("UDP", "S: Connecting...");
        /* Create new UDP-Socket */
        DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr);

        /* By magic we know, how much data will be waiting for us */
        byte[] buf = new byte[17];
        /* Prepare a UDP-Packet that can
        * contain the data we want to receive */
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        Log.d("UDP", "S: Receiving...");

            /* Receive the UDP-Packet */
        socket.receive(packet);
        Log.d("UDP", "S: Received: '" + new String(packet.getData()) +"'");
        Log.d("UDP", "S: Done.");
    } catch (Exception e) {
            Log.e("UDP", "S: Error", e);
    }
}

和我PC上的客户端代码:

public class Client implements Runnable {
DatagramSocket socket= null;
 public static final String SERVERIP = "127.0.0.1"; // 'Within' the emulator!
 public static final int SERVERPORT = 4444;
@Override
public void run() {
    try {
        // Retrieve the ServerName
        InetAddress serverAddr =InetAddress.getByName(SERVERIP);

        System.out.println("C: Connecting...");
        /* Create new UDP-Socket */
         socket = new DatagramSocket();

        /* Prepare some data to be sent. */
        byte[] buf = ("Hello from Client").getBytes();

        /* Create UDP-packet with
         * data & destination(url+port) */
        DatagramPacket packet = new DatagramPacket(buf, buf.length,     serverAddr, SERVERPORT);
        System.out.println("C: Sending: '" + new String(buf) + "'");

        /* Send out the packet */
        socket.send(packet);
        System.out.println("C: Sent.");
        System.out.println("C: Done.");
    } catch (Exception e) {
        System.out.println("C: Error:"+ e);
    }
}

public static void main( String args[] ){
    // Kickoff the Client
    new Thread(new Client()).start();

}

我通过USB连接平板电脑和PC,我不知道它是否是通过usb的通信方式,但我发现: http://www.anddev.org/udp-networking_-_within_the_emulator-t280.html

我的问题是我无法在平板电脑上获取客户端字符串。 我尝试发送广播,但我没有回应

如果有人知道如何配置网络,请对我有用。

1 个答案:

答案 0 :(得分:0)

我认为这会对你有帮助。

http://qtcstation.com/2011/03/connecting-android-to-the-pc-over-usb/

它不是UDP而是TCP连接,但它适用于真实设备。

如果有人对UDP有更好的答案,我对此感兴趣。