public class MainActivity extends Activity {
DatagramSocket ds = null;
DatagramPacket dp = null;
Button b = null;
EditText et = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) this.findViewById(R.id.btnSned);
et = (EditText) this.findViewById(R.id.edtSend);
try
{
ds = new DatagramSocket();
}
catch (SocketException e)
{
e.printStackTrace();
}
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String s = et.getText().toString();
byte [] buf = s.getBytes();
int length = buf.length;
try
{
dp = new DatagramPacket(buf,10,InetAddress.getByName("192.168.0.1"),30000);
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
try
{
ds.send(dp);
ds.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
}
}
------解决思路----------------------
这个可以看看日志,不要在主线程(UI线程)中做网络操作,另起动线程来做。