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
我的问题是我无法在我的平板电脑上获得客户端字符串。 我试图发送广播,但我没有回应
请,如果有人知道如何配置网络,这将对我非常有用。
谢谢,我已经看到这个,但我需要做一个没有连接的通信(如在TCP中)。 – 2014-10-24 14:03:33