zoukankan      html  css  js  c++  java
  • UDP通信

    UDPServer.java

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.StringReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    public class UDPServer{
    	public static void main(String[] args) throws IOException{
    		new UDPServerThread().start();
    	}
    }
    class UDPServerThread extends Thread{
    	protected DatagramSocket socket=null;
    	protected BufferedReader in =null;
    	protected Boolean moreQuotes = true;
    	public UDPServerThread() throws IOException{
    		super();
    		socket = new DatagramSocket(4445);
    		in = new BufferedReader(new StringReader("one
    two
    three"));
    	}
    	public void run(){
    		while(moreQuotes){
    			try{
    				byte[] buf=new byte[256];
    				DatagramPacket packet=new DatagramPacket(buf,buf.length);
    				socket.receive(packet);
    				
    				String received =new String(packet.getData(),0,packet.getLength(),"utf-8");
    				System.out.println("Server received: "+received);									
    				
    				String dString = getNextQuote();
    				
    				buf=dString.getBytes("utf-8");
    				
    				InetAddress address =packet.getAddress();
    				int port =packet.getPort();
    				packet = new DatagramPacket(buf,buf.length,address,port);
    				socket.send(packet);
    			}catch(IOException e){
    				e.printStackTrace();
    				moreQuotes = false;
    			}
    		}
    		socket.close();
    	}
    	protected String getNextQuote(){
    		String returnValue=null;
    		try{
    			if((returnValue=in.readLine())==null){
    				in.close();
    				moreQuotes=false;
    				returnValue="No more quotes.Goodbye.";
    			}
    		}catch(IOException e){
    			returnValue="IOException occurred.";
    		}
    		return returnValue;
    	}
    }
    

      

    UDPClient.java

    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    class UDPClient{
    	public UDPClient(){
    		
    	}
    	public static void main(String[] args) throws IOException{
    		if(args.length!=1){
    			System.out.println("Usage:java UDPClient……");
    			return;
    		}
    		DatagramSocket socket = new DatagramSocket();
    		
    		byte[] buf =new byte[256];
    		buf[0]='O';
    		buf[1]='S';
    		InetAddress address= InetAddress.getByName(args[0]);
    		DatagramPacket packet =new DatagramPacket(buf,buf.length,address,4445);
    		socket.send(packet);
    		String send =new String(packet.getData(),0,packet.getLength(),"utf-8");
    		System.out.println("Client send: "+send);
    
    		
    		packet =new DatagramPacket(buf,buf.length);
    		socket.receive(packet);
    		
    		String received =new String(packet.getData(),0,packet.getLength(),"utf-8");
    		System.out.println("Server Say: "+received);
    		
    		socket.close();
    	}
    }
    

     输出:

  • 相关阅读:
    手把手教你进行Python虚拟环境配置
    40行代码教你利用Python网络爬虫批量抓取小视频
    用Python模拟技巧带你实现自动抽屉登录&自动点赞
    干货|Python大佬手把手带你破解哔哩哔哩网滑动验证(下篇)
    干货|Python大佬手把手带你破解哔哩哔哩网滑动验证(上篇)
    Spring 常见的事务管理、事务的传播特性、隔离级别
    Spring AOP注解
    linux 内核的futex pi-support,即pi-futex使用rt_mutex委托
    pthread的lowlevellock
    linux 内核的rt_mutex (realtime互斥体)
  • 原文地址:https://www.cnblogs.com/yuanzhenliu/p/5788957.html
Copyright © 2011-2022 走看看