优化的UDP代码及多线程的实现

public class SendDemo {

	public static void main(String[] args) throws IOException {
		//创建发送端的Socket对象
		DatagramSocket  ds = new DatagramSocket() ;
		
		//创建数据报包对象
		byte[] bys = "hello,udp,我来了".getBytes() ;
		DatagramPacket dp = new DatagramPacket(bys, bys.length,InetAddress.getByName("192.168.0.124"),12345) ;
	
		//发送数据
		ds.send(dp);
		
		//释放资源
		
		ds.close();
	}
}

public class ReceiveDemo {
	
	public static void main(String[] args) throws IOException {
		
		//创建接收端的Socket对象
		DatagramSocket ds = new DatagramSocket(12345) ;
		
		//创建接收容器
		//创建字节数组的大小1024或者1024的倍数
		byte[] bys = new byte[1024] ;
		DatagramPacket dp = new DatagramPacket(bys, bys.length);
		
		//调用接收的方法
		ds.receive(dp);//阻塞式方法
		
		//解析数据,将数据展示在控制台
		//获取ip地址
		String ip = dp.getAddress().getHostAddress() ;
		//public byte[] getData() 获取缓冲区中实际的字节数
		//public int getLength()  获取缓冲区中实际的长度
		String s = new String(dp.getData(), 0,dp.getLength()) ;
		
		//展示控制台
		System.out.println("from"+ip+"data is:"+s);
		
		//释放资源
		ds.close();
	}
}

以上代码只能实现一次发一个消息,不能多次发送,下面我们学习怎么实现多次发送和接收

多线程:
Runable接口的方式
SendThread implements Runnable{
  }
   发送端的线程
接收端的线程
 
主线程

只需要需一个窗口

public class SendDemo {

	public static void main(String[] args) {

		try {
			// 创建Socket对象
			DatagramSocket ds = new DatagramSocket();

				// 键盘录入
				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

				String line = null ; //读了一次
				while((line=br.readLine())!=null){
	
					//自定义结束条件
					if("886".equals(line)) {
						break ;
					}

					byte[] bys = line.getBytes() ;
					int length = bys.length ;
					// 创建数据报包
					DatagramPacket dp = new DatagramPacket(bys,length,
							InetAddress.getByName("192.168.10.1"), 10086);

					// 发送数据
					ds.send(dp);

				
				}
				// 释放资源
				ds.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

public class ReceiveDemo {
	
	public static void main(String[] args) {
		
		try {
			//创建接收端的Socket对象
			DatagramSocket ds = new DatagramSocket(10086);
			
			while(true) {
				//需要创建接收容器
				byte[] bys = new byte[1024] ;
				DatagramPacket dp = new DatagramPacket(bys, bys.length) ;
				
				//接收
				ds.receive(dp);
				
				//解析数据
				String ip = dp.getAddress().getHostAddress() ;
				String s = new String(dp.getData(), 0, dp.getLength()) ;
				
				System.out.println("from" +ip +"data is:"+s);
				
				//释放资源,接收端不停地接收数据,所以不应该该关闭
//				ds.close();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
  多线程:
Runable接口的方式
  SendThread implements Runnable{
  }
发送端的线程
接收端的线程
 
  主线程
只需要需一个窗口 ,让我们不断发送数据
  

 简单的聊天室

项目的聊天室  集合+多线程+IO流+网络编程 综合应用
1)公聊
  2)私聊
3)在线人数
4)发送文件

5)下线提醒

public class ReceiveThread implements Runnable {

	private DatagramSocket ds;

	public ReceiveThread(DatagramSocket ds) {
		this.ds = ds;
	}

	@Override
	public void run() {
		try {

			while(true) {
				//需要创建接收容器
				byte[] bys = new byte[1024] ;
				DatagramPacket dp = new DatagramPacket(bys, bys.length) ;
				
				//接收
				ds.receive(dp);
				
				//解析数据
				String ip = dp.getAddress().getHostAddress() ;
				String s = new String(dp.getData(), 0, dp.getLength()) ;
				
				System.out.println("from" +ip +"data is:"+s);
				
				//释放资源,接收端不停地接收数据,所以不应该该关闭
//				ds.close();
			}
		}catch(IOException e) {
			e.printStackTrace(); 
		}
	}

}
public class SendThread implements Runnable {

	private DatagramSocket ds ;
	public SendThread(DatagramSocket ds) {
		this.ds = ds ;
	}
	
	@Override
	public void run() {
		try {
			//使用字符缓冲输入流
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) ;
			String line = null ;
			//不断发送数据
			while((line=br.readLine())!=null) {
				//自定义结束条件
				if("over".equals(line)) {
					break ;
				}
				
				byte[] bys = line.getBytes() ;
				//创建数据报包
				DatagramPacket dp = 
						new DatagramPacket(bys, bys.length, 
									InetAddress.getByName("192.168.1.103"), 10086) ;
				
				//发送数据
				ds.send(dp);
				
			}
			//释放
			ds.close();
			
		}catch(IOException e) {
			e.printStackTrace(); 
		}
	}

}

public class ChartRoom {
	
	public static void main(String[] args) throws Exception {
		
		//分别创建发送端和接收端的Socket对象
		DatagramSocket sendSocket = new DatagramSocket() ;
		DatagramSocket receiveSocket = new DatagramSocket(10086) ;
		
		//创建资源对象
		SendThread st = new SendThread(sendSocket) ;
		ReceiveThread rt = new ReceiveThread(receiveSocket) ;
		
		//创建线程对象
		Thread t1 = new Thread(st) ;
		Thread t2 = new Thread(rt) ; 
		
		//启动线程
		t1.start();
		t2.start();
		
	}
}




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值