• udp 局域网群聊


    UDP:
    无连接协议
    udp协议发送数据,用的是数据报包的形式。(64KB以内)
     
     
    发送端:
    1.定义发送的datagramsocket对象,发送端可以不用定义端口
    2.定义封装数据包datagrampacket
    3.发送数据包
    4.关闭资源
     
    //InetAddress.getByName("192.168.3.255")必须是统一的网段
     
    public class UdpSendThread implements Runnable {
    
    	DatagramSocket ds;
    	byte[] buf;
    
    	public UdpSendThread(DatagramSocket ds) {
    		this.ds = ds;
    	}
    
    	public UdpSendThread(DatagramSocket ds, String str) {
    
    		this.ds = ds;
    		this.buf = str.getBytes();
    	}
    
    	@Override
    	public void run() {
    
    		String brStr;
    		try {
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "utf-8"));
    	
    			
    				while ((brStr = br.readLine()) != null) {
    
    					if(brStr.startsWith("@")){
    						
    					}
    				
    					DatagramPacket dp = new DatagramPacket(brStr.getBytes(), brStr.getBytes().length,
    							InetAddress.getByName("192.168.3.255"), 9000);
    
    					ds.send(dp);
    					if (brStr.equals("over")) {
    						break;
    					}
    
    				}
    			
    		
    			//br.close();
    			ds.close();
    		} catch (UnknownHostException | UnsupportedEncodingException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    

      

    接收端:
    1、建立DatagramSocket对象
    2、定义一个空的数据包
    3、接受数据包
    4、解析数据包
    5、关闭资源
    public class UdpRececiveThread extends Thread {
    
    	DatagramSocket ds;
    	
    	public UdpRececiveThread(DatagramSocket ds) {
    		
    		this.ds = ds;
    	}
    	
    	@Override
    	public void run() {
    		
    		while(true){
    			byte[] buf = new byte[1024*1024];
    			DatagramPacket dp = new DatagramPacket(buf, buf.length);
    			
    			try {
    				ds.receive(dp);
    				String ip = dp.getAddress().getHostAddress();
    				String content = new String(dp.getData(),0,dp.getLength());
    				if(content.equals("over")){
    					System.out.println(ip+"退出");
    				}
    				
    				System.out.println(ip+":"+content);
    				
    				
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			
    		}
    	}
    }
    

      测试类:

    	public static void main(String[] args) throws SocketException {
    		
    		DatagramSocket sendDs = new DatagramSocket();
    		DatagramSocket reDs = new DatagramSocket(9000);
    	    new Thread(new UdpSendThread(sendDs)).start();;
    		new UdpRececiveThread(reDs).start();
    	}
    

      

  • 相关阅读:
    40种为网页设计师准备的高品质和免费的多媒体PSD文件
    GNU make manual 翻译( 一百一十一)
    对 makefile 中 .ONESHELL的学习体会
    GNU make manual 翻译( 一百一十四)
    对 makefile 中 .NOTPARALLE 的学习体会
    静态模式规则的例子
    GNU make manual 翻译( 一百一十二)
    GNU make manual 翻译( 一百一十五)
    对 makefile 中 .EXPORT_ALL_VARIABLES 的学习体会
    如何应对 Another app is currently holding the yum lock;
  • 原文地址:https://www.cnblogs.com/ShengXi-1994/p/5581109.html
Copyright © 2020-2023  润新知