【发布时间】:2015-05-11 08:30:13
【问题描述】:
我想使用 WIFI 连接从我的 android 手机 4.2(客户端)向 PC(服务器)发送一条 UDP 消息。我的手机和电脑是通过无线路由器连接的。但是没有收到从手机到手机的消息。我还成功地测试了 PC 到 PC 连接的代码。我已将互联网权限添加到 manifest.xml。如果你能帮助我,我会很高兴的。谢谢你。我已添加此权限。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
客户:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
final TextView tv = (TextView) findViewById(R.id.textView1);
final TextView tv2= (TextView) findViewById(R.id.textView2);
button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
boolean morgan= isOnline();
String s = String.valueOf(morgan);
tv.setText(s);
try{
//InetAddress ipaddress = InetAddress.getByName("localhost");
InetAddress ipaddress = InetAddress.getByName("192.168.10.11");
int port = 6500;
//byte[] buffer = new byte[1024]; // empty byte array
String msg ="hello goooooooogle"; // send this message to the server
byte [] b_array = msg.getBytes();
//on SERVER side DatagramSocket able to receive packets on 8080 port
DatagramPacket packet = new DatagramPacket(b_array, b_array.length, ipaddress, port);// DatagramPacket(byte[], byte_length, InetAddress, port_number)
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
});
}
public boolean isOnline() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 192.168.10.11");
//Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
}
catch (IOException e)
{ e.printStackTrace(); }
catch (InterruptedException e) { e.printStackTrace(); }
return false;
}
@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
{
public static void main(String args[])
{
try{
System.out.println("aaa");
byte[] inbuf = new byte[1000]; // default size
DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);
DatagramSocket socket = new DatagramSocket(6500);
socket.receive(packet);
int numBytesReceived = packet.getLength();
System.out.println(numBytesReceived);
String s = new String(inbuf);
System.out.println(s);
//System.out.println(inbuf[2]);
socket.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
【问题讨论】:
-
'/在 SERVER 端 DatagramSocket 能够在 8080 端口接收数据包'。你的意思是如果你的android客户端连接到8080一切正常?
-
您的应用程序正在崩溃,不是吗?你为什么不告诉?您将看到 logcat 中提到的异常。请告诉我们。
-
没有崩溃。它工作正常。但是服务器(PC)端没有收到数据包。我试过 8080 但不工作。
-
我认为你会有一个例外。那些 println 说什么?将 e.printStackTrace() 也放入 catch 块中并查看 logcat。
-
您确定您的网络允许 Wifi-2-Wifi 连接吗?通常这是禁用的。