模擬10輛車載板卡發送數據

模擬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();
            }

        }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章