GUI界面的UDP聊天室

实验要求:

(1)用UDP实现程序的编写。

(2)UDP没有服务端和客户端的区分。

 

运行结果如下:

(1)启动程序先在控制台输入端口号,操作两次。

GUI界面的UDP聊天室_第1张图片

 GUI界面的UDP聊天室_第2张图片

 

(2)在两个聊天框下方输入对方的IP地址和端口号。

GUI界面的UDP聊天室_第3张图片

 

(3)分别跟对方发送信息。 

GUI界面的UDP聊天室_第4张图片

 

 

 

代码如下:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

import javax.swing.*;
import javax.swing.border.TitledBorder;

public class SimpleChatClient extends JFrame implements ActionListener{
	
	int otherPort;
	String otherHost;
	static int localport;
	
	String message;
    private DatagramSocket socket;
	
	JLabel jl = new JLabel("正在监听" + localport + "端口!");
	
	JTextArea jta_1 = new JTextArea(4, 43);
	JTextArea jta_2 = new JTextArea(6, 43);
	
	JTextField jtf_1 = new JTextField(9);	//要联系的对象的地址
	JTextField jtf_2 = new JTextField(9);	//要联系的对象的端口
	
	JButton send = new JButton("发送");
	JButton	clear = new JButton("清空");
	
	JPanel jp_1 = new JPanel();
	JPanel jp_2 = new JPanel();
	
	public SimpleChatClient() {
		super("聊天室GUI版");
		init();
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	public void init() {
		//组装视图
		this.setSize(550, 410);
		
		jl.setHorizontalAlignment(jl.RIGHT);
		this.add(jl, BorderLayout.NORTH);
		
		
		jp_1.setLayout(new BorderLayout());
		TitledBorder tb_1 = new TitledBorder("聊天记录");
		jta_1.setBorder(tb_1);
		jp_1.add(new JScrollPane(jta_1));
		
		TitledBorder tb_2 = new TitledBorder("请输入文字:");
		jta_2.setBorder(tb_2);
		jp_1.add(new JScrollPane(jta_2), BorderLayout.SOUTH);
		this.add(jp_1);
		
		
		jp_2.add(jtf_1);
		jp_2.add(jtf_2);
		jp_2.add(send);
		jp_2.add(clear);
		send.addActionListener(this);			//注册监听
		clear.addActionListener(this);
		
		jtf_1.addActionListener(this);
		jtf_2.addActionListener(this);
		
		this.add(jp_2, BorderLayout.SOUTH);
	}
	
    private void send(){ 					//发送消息
        message = jta_2.getText();
        byte[] contains = message.getBytes();
        sendMessage(contains);
        String str = "我对" + otherHost + ":" + otherPort + "说:" + message + "\n";
        jta_1.append(str);					//添加进聊天记录框
        jta_2.setText("");					//发送完信息后清空输入信息框
    }
	
	private void sendMessage(byte[] data){
        try {
            DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName(otherHost), localport);
            socket.send(packet);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	public static void main(String[] args) {	//程序入口
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入本地端口:");
		localport = sc.nextInt();
		new SimpleChatClient();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		
		if(obj == send) {
			send();
		}
			
		if(obj == clear) {
			jta_2.setText("");
		}
		
		if(obj == jtf_1) {
			otherHost = jtf_1.getText();
			if(otherHost.equals("")) {
				JOptionPane.showMessageDialog(jtf_1, "对方IP不能为空!");
			}
		}
		
		if(obj == jtf_2) {
			otherPort = Integer.valueOf(jtf_2.getText()).intValue();
				new Receive().start();
		}
	}
	
	private class Receive extends Thread{
	    @Override
	    public void run() {
	        DatagramPacket packet = null;
	        try {
	            socket = new DatagramSocket(otherPort);
	            byte[] data = new byte[1024 * 4];
	            packet = new DatagramPacket(data, data.length);
	            while (true){
	                socket.receive(packet);
	                byte[] bytes = packet.getData();
	                String message = new String(bytes, 0, bytes.length);
	                String str = otherHost + ":" + otherPort + "对我说:" + message + "\n";
	                jta_1.append(str);
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
	}
}

 

 

你可能感兴趣的:(java,udp,开发语言)