Java使用GUI完成UDP通信
程序员文章站
2022-06-30 19:54:53
...
Client代码:
package UDPBase;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Client extends JFrame {
private JPanel contentPane;
private JTextField textIP;
private JTextField textPort;
private JTextField textSend;
private JTextField textResult;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Client frame = new Client();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Client() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 629, 274);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("配置信息");
label.setBounds(8, 9, 54, 15);
contentPane.add(label);
JLabel lblIp = new JLabel("IP");
lblIp.setBounds(18, 34, 29, 15);
contentPane.add(lblIp);
textIP = new JTextField();
textIP.setText("192.168.1.6");
textIP.setBounds(43, 34, 76, 21);
contentPane.add(textIP);
textIP.setColumns(10);
JLabel label_1 = new JLabel("端口");
label_1.setBounds(129, 37, 54, 15);
contentPane.add(label_1);
textPort = new JTextField();
textPort.setText("8800");
textPort.setBounds(170, 34, 66, 21);
contentPane.add(textPort);
textPort.setColumns(10);
JButton button = new JButton("初始化");
button.addActionListener(new ButtonActionListener());
button.setBounds(288, 34, 93, 23);
contentPane.add(button);
JButton button_1 = new JButton("关闭");
button_1.setBounds(425, 32, 93, 25);
contentPane.add(button_1);
JPanel panel = new JPanel();
panel.setBounds(8, 10, 595, 61);
contentPane.add(panel);
JLabel label_2 = new JLabel("操作");
label_2.setBounds(10, 94, 54, 15);
contentPane.add(label_2);
textSend = new JTextField();
textSend.setBounds(20, 108, 455, 25);
contentPane.add(textSend);
textSend.setColumns(10);
JButton textsend = new JButton("发送");
textsend.addActionListener(new Button_2ActionListener());
textsend.setBounds(493, 109, 93, 23);
contentPane.add(textsend);
JLabel label_3 = new JLabel("反馈信息");
label_3.setBounds(10, 151, 54, 15);
contentPane.add(label_3);
textResult = new JTextField();
textResult.setBounds(20, 176, 566, 32);
contentPane.add(textResult);
textResult.setColumns(10);
JPanel panel_1 = new JPanel();
panel_1.setBounds(8, 87, 595, 138);
contentPane.add(panel_1);
}
socketThd sthd;
DatagramSocket dsSocket=null;
private class ButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//初始化
try {
dsSocket=new DatagramSocket();
new socketThd().start();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
private class Button_2ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//发送数据
try {
InetAddress ip=InetAddress.getByName(textIP.getText().trim());
int port=Integer.parseInt(textPort.getText().trim());
String strSend=textSend.getText().trim();
byte [] bsSend=strSend.getBytes("UTF-8");
DatagramPacket dpSend=new DatagramPacket(bsSend,bsSend.length,ip,port);
dsSocket.send(dpSend);
} catch (Exception e2) {
// TODO: handle exception
}
}
}
class socketThd extends Thread{
//监听服务器反馈的信息
public void run() {
try {
while(true) {
byte [] bsRec=new byte[1024];
DatagramPacket dpRec=new DatagramPacket(bsRec,bsRec.length);
dsSocket.receive(dpRec);//阻塞命令
String strRec=new String(bsRec,0,dpRec.getLength(),"UTF-8");
textResult.setText(strRec);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
Server代码:
package UDPBase;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Server extends JFrame {
private JPanel contentPane;
private JTextField textPort;
public JTextArea textLog;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Server frame = new Server();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Server() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 460, 471);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("配置信息");
label.setBounds(10, 20, 54, 15);
contentPane.add(label);
JLabel label_1 = new JLabel("端口:");
label_1.setBounds(20, 53, 54, 15);
contentPane.add(label_1);
textPort = new JTextField();
textPort.setText("8800");
textPort.setBounds(57, 50, 66, 21);
contentPane.add(textPort);
textPort.setColumns(10);
JButton button = new JButton("开启服务");
button.addActionListener(new ButtonActionListener());
button.setBounds(167, 49, 93, 23);
contentPane.add(button);
JButton button_1 = new JButton("关闭服务");
button_1.setBounds(311, 49, 93, 23);
contentPane.add(button_1);
JPanel panel = new JPanel();
panel.setBounds(0, 10, 434, 82);
contentPane.add(panel);
JLabel label_2 = new JLabel("消息记录");
label_2.setBounds(10, 112, 54, 15);
contentPane.add(label_2);
JPanel panel_1 = new JPanel();
panel_1.setBounds(10, 102, 434, 305);
contentPane.add(panel_1);
panel_1.setLayout(null);
textLog = new JTextArea();
textLog.setBounds(10, 22, 414, 273);
panel_1.add(textLog);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 22, 414, 273);
panel_1.add(scrollPane);
}
DatagramSocket dsRec=null;
private class ButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//开启服务
try {
int port=Integer.parseInt(textPort.getText().trim());
dsRec=new DatagramSocket(port);
textLog.append("UDP服务开启...\r\n");
new UDPthread().start();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
class UDPthread extends Thread{
@Override
public void run() {
try {
while(true) {
byte [] bsRec=new byte[1024];
DatagramPacket dpRec=new DatagramPacket(bsRec,bsRec.length);
dsRec.receive(dpRec);//阻塞式命令
String strRec=new String(bsRec,0,dpRec.getLength(),"UTF-8");
System.out.println(strRec);
textLog.append("客户端:"+strRec);
//处理
String strRtn="服务器处理:"+strRec;
byte []bsSend=strRtn.getBytes("UTF-8");
DatagramPacket dpSend=new DatagramPacket(bsSend,bsSend.length,dpRec.getAddress(),dpRec.getPort());
dsRec.send(dpSend);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
实现结果如图:
上一篇: 百度t7 教程 node 初步1
下一篇: Node模块-Module,Path
推荐阅读
-
使用weixin-java-tools完成微信授权登录、微信支付的示例
-
Java模拟UDP通信示例代码
-
Java开发笔记(一百一十六)采用UDP协议的Socket通信
-
PHP实现与java 通信的插件使用教程
-
使用C#实现基于TCP和UDP协议的网络通信程序的基本示例
-
c/c++ 网络编程 使用getaddrinfo的单纯UDP 通信
-
vue中使用v-model完成组件间的通信
-
java jdk 8反编译工具JD-GUI、procyon-decompiler、luyten、crf下载使用简介
-
JAVA基础之网络通信协议--TCP与UDP
-
java网络编程_java网络编程(2)socket通信案例(TCP和UDP)