2014-09-29 6 views
0

USB 케이블을 통해 안드로이드 타블렛 (nexus 10)과 내 PC를 UDP로 연결하려고합니다. 이 프로젝트를 위해, 나는이 코드를 사용 : 'udp 연결 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); 
    } 
} 

내 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를 연결 , 그것이 ustb 이상의 공산주의를위한 est 방법인지 나 do not는 모른다. 그러나 나는 이것을 거기에서 발견한다 : http://www.anddev.org/udp-networking_-_within_the_emulator-t280.html

제 문제는 제가 타블렛에서 클라이언트 문자열을 가져올 수 없다는 것입니다. 브로드 캐스트로 전송하려고하지만 응답이 없습니다.

누군가가 네트워크 구성 방법을 알고 있다면 제게 도움이 될 것입니다.

답변

0

나는 이것이 당신을 도울 것이라고 생각합니다.

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

이 없습니다 UDP하지만 TCP 연결의하지만 실제 장치와 함께 작동합니다.

누군가가 UDP에 대해 더 좋은 답변을 가지고 있다면, 나는 그것에 관심이 있습니다.

+1

덕분에, 나는 이미 이것을보고 있지만 TCP없이 연결없이 통신을해야합니다. –