您的位置:首页 > 其它

模拟10辆车载板卡发送数据

2019-05-25 16:42 585 查看

模拟10辆车载板卡发送数据,通信端口: 11111

链接:https://pan.baidu.com/s/1ZhjMZ4FmNtGJo0CItCHltw
提取码:nkz7
复制这段内容后打开百度网盘手机App,操作更方便哦

import java.io.IOException;
import java.net.InetAddress;

public class UDP_Client {

public static void main(String args[])throws IOException, InterruptedException{
InetAddress loc = InetAddress.getLocalHost();
Thread thread[] = new Thread[100];
for(int i = 0; i < 10; i++){
TestRunnable testRunnable = new TestRunnable();
testRunnable.setTries(i);
testRunnable.setLoc(loc);
thread[i] = new Thread(testRunnable);
thread[i].start();
}

}
}
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.text.DecimalFormat;

public class TestRunnable implements Runnable {

int tries = 0;                            //重发数据的次数
public void setTries(int tries) {
this.tries = tries;
}
InetAddress loc;
public void setLoc(InetAddress loc) {
this.loc = loc;
}
private static final int TIMEOUT = 5000;  //设置接收数据的超时时间
DatagramSocket ds = null;

int test_nums = 0;
int ProtoCnt = 0;
DecimalFormat df=new DecimalFormat("0000");
//数据发向本地7777端口
//客户端在9000端口监听接收到的数据
public void run() {
try {

ds = new DatagramSocket(8000 + tries);
ds.setSoTimeout(TIMEOUT);              //设置接收数据时阻塞的最长时间
while(true){
if(200 == test_nums){
test_nums = 0;
} else {
test_nums++;
}
String str_end = "Try cnt "+tries + ", Processors " + Runtime.getRuntime().availableProcessors()+"\r\n";
System.out.println(str_end);
if(0 == test_nums % 2){
ProtoCnt++;
}
String df_ProtoCnt=df.format(ProtoCnt);
String df_tries=df.format(tries);
String textdata = "test data message is sending...";
//监控板状态信息 begin
//System.out.println(socketmib.toString());
//String str_end=socketmib.toString();
//DatagramPacket dp_end =new DatagramPacket(socketmib.toByteArray(),socketmib.toByteArray().length,loc,11111);
DatagramPacket dp_end =new DatagramPacket(textdata.getBytes(),textdata.length(),InetAddress.getByName("10.18.60.172"),11111);
ds.send(dp_end);
Thread.sleep(500);
}
//tries=0;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ds.close();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: