UDP简易聊天室

 

服务器类

public class UDPServer  implements Runnable{

	private int port;
	
	public UDPServer(int port) {
		super();
		this.port = port;
	}

	@Override
	public void run() {
		//创建UDP服务器端的对象 必须指定端口
		DatagramSocket ds=null;
		try {
			ds=new DatagramSocket(port);
			
			//定义接收数据的字节数组
			byte[] bytes=new byte[1024];			
			//定义接收的数据包
			DatagramPacket dp=new DatagramPacket(bytes, bytes.length);
			while(true)
			{
				System.out.println("服务器启动,正在等待接收数据.......");
				//接收数据包
				ds.receive(dp);
				Date date=new Date();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String dd=sdf.format(date);
				//获得数据包中的数据  (这个长度是我们自己定义时的长度 1024)
				byte[] dpbs=dp.getData();
				//这个数据是数据的实际长度
				int len=dp.getLength();
				//获得发送端的ip
				InetAddress ia = dp.getAddress();
				String ip=ia.getHostAddress();
				//组装接收的数据
				String str=new String(dpbs, 0, len);
							
				Chat.setUU(ip+"  ---  "+dd+"\r\n"+str);				
				//System.out.println(ip+"说:\r\n"+str);
							
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			if(ds!=null)
				ds.close();
		}
		
	}
}

客户端类

public class UDPClient  {
	
	//发送目的的ip
	private String ip;
	//发送目的的端口
	private int port;
	
	public UDPClient(String ip, int port) {
		super();
		this.ip = ip;
		this.port = port;
	}

	public void send(String str) {
		//创建客户端的套接字对象
		DatagramSocket ds=null;
		
		try {
			ds=new DatagramSocket();
			
			//System.out.println("已经接入:"+ip);
			
			
			byte[] bytes=str.getBytes();	
			
			//创建要发送的目的地的IP对象
			InetAddress ia = InetAddress.getByName(ip);
			//打数据包
			DatagramPacket dp=new DatagramPacket(bytes, bytes.length, ia, port);
			//发送
			ds.send(dp);
				
			//System.out.println("我说:\r\n"+str);

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if(ds!=null)
				ds.close();
			
		}
	}
}

聊天界面类

public class Chat extends JFrame{


		JButton OK;
		static JTextArea useid=new JTextArea(10,20);
		JTextArea password=new JTextArea(10,20);
		
		Box box1,box2,box3;
		static void setUU(String s){
			useid.setText(useid.getText()+"\r\n"+s);
		}
		public Chat(){

			this.setBounds(400, 400,400,400);
			this.setTitle("聊天");
			this.setLayout(new FlowLayout());
			box1=Box.createVerticalBox();
			box1.add(new JLabel("聊天记录"));
			box1.add(Box.createVerticalStrut(8));
			box1.add(new JLabel("输入框"));
			box1.add(Box.createVerticalStrut(8));
			
			box2=Box.createVerticalBox();//�����ı���
			box2.add(useid);
			box2.add(Box.createVerticalStrut(8));
			box2.add(password);
			box2.add(Box.createVerticalStrut(8));
			
			box3=Box.createHorizontalBox();
			box3.add(box1);
			box3.add(box1.createHorizontalStrut(10));
			box3.add(box2);
			
			useid.setEditable(false);
			OK=new JButton("发送");
			this.add(box3);
			this.add(OK);
			this.add(box3);
			this.add(box3);
			this.add(OK);
			this.add(box3);
			
			this.setVisible(true);
			
			Thread t=new Thread(new UDPServer(10000));
			t.start();
			
			
			OK.addMouseListener(new MouseAdapter() {
				
				@Override
				public void mouseClicked(MouseEvent e) {					
					String str=password.getText();
					UDPClient uc=new UDPClient("192.168.199.206",10000);
					uc.send(str);	
					password.setText("");
					Date date=new Date();
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String dd=sdf.format(date);
					Chat.setUU("你"+"  ---  "+dd+"\r\n"+str);	
					
					
					
				}
				
				
				
			});
			
			
			
			
		}	
	}

启动测试类

public class Start {

	public static void main(String[] args) {
			
		new Chat();

	}

}

猜你喜欢

转载自blog.csdn.net/QuietHRH/article/details/81173297
今日推荐