多线程实现网络UDP

注意在实现过程中 用的是 内部类 (用来对作为通信的接受端)
整个过程中 只能用 一个 DatagramSocket ds 对象 这样才能对
如果不是同一个 ds 就不能实现交互 所以在 内部类 时 定义一个 私有的ds 作为构造函数的参数
用来在 发送 数据时 还可以同时 接受数据 在同一个 ds 对象 下;

相互之间 发送 特定消息可结束线程

import java.io.IOException;
import java.net.;
import java.util.
;

public class ComputerOne {

public static final String IP = "192.168.43.170" ;
public static final int S_PORT = 8888;
public static final int R_PORT = 9999;

public static void main(String[] args) {
	new ComputerOne().UdpStart();
}

private void UdpStart() {
	DatagramSocket ds = null;
	
	try {
		ds = new DatagramSocket(S_PORT);
		
		Scanner sc = new Scanner(System.in);
		
		Receive r = new Receive(ds);
		
		new Thread(r).start();
		
		while(true) {
			
			System.out.println("输入一个发送的语句:");
			
			String msg = sc.next().trim();
			
			byte[] buf = msg.getBytes();
			
			InetSocketAddress ins = new InetSocketAddress(IP , R_PORT);
			
			DatagramPacket dp = new DatagramPacket(buf , 0 ,buf.length , ins);
			
			ds.send(dp);
			
		}
	} catch (SocketException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}finally {
		if(ds != null) {
			ds.close();
		}
	}
}
class Receive implements Runnable {
	public static final int R_PORT = 9999;
	private DatagramSocket ds = null;
	public Receive(DatagramSocket ds) {
		this.ds = ds;
	}
	@Override
	public void run() {
		
			
			
			try {
				
				
				while(true) {
					byte[] buf = new byte[1024];
					
					DatagramPacket dp = new DatagramPacket(buf , 0 , buf.length);
					
					ds.receive(dp);
					
					String str =new String(buf , 0 , buf.length);
					
					System.out.println("ComputerOne----->"+ str);
					}
				
			} catch (SocketException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally {
				if(ds != null) {
					ds.close();
				}
			}
			
		}
	}

}

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.Scanner;

public class ComputerTwo {
public static final String IP = “192.168.43.170” ;
public static final int S_PORT = 9999;
public static final int R_PORT = 8888;
public static void main(String[] args) {
new ComputerTwo().UdpStart();
}
private void UdpStart() {
DatagramSocket ds = null;

	try {
		ds = new DatagramSocket(S_PORT);
		
		Scanner sc = new Scanner(System.in);
		
		Receive2 r = new Receive2(ds);
		
		new Thread(r).start();
		
		while(true) {
			
			System.out.println("输入一个发送的语句:");
			
			String msg = sc.next().trim();
			
			byte[] buf = msg.getBytes();
			
			InetSocketAddress ins = new InetSocketAddress(IP , R_PORT);
			
			DatagramPacket dp = new DatagramPacket(buf , 0 ,buf.length , ins);
			
			ds.send(dp);
			
		}
	} catch (SocketException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}finally {
		if(ds != null) {
			ds.close();
		}
	}
}

}

class Receive2 implements Runnable {
public static final int R_PORT = 8888;
private DatagramSocket ds = null;
public Receive2(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {

		try {
			while(true) {
			byte[] buf = new byte[1024];
			
			DatagramPacket dp = new DatagramPacket(buf , 0 , buf.length);
			
			ds.receive(dp);
			
			String str =new String(buf , 0 , buf.length);
			
			System.out.println("ComputerOne----->"+ str);
			}
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(ds != null) {
				ds.close();
			}
		}
		
	
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值