import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class liaotian implements Runnable,ActionListener{
JTextArea showArea;
JLabel lbl1,lbl2,lbl3;
JTextField msgText,sendPortText,receivePortText,IPAddressText;
JFrame mainJframe;
JButton sendBtn,startBtn;
JScrollPane JSPane;
JPanel pane1,pane2;
Container con;
Thread thread=null;
DatagramPacket sendPack,receivePack;
DatagramSocket sendSocket,receiveSocket;
private InetAddress sendIP;
private int sendPort,receivePort; //存储发送端口和接收端口
private byte inBuf[], outBuf[];
public static final int BUFSIZE=1024;
public liaotian() {
mainJframe=new JFrame("liaotian");
con=mainJframe.getContentPane();
showArea=new JTextArea();
showArea.setEditable(false);
showArea.setLineWrap(true);
lbl1=new JLabel("我的端口号:");
lbl2=new JLabel("对方端口号:");
lbl3=new JLabel("对方的IP地址:");
sendPortText=new JTextField();
sendPortText.setColumns(5);
receivePortText=new JTextField();
receivePortText.setColumns(5);
IPAddressText=new JTextField();
IPAddressText.setColumns(8);
startBtn=new JButton("开始");
startBtn.addActionListener(this);
pane1=new JPanel();
pane1.setLayout(new FlowLayout());
pane1.add(lbl1);
pane1.add(receivePortText);
pane1.add(lbl2);
pane1.add(sendPortText);
pane1.add(lbl3);
pane1.add(IPAddressText);
pane1.add(startBtn);
JSPane=new JScrollPane(showArea);
msgText=new JTextField();
msgText.setColumns(40);
msgText.setEditable(false);
msgText.addActionListener(this);
sendBtn=new JButton("发送");
sendBtn.setEnabled(false);
sendBtn.addActionListener(this);
pane2=new JPanel();
pane2.setLayout(new FlowLayout());
pane2.add(msgText);
pane2.add(sendBtn);
con.add(pane1,BorderLayout.NORTH);
con.add(JSPane,BorderLayout.CENTER);
con.add(pane2,BorderLayout.SOUTH);
mainJframe.setSize(600,400);
mainJframe.setVisible(true);
mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new liaotian();
}
public void actionPerformed(ActionEvent e) {
try{
if(e.getSource()==startBtn){ //按下了开始按钮
inBuf=new byte[BUFSIZE];
sendPort=Integer.parseInt(sendPortText.getText());
sendIP=InetAddress.getByName(IPAddressText.getText());
sendSocket=new DatagramSocket();
receivePort=Integer.parseInt(receivePortText.getText());
//创建接收数据包
receivePack=new DatagramPacket(inBuf,BUFSIZE);
//指定接收数据的端口
receiveSocket=new DatagramSocket(receivePort);
//创建线程准备接收对方的消息
thread=new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
startBtn.setEnabled(false);
sendBtn.setEnabled(true);
msgText.setEditable(true);
}else{ //按下了发送按钮或回车键
outBuf=msgText.getText().getBytes();
//组装要发送的数据包
sendPack=new DatagramPacket(outBuf,outBuf.length,sendIP,
sendPort);
//发送数据
sendSocket.send(sendPack);
showArea.append("我:"+msgText.getText()+"\n");
msgText.setText(null);
}
}catch(UnknownHostException el){
showArea.append("无法连接到指定地址\n");
}catch(SocketException el){
showArea.append("无法打开指定端口\n");
}catch(IOException el){
showArea.append("发送数据失败\n");
}
}
//在线程中接收数据
public void run() {
String msgstr;
while (true){
try{ //注意这个try的位置
receiveSocket.receive(receivePack);
msgstr=new String(receivePack.getData(),0,receivePack.
getLength());
showArea.append(sendPort+":"+msgstr+"\n");
}catch(IOException el){
showArea.append("接收数据出错\n");
}
}
}
}
liaotian.rar_UDP_使用UDP套接字编程_在局域网中进行点对点聊天的程序_聊天 点对点
版权申诉
178 浏览量
2022-09-21
22:19:27
上传
评论
收藏 1KB RAR 举报
JonSco
- 粉丝: 67
- 资源: 1万+
最新资源
- 汽车级带驱动保护芯片规格书
- 基于matlab实现遗传算法优化bp神经网络权值阈值的MATLAB程序.rar
- 基于matlab实现遗传算法优化BP网络(用于电力负荷预测预测).rar
- 基于matlab实现用于speech enhancement的经典噪声估计源代码完整,可直接使用 .rar
- 基于matlab实现振动信号预处理,主要为五点三次平滑法和平均法.rar
- 基于matlab实现直流电机双闭环控制matlab程序,双闭环能增加系统的动态响应,提升控制性能.rar
- 基于matlab实现直流电机的闭环控制,通过simulink的仿真,来设置pid参数,达到成功的控制直流电机的运行 .rar
- 基于matlab实现在振动信号处理中的应用,书籍和源码 工程振动信号的预处理以及模态参数的识别程序.rar
- Sora关于视频分镜的提示词技巧.docx
- 基于java的Saas OA协同办公软件源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈