2014-09-29 122 views
0

我嘗試建立一個連接與我的android平板電腦(nexus 10)和我的電腦與UDP通過USB電纜。對於這個項目,我用這個代碼:」udp連接android真正的設備通過USB

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

,並在我的電腦上的客戶端代碼:

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

} 

我連接平板電腦和PC通​​過USB ,我不知道這是否是最好的usb communitace方式,但我找到了這裏: http://www.anddev.org/udp-networking_-_within_the_emulator-t280.html

我的問題是我無法在我的平板電腦上獲得客戶端字符串。 我試圖發送廣播,但我沒有迴應

請,如果有人知道如何配置網絡,這將對我非常有用。

回答