package six;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class UDPClient {

	private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JButton btnNewButton_3;
	private JTextArea textArea_1;
	private int cport;//客户端所用
	private String ip ="127.0.0.1";
	private String sport = "1024";//服务器的ip
	private DatagramSocket datagramSocket;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UDPClient window = new UDPClient();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public UDPClient() {
		initialize();
		initSocket();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		frame = new JFrame();
		frame.setBounds(100, 100, 496, 357);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);

		JLabel lblNewLabel = new JLabel("ip:");
		panel.add(lblNewLabel);

		textField = new JTextField();
		textField.setText(ip);
		panel.add(textField);
		textField.setColumns(6);

		JLabel lblNewLabel_1 = new JLabel("port:");
		panel.add(lblNewLabel_1);

		textField_1 = new JTextField();
		textField_1.setText(sport);
		panel.add(textField_1);
		textField_1.setColumns(5);

		JButton btnNewButton = new JButton("确认");
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel.add(btnNewButton);

		panel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "服务器信息", TitledBorder.LEADING,
				TitledBorder.TOP, new Font("微软雅黑", Font.PLAIN, 12), new Color(59, 59, 59)));
		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.WEST);

		JTextArea textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setRows(10);
		textArea.setColumns(11);
		panel_1.add(textArea);

		panel_1.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "登录情况", TitledBorder.LEADING,
				TitledBorder.CENTER, new Font("微软雅黑", Font.PLAIN, 12), new Color(59, 59, 59)));

		JPanel panel_2 = new JPanel();
		frame.getContentPane().add(panel_2, BorderLayout.EAST);

		textArea_1 = new JTextArea();
		textArea_1.setEditable(false);
		textArea_1.setColumns(29);
		textArea_1.setRows(10);
		panel_2.add(textArea_1);

		panel_2.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "聊天内容", TitledBorder.LEADING,
				TitledBorder.TOP, new Font("微软雅黑", Font.PLAIN, 12), new Color(59, 59, 59)));

		JPanel panel_3 = new JPanel();
		frame.getContentPane().add(panel_3, BorderLayout.SOUTH);

		textField_2 = new JTextField();
		panel_3.add(textField_2);
		textField_2.setColumns(10);

		btnNewButton_3 = new JButton("发送");
		btnNewButton_3.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(btnNewButton_3);

		JButton btnNewButton_1 = new JButton("上传");
		panel_3.add(btnNewButton_1);
		btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));

		JButton btnNewButton_2 = new JButton("下载");
		panel_3.add(btnNewButton_2);
		panel_3.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "事件处理", TitledBorder.LEADING,
				TitledBorder.TOP, new Font("微软雅黑", Font.PLAIN, 12), new Color(59, 59, 59)));
		btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 12));

		// 连接的事务处理
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnNewButton.setEnabled(false); // 禁用按钮,避免重复启动
				final String ipAdress = textField.getText();
				final String remotePort = textField_1.getText();
				if (ipAdress == null || remotePort == null) {
					JOptionPane.showMessageDialog(null, "请输入正确的IP和端口号");
					return;
				}
				if (datagramSocket == null || datagramSocket.isClosed()) {
					JOptionPane.showMessageDialog(null, "监听失败!");
					return;
				}
			}
		});

		// 发送信息的事务处理
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				final String ipAdress = textField.getText();
				final String remotePort = textField_1.getText();
				byte[] buf = textField_2.getText().getBytes();
				try {
					textArea_1.append("我对" + ipAdress + ":" + remotePort + "说:" + textField_2.getText() + "\n\n");
					textArea_1.setCaretPosition(textArea_1.getText().length());
					datagramSocket.send(new DatagramPacket(buf, buf.length, InetAddress.getByName(ipAdress),
							Integer.parseInt(remotePort)));
					textField_2.setText("");
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(null, "未发送成功!");
					e2.printStackTrace();
				}
			}
		});

		// 上传的事务处理
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});

		// 下载的事务处理
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
	}

	public void initSocket() { // 设置端口号

		while (true) {
			try {
				if (datagramSocket != null && !datagramSocket.isClosed()) {
					datagramSocket.close();
				}
				try {
					cport = Integer
							.parseInt(JOptionPane.showInputDialog(null, "请输入端口号", "端口号", JOptionPane.QUESTION_MESSAGE));
					if (cport < 1 || cport > 65535) {
						throw new RuntimeException("端口号超范围!");
					}
				} catch (Exception e3) {
					JOptionPane.showMessageDialog(null, "请重新输入!");
					continue;
				}
				datagramSocket = new DatagramSocket(cport);
				
				startListener();
				frame.setTitle(cport+"号客户机");
				break;
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, "监听失败,请重新设置端口");
			}
		}
	}

	public void startListener() { // 监听端口 用来接收消息
		new Thread() {
			private DatagramPacket p;

			public void run() {
				byte[] buf = new byte[1024];
				p = new DatagramPacket(buf, buf.length);
				while (!datagramSocket.isClosed()) {
					try {
						datagramSocket.receive(p);
						textArea_1.append(p.getAddress().getHostAddress() + ":"
								+ ((InetSocketAddress) p.getSocketAddress()).getPort() + "对我说:\n"
								+ new String(p.getData(), 0, p.getLength()) + "\n\n");
						textArea_1.setCaretPosition(textArea_1.getText().length());

					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}.start();

	}
}
最近下载更多
zhendong  LV7 2022年3月6日
meyuso  LV9 2022年2月22日
你们的代码都是我的了  LV16 2021年9月18日
dengge123  LV13 2021年6月6日
lid7kay  LV3 2021年5月23日
piupiu1111  LV1 2020年7月26日
1791260721  LV1 2020年6月30日
15693621019  LV2 2020年6月22日
cheungf  LV1 2020年6月3日
smsbQAQ  LV1 2020年5月17日
最近浏览更多
krispeng  LV6 4月15日
lshlsh 2023年12月25日
暂无贡献等级
李朝磊  LV18 2023年12月3日
2385649653  LV7 2023年6月25日
qwe369  LV1 2023年5月18日
hhdbzz  LV1 2022年12月29日
然燃然  LV1 2022年12月23日
Lvelvelve  LV2 2022年12月11日
sslhss 2022年11月30日
暂无贡献等级
月之氏族  LV23 2022年11月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友