Client代码:
package UDPchor;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
public class client extends JFrame {
private JPanel contentPane;
private JTextField txtPort;
private JTextField txtIP;
private JTextField txt1;
private JTextField txt2;
private JTextField txt3;
String num1="";
String num2="";
String opt="";
socketThd sthd;
DatagramSocket dsSocket=null;
/**
* 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() {
setTitle("客户端");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(170, 159, 2, 2);
contentPane.add(scrollPane);
txtPort = new JTextField();
txtPort.setText("8890");
txtPort.setBounds(176, 24, 66, 23);
contentPane.add(txtPort);
txtPort.setColumns(10);
JButton btnStart = new JButton("登录");
btnStart.setBounds(270, 24, 66, 23);
contentPane.add(btnStart);
JButton btnClose = new JButton("退出");
btnClose.setBounds(358, 24, 66, 23);
contentPane.add(btnClose);
JLabel lblNewLabel = new JLabel("登录信息");
lblNewLabel.setBounds(10, 10, 54, 15);
contentPane.add(lblNewLabel);
JLabel label = new JLabel("端口");
label.setBounds(143, 28, 29, 15);
contentPane.add(label);
txtIP = new JTextField();
txtIP.setText("192.168.1.10");
txtIP.setBounds(67, 25, 66, 21);
contentPane.add(txtIP);
txtIP.setColumns(10);
JLabel lblIp = new JLabel("服务器IP");
lblIp.setBounds(10, 28, 54, 15);
contentPane.add(lblIp);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 414, 47);
contentPane.add(panel);
JLabel label_1 = new JLabel("操作");
label_1.setBounds(10, 82, 54, 15);
contentPane.add(label_1);
JLabel label_2 = new JLabel("操作数1");
label_2.setBounds(43, 128, 54, 15);
contentPane.add(label_2);
JLabel label_3 = new JLabel("操作数2");
label_3.setBounds(43, 163, 54, 15);
contentPane.add(label_3);
JLabel label_4 = new JLabel("操作数3");
label_4.setBounds(43, 198, 54, 15);
contentPane.add(label_4);
txt1 = new JTextField();
txt1.setBounds(107, 125, 135, 21);
contentPane.add(txt1);
txt1.setColumns(10);
txt2 = new JTextField();
txt2.setColumns(10);
txt2.setBounds(107, 159, 135, 21);
contentPane.add(txt2);
txt3 = new JTextField();
txt3.setColumns(10);
txt3.setBounds(107, 195, 135, 21);
contentPane.add(txt3);
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//初始化
try {
dsSocket=new DatagramSocket();
new socketThd().start();
} catch (Exception e2) {
// TODO: handle exception
}
}
});
JButton btnPlus = new JButton("加法(+)");
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//发送数据
try {
InetAddress ip=InetAddress.getByName(txtIP.getText().trim());
int port=Integer.parseInt(txtPort.getText().trim());
num1=txt1.getText().trim();
num2=txt2.getText().trim();
double c=Double.parseDouble(num1)+Double.parseDouble(num2);
String strSend=""+c;
System.out.println("发送的:"+strSend);
byte [] bsSend=strSend.getBytes("UTF-8");
DatagramPacket dpSend=new DatagramPacket(bsSend,bsSend.length,ip,port);
dsSocket.send(dpSend);
} catch (Exception e2) {
// TODO: handle exception
}
}
});
btnPlus.setHorizontalAlignment(SwingConstants.LEFT);
btnPlus.setBounds(252, 128, 81, 35);
contentPane.add(btnPlus);
JButton btnSub = new JButton("减法(-)");
btnSub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//发送数据
try {
InetAddress ip=InetAddress.getByName(txtIP.getText().trim());
int port=Integer.parseInt(txtPort.getText().trim());
num1=txt1.getText().trim();
num2=txt2.getText().trim();
double c=Double.parseDouble(num1)-Double.parseDouble(num2);
String strSend=""+c;
System.out.println(strSend);
byte [] bsSend=strSend.getBytes("UTF-8");
DatagramPacket dpSend=new DatagramPacket(bsSend,bsSend.length,ip,port);
dsSocket.send(dpSend);
} catch (Exception e2) {
// TODO: handle exception
}
}
});
btnSub.setHorizontalAlignment(SwingConstants.LEFT);
btnSub.setBounds(343, 126, 82, 35);
contentPane.add(btnSub);
JButton btnMui = new JButton("乘法(*)");
btnMui.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//发送数据
try {
InetAddress ip=InetAddress.getByName(txtIP.getText().trim());
int port=Integer.parseInt(txtPort.getText().trim());
num1=txt1.getText().trim();
num2=txt2.getText().trim();
double c=Double.parseDouble(num1)*Double.parseDouble(num2);
String strSend=""+c;
System.out.println(strSend);
byte [] bsSend=strSend.getBytes("UTF-8");
DatagramPacket dpSend=new DatagramPacket(bsSend,bsSend.length,ip,port);
dsSocket.send(dpSend);
} catch (Exception e2) {
// TODO: handle exception
}
}
});
btnMui.setHorizontalAlignment(SwingConstants.LEFT);
btnMui.setBounds(252, 188, 78, 35);
contentPane.add(btnMui);
JButton btnDiv = new JButton("除法(/)");
btnDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//发送数据
try {
InetAddress ip=InetAddress.getByName(txtIP.getText().trim());
int port=Integer.parseInt(txtPort.getText().trim());
num1=txt1.getText().trim();
num2=txt2.getText().trim();
double c=Double.parseDouble(num1)/Double.parseDouble(num2);
String strSend=""+c;
System.out.println(strSend);
byte [] bsSend=strSend.getBytes("UTF-8");
DatagramPacket dpSend=new DatagramPacket(bsSend,bsSend.length,ip,port);
dsSocket.send(dpSend);
} catch (Exception e2) {
// TODO: handle exception
}
}
});
btnDiv.setHorizontalAlignment(SwingConstants.LEFT);
btnDiv.setBounds(343, 188, 82, 35);
contentPane.add(btnDiv);
JPanel panel_1 = new JPanel();
panel_1.setBounds(10, 67, 424, 184);
contentPane.add(panel_1);
}
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");
System.out.println(strRec);
txt3.setText(strRec);
}
} catch (Exception e) {
// TODO: handle exception
}
}
//信息发送
public void send(String num1,String num2,String opt) {
}
}
}
Service代码:
package UDPchor;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.awt.event.ActionEvent;
import javax.swing.border.TitledBorder;
public class service extends JFrame {
private JPanel contentPane;
private JTextField textPort;
private JTextArea textArea;
DatagramSocket dsRec=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
service frame = new service();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public service() {
setTitle("服务端");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 493, 360);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("配置信息");
label.setBounds(10, 10, 54, 15);
contentPane.add(label);
JLabel label_1 = new JLabel("端口");
label_1.setBounds(10, 35, 36, 15);
contentPane.add(label_1);
textPort = new JTextField();
textPort.setBounds(48, 35, 66, 21);
textPort.setText("8890");
contentPane.add(textPort);
textPort.setColumns(10);
JButton btnStart = new JButton("开始服务");
btnStart.setBounds(147, 34, 93, 23);
contentPane.add(btnStart);
JButton btnClose = new JButton("停止服务");
btnClose.setBounds(278, 31, 93, 23);
contentPane.add(btnClose);
JPanel panel = new JPanel();
panel.setBounds(0, 10, 399, 63);
contentPane.add(panel);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "\u804A\u5929\u8BB0\u5F55", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setBounds(10, 83, 457, 228);
contentPane.add(panel_1);
panel_1.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(10, 21, 437, 176);
panel_1.add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//开启服务
try {
int port=Integer.parseInt(textPort.getText().trim());
dsRec=new DatagramSocket(port);
textArea.append("UDP服务开启...\r\n");
new UDPthread().start();
} catch (Exception e2) {
// TODO: handle exception
}
}
});
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
class UDPthread extends Thread{
@Override
public void run() {
try {
while(true) {
byte [] bsRec=new byte[1024];
String rtn="";
DatagramPacket dpRec=new DatagramPacket(bsRec,bsRec.length);
dsRec.receive(dpRec);//阻塞式命令
String strRec=new String(bsRec,0,dpRec.getLength(),"UTF-8");
System.out.println("服务端接受的:"+strRec);
textArea.append("客户端:"+strRec);
System.out.println("客户端:"+strRec);
//处理
String strRtn=strRec;
System.out.println("RTN"+strRtn);
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
}
}
}
}
实现结果如下: