zoukankan      html  css  js  c++  java
  • 网络编程--UDP通讯

    UTP传输

    public class Send1 {
        public static void main(String[] args) throws Exception {
            Scanner sc=new Scanner(System.in);
            DatagramSocket socket=new DatagramSocket();//码头
            while(true){
            String str=sc.nextLine();
            if("quit".equals(str)){
                break;
            }
            DatagramPacket packet=new DatagramPacket(str.getBytes(),str.length(), InetAddress.getByName("127.0.0.1"),6666);//集装箱
            socket.send(packet);
            }
            socket.close();
        }
    }
    class Receive{
        public static void main(String[] args) throws IOException {
            DatagramSocket socket=new DatagramSocket(6666);
            DatagramPacket packet=new DatagramPacket(new byte[1024],1024);//接受长度设置为1024
            while (true){
                socket.receive(packet);
                byte[] arr=packet.getData();
                int len=packet.getLength();                         //获取有效字节个数
                String ip=packet.getAddress().getHostAddress();     //获取IP地址
                int port = packet.getPort();                        //获得端口号
                System.out.println(new String(arr,0,len)+" "+"IP:"+ip+" "+"port:"+port);
            }
    
        }
    }
    

    使用多线程在同一窗口下完成

    	public class Demo3_MoreThread {
    
    		/**
    		 * @param args
    		 */
    		public static void main(String[] args) {
    			new Receive().start();
    			
    			new Send().start();
    		}
    	
    	}
    
    	class Receive extends Thread {
    		public void run() {
    			try {
    				DatagramSocket socket = new DatagramSocket(6666);					//创建socket相当于创建码头
    				DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);	//创建packet相当于创建集装箱
    				
    				while(true) {
    					socket.receive(packet);												//接收货物
    					byte[] arr = packet.getData();
    					int len = packet.getLength();
    					String ip = packet.getAddress().getHostAddress();
    					System.out.println(ip + ":" + new String(arr,0,len));
    				}
    			} catch (IOException e) {
    				
    				e.printStackTrace();
    			}
    		}
    	}
    
    	class Send extends Thread {
    		public void run() {
    			try {
    				DatagramSocket socket = new DatagramSocket();		//创建socket相当于创建码头
    				Scanner sc = new Scanner(System.in);
    				
    				while(true) {
    					String str = sc.nextLine();
    					if("quit".equals(str))
    						break;
    					DatagramPacket packet = 							//创建packet相当于创建集装箱
    							new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);
    					socket.send(packet);			//发货
    				}
    				socket.close();
    			}  catch (IOException e) {
    				
    				e.printStackTrace();
    			}
    		}
    	}
  • 相关阅读:
    RabbitMQ第一次不能正常读取第二次正常的问题
    ng跳转映射,被阿里云的云盾拦截,提示备案问题分析
    Java读取excel表,getPhysicalNumberOfCells()和getLastCellNum区别
    IDEA同步上传lua代码,方便开发。
    redis安装
    面试感悟----一名3年工作经验的程序员应该具备的技能
    SQL Server的case when用法
    SQL Server常用函数使用方法(学习)
    Openresty编写Lua代码一例
    Nginx的配置文件nginx.conf解析
  • 原文地址:https://www.cnblogs.com/PoetryAndYou/p/11196744.html
Copyright © 2011-2022 走看看