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

    UDP收发消息

    udp协议使用数据报套接字,无需建立链接

    //send
    public class Udp_Send_01 {
    	public static void main(String[] args) throws IOException {
    		
    		DatagramSocket socket=new DatagramSocket();
    		
    		InetAddress add=InetAddress.getByName("localhost");
    		int port=9090;
    		String msg="你好啊";
    		DatagramPacket data=new DatagramPacket(msg.getBytes(), 0,msg.getBytes().length,add,port);
    		
    		socket.send(data);
    		
    		socket.close();
    		
    	}
    }
    //receive
    public class Udp_Receiver_01 {
    	public static void main(String[] args) throws IOException {
    		DatagramSocket socket=new DatagramSocket(9090);
    		
    		byte[] bytes=new byte[1024];
    		DatagramPacket packet=new DatagramPacket(bytes, 0,bytes.length);
    		socket.receive(packet);//阻塞接收
    		System.out.println(packet.getAddress());
    		System.out.println(new String(packet.getData(),0,packet.getData().length));
    		
    		socket.close();
    		
    	}
    }
    /*************************循环发送,接收*************************/
    public class Sender_01 {
    	public static void main(String[] args) throws IOException {
    		DatagramSocket socket=new DatagramSocket(8888);
    	
    		BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
    		
    		String data;
    		byte[] bytes;
    		
    		while(!(data=reader.readLine()).equals("bye")) {
    			
    			bytes=data.getBytes();
    			DatagramPacket pack=new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress("localhost", 6666));
    			socket.send(pack);
    		}
    		socket.close();
    
    	}
    }
    //
    public class Receiver_01 {
    	public static void main(String[] args) throws IOException {
    		DatagramSocket socket=new DatagramSocket(6666);
    		
    		byte[] bytes=new byte[1024];
    		DatagramPacket packet=new DatagramPacket(bytes, 0,bytes.length);
    		while(true) {
    			socket.receive(packet);
    			String msg=new String(packet.getData(),0,packet.getData().length);
    			if(msg.equals("bye")) {
    				break;
    			}
    			System.out.println(msg);
    			
    		}
    		socket.close();
    	
    	}
    }
    

    建立一个通用收发线程实现全双工收发

    //发送消息线程
    public class Sendmsg implements Runnable{
    	DatagramSocket socket=null;
    	BufferedReader reader=null;
    	private int fromport;
    	private String toip;
    	private int toport;
    	private String name;
    	public Sendmsg(int fromport,String toip,int toport,String name) throws SocketException, UnknownHostException {
    		// TODO Auto-generated constructor stub
    		this.fromport=fromport;
    		this.toip=toip;
    		this.toport=toport;
    		this.name=name;
    		
    		socket=new DatagramSocket(this.fromport);
    		reader=new BufferedReader(new InputStreamReader(System.in));
    		
    	}
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		String data;
    		byte[] bytes;
    		
    		try {
    			
    			while(!(data=reader.readLine()).equals("bye")) {
    				
    				bytes=(name+":"+data).getBytes();
    				DatagramPacket pack=new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress(this.toip, this.toport));
    				socket.send(pack);
    			}
    			
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			reader.close();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		socket.close();
    	}
    
    }
    //接收消息线程
    public class Receivemsg implements Runnable{
    
    	DatagramSocket socket=null;
    	private int port;//从此端口接收消息
    	public Receivemsg(int port) throws SocketException {
    		// TODO Auto-generated constructor stub
    		this.port=port;
    		socket=new DatagramSocket(this.port);
    	}
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		
    		byte[] bytes=new byte[1024];
    		DatagramPacket packet=new DatagramPacket(bytes, 0,bytes.length);
    		while(true) {
    			try {
    				socket.receive(packet);
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			String msg=new String(packet.getData(),0,packet.getData().length);
    			if(msg.equals("bye")) {
    				break;
    			}
    			System.out.println("address:"+packet.getAddress()+" "+msg);
    			
    		}
    		socket.close();
    	}
    
    }
    //新建两个类的收发线程
    public class Student {
    	public static void main(String[] args) throws SocketException, UnknownHostException {
    		new Thread(new Sendmsg(6666, "localhost", 8888, "student")).start();
    		new Thread(new Receivemsg(7777)).start();
    	}
    }
    public class Teacher {
    	public static void main(String[] args) throws SocketException, UnknownHostException {
    		new Thread(new Sendmsg(5555, "localhost", 7777, "teacher")).start();
    		new Thread(new Receivemsg(8888)).start();
    	}
    }
    
  • 相关阅读:
    UVA11039
    UVA10970大块巧克力
    UVA10970大块巧克力
    UVA10340子序列
    UVA10340子序列
    UVA10382喷水装置
    UVA10382喷水装置
    UVA10020(最小区间覆盖)
    洛谷 P2141 珠心算测验
    UVa 11292 勇者斗恶龙(The Dragon of Loowater)
  • 原文地址:https://www.cnblogs.com/zhang-han/p/14123283.html
Copyright © 2011-2022 走看看