Java复习之知识点整理(二十三)--- 使用UDP搭建屏幕广播案例(二)学生端

-----------------------------------------------------------------------------------
-----------------------四、学生端主类:--------------------------------------
-----------------------------------------------------------------------------------

public class StudentMain {

	public static void main(String[] args) {
		
		StudentUI ui = new StudentUI();
		Student stu = new Student(ui);
		stu.start();		
	}
}


-----------------------------------------------------------------------------------
-----------------------五、学生端界面UI类:-------------------------------
-----------------------------------------------------------------------------------

public class StudentUI extends JFrame
{

	private JLabel lbl;
	private Student stu;
	
	public static HashMap<String,Image> imgMap = new HashMap<String, Image>();
	
	public StudentUI()
	{
		init();
		this.setVisible(true);		
	}
	
	/**
	 * 刷新窗口
	 */
	private void init()
	{
		this.setTitle("学生端");
		this.setBounds(0, 0, 1366, 768);
		this.setLayout(null);
		
		lbl = new JLabel();
		lbl.setBounds(0, 0, 1366, 768);	
		ImageIcon icon = new ImageIcon("d:\\1.jpg");
		lbl.setIcon(icon);
		this.add(lbl);
	}

	/**
	 * 
	 * @param 更新屏幕界面
	 */
	public void updateScreens(byte[] bs) {
			
		ImageIcon icon = new ImageIcon(bs);
		lbl.setIcon(null);
		lbl.setIcon(icon);	
	}
}


-----------------------------------------------------------------------------------
-----------------------六、学生端:-------------------------------------------
-----------------------------------------------------------------------------------

public class Student extends Thread
{

	private StudentUI ui;
	
	private DatagramSocket sSocket;
	
	//存放消息的集合
	public HashMap<String, HashMap<Integer, Message>> messageMap = new HashMap<String, HashMap<Integer,Message>>();
	
	public Student(StudentUI ui)
	{
		//1.新建udp Socket
		try {
			
			this.ui = ui;
			sSocket = new DatagramSocket(9999);
			
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
	/**
	 * 开始接收教师端发送的消息
	 */
	public void run() {
		
		try {
			
			//2.创建pack
			byte [] buf = new byte[64 * 1024];
			DatagramPacket pack = new DatagramPacket(buf, buf.length);
			
			//4.接收数据
			while(true)
			{
				//阻塞的
				sSocket.receive(pack);	
				//1.解析数据
				Message msg = analyPack(pack);
				//2.向集合中增加元素
				putMessageIntoMap(msg);
				
			}			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}


	/**
	 * 解析数据
	 */
	private Message analyPack( DatagramPacket pack ) {
		
		//获取数据包
		byte [] srcbyteArr = pack.getData();
		//设定缓冲区
		byte [] buf;
		
		//1.新建Message
		Message msg = new Message();
		
		//2.获取唯一标识
		buf = new byte[8];
		System.arraycopy(srcbyteArr, 0, buf, 0, 8);
		String flag = Util.bytes2Long(buf) + "";
		msg.setFlag(flag);
		
		//3.获取总块数
		int totleCount = srcbyteArr[8];
		
		msg.setTotleCount(totleCount);
		
		//4.获取当前序号
		int index = srcbyteArr[9];
		msg.setCurIndex(index);
		
		//5.获取内容长度
		buf = new byte[4];
		System.arraycopy(srcbyteArr, 10, buf, 0, 4);
		int len = Util.bytes2Int(buf);
		
		//6.获取内容
		buf = new byte[len];
		System.arraycopy(srcbyteArr, 14, buf, 0, len);
		msg.setContent(buf);
			
		return msg;
	}
	
	
	/**
	 * 向集合中增加元素
	 */
	private void putMessageIntoMap(Message msg)
	{
		//唯一标识符
		String flag = msg.getFlag();
		//总数量
		int totleCount = msg.getTotleCount();
	
		
		//System.out.println("New Msg flag:" + flag + 
		
		HashMap<Integer, Message> map = new HashMap<Integer, Message>();
		if(messageMap.containsKey(flag))
		{
			map = messageMap.get(flag);				
		}
		
		map.put(msg.getCurIndex(),msg);
		messageMap.put(flag, map);
		System.out.println("flag:" + flag + "---size" + map.size() + "---New Msg Index:" + msg.getCurIndex()  + "---New Msg totle:" + msg.getTotleCount());

		//判断map是否满了,可以组装了
		if(map.size() >= totleCount)
		{
			System.out.println("已经凑齐一张了:" + flag);
			Set<Integer> keySet = map.keySet();
			List<Integer> list = new ArrayList<Integer>();
			for(Integer i : keySet)
			{				
				list.add(i);	
			}
			
			Collections.sort(list);
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			
			
			try {
				
				for(Integer i : list)
				{			
					baos.write(map.get(i).getContent());	
				}
				byte [] bs = baos.toByteArray();
				
				ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bs));
				
				zis.getNextEntry();
				
				byte[] buf = new byte[1024];
				int len = -1 ;
				
				//创建baos,容纳解压的帧画面数据
				baos = new ByteArrayOutputStream() ;
				while((len = zis.read(buf)) != -1){
					baos.write(buf, 0, len);
				}
				zis.close();
				//得到解压的帧画面数据
				byte[] unzipFrameData = baos.toByteArray();
	
				ui.updateScreens(unzipFrameData);
				baos.close();
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}		
	}
}









猜你喜欢

转载自blog.csdn.net/xcvbxv01/article/details/81053853
今日推荐