80,351
社区成员
发帖
与我相关
我的任务
分享
class ClientClass extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
try {
InetAddress serverAddress=InetAddress.getByName("255.255.255.255");
int messageCount=0;
DatagramSocket socket=new DatagramSocket();
byte[] buffer=localIpAddress.getBytes();
DatagramPacket packet=new DatagramPacket(buffer,buffer.length,serverAddress,4567);
while (true) {
try {
socket.send(packet);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("Send Data:" + new String(packet.getData()));
Toast.makeText(MainActivity.this, "数据已发送!", Toast.LENGTH_SHORT).show();
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
messageCount++;
buffer = (localIpAddress + messageCount).getBytes();
packet.setData(buffer,0,buffer.length);
}
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(MainActivity.this, "数据未能发送!", Toast.LENGTH_SHORT).show();
}
super.run();
}
}