简介

RT

code

server


package com.kuang;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by lee on 2021/4/1.
 */
public class UdpServer {
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Runnable r1 = new Runnable() { 	// 发送
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    DatagramSocket ds = new DatagramSocket(9999);
                    String s = null;
                    System.out.print("输入:");
                    while ((s = br.readLine()) != null) {
                        DatagramPacket dp = new DatagramPacket(s.getBytes(), s.getBytes().length,
                                InetAddress.getByName("127.0.0.1"), 10000);//广播
                        ds.send(dp);
                        if(s.equals("quit")) {
                            ds.close();
                            break;
                        }
//						System.out.print("输入:");
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };

        Runnable r2=new Runnable() {	//接收
            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    DatagramSocket ds=new DatagramSocket(10001);
                    byte[] buff=new byte[1024];
                    DatagramPacket dp=new DatagramPacket(buff,buff.length);
                    while(true) {
                        ds.receive(dp);
                        String s = new String(dp.getData(),0,dp.getLength());
                        String ip=dp.getAddress().getHostAddress();
                        int port=dp.getPort();
                        System.out.println("消息:"+ip+"   "+port+" : "+s);
                        if(s.equals("quit")) {
                            ds.close();
                            break;
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };

        ExecutorService es= Executors.newFixedThreadPool(2);
        es.submit(r1);
        es.submit(r2);

    }//main
}

client

package com.kuang;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by lee on 2021/4/1.
 */
public class UdpClient {
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Runnable r1 = new Runnable() { 	// 发送
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    DatagramSocket ds = new DatagramSocket(10002);
                    String s = null;
                    System.out.print("输入:");
                    while ((s = br.readLine()) != null) {
                        DatagramPacket dp = new DatagramPacket(s.getBytes(), s.getBytes().length,
                                InetAddress.getByName("127.0.0.1"), 10001);//广播
                        ds.send(dp);
                        if(s.equals("quit")) {
                            ds.close();
                            break;
                        }
//						System.out.print("输入:");
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };

        Runnable r2=new Runnable() {	//接收
            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    DatagramSocket ds=new DatagramSocket(10000);
                    byte[] buff=new byte[1024];
                    DatagramPacket dp=new DatagramPacket(buff,buff.length);
                    while(true) {
                        ds.receive(dp);
                        String s = new String(dp.getData(),0,dp.getLength());
                        String ip=dp.getAddress().getHostAddress();
                        int port=dp.getPort();
                        System.out.println("消息:"+ip+"   "+port+" : "+s);
                        if(s.equals("quit")) {
                            ds.close();
                            break;
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };

        ExecutorService es= Executors.newFixedThreadPool(2);
        es.submit(r1);
        es.submit(r2);

    }//main
}


















posted on 2021-04-01 21:07  HDU李少帅  阅读(126)  评论(0编辑  收藏  举报