通过USB UDP连接的Android真实设备
我试图建立与我的Android平板电脑(Nexus 10的),我与UDP通过USB电缆PC的连接。对于这个项目,我用这个code:
i try to establish a connection with my android tablet (nexus 10) and my PC with UDP over a USB cable. For this project, i use this code :'
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);
}
服务器code:
the server code:
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);
}
}
和我的电脑上的客户端code:
and the client Code on my 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();
}
我连接平板和PC通过USB,我不知道这是否是EST方式为communitace通过USB但我觉得这里面:
http://www.anddev.org/udp-networking_-_within_the_emulator-t280.html
我的问题是,我不能让我的平板电脑客户端的字符串。
我尝试在广播发送,但我都没有反应。
My problem is that i can't get the client String on my tablet. i try to send in broadcast but i have no response
请,如果有人知道如何配置网络,这将是对我很大的帮助。
Please, if somebody know how to configure the network, it will be very helpful for me.
我认为这会帮助你。
http://qtcstation.com/2011 / 03 /连接功能的Android到了-PC-过USB /
这不是UDP,但TCP连接,但它与真正的设备工作。
It's not UDP but TCP connection but it works with a real device.
如果有人有大约UDP一个更好的答案,我很感兴趣。
If somebody has a better answer about UDP, I'm interested in it.