UDP群聊

package UDP;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.IOException;

import java.lang.String;

public class lt extends JFrame{

private static final int DEFAULT_PORT=8899;

private JLabel stateLB;

private JTextArea centerTextArea;

private JPanel southPanel;

private JTextArea inputTextArea;

private JPanel bottomPanel;

private JTextField ipTextField;

private JTextField remotePortTF;

private JButton sendBT;

private JButton clearBT;

private DatagramSocket datagramSoket;

private void setUpUI(){

setTitle("聊天");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(400,400);

setResizable(false);//窗口大小不可改变

setLocationRelativeTo(null);//设置窗口相对于指定组件的位置

stateLB=new JLabel("weijianting");

stateLB.setHorizontalAlignment(JLabel.RIGHT);

centerTextArea=new JTextArea();

centerTextArea.setEditable(false);

centerTextArea.setBackground(new Color(211,211,211));

southPanel=new JPanel(new BorderLayout());

inputTextArea=new JTextArea(5,20);

bottomPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));

ipTextField=new JTextField("127.0.0.1",8);

remotePortTF=new JTextField(String.valueOf(DEFAULT_PORT),3);

sendBT=new JButton("发送");

clearBT=new JButton("清屏");

bottomPanel.add(ipTextField);

bottomPanel.add(remotePortTF);

bottomPanel.add(sendBT);

bottomPanel.add(clearBT);

southPanel.add(new JScrollPane(inputTextArea),BorderLayout.CENTER);

southPanel.add(bottomPanel,BorderLayout.SOUTH);

add(stateLB,BorderLayout.NORTH);

add(new JScrollPane(centerTextArea),BorderLayout.CENTER);

add(southPanel,BorderLayout.SOUTH);

setVisible(true);

}

private void setListener(){

sendBT.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

final String ipAddress=ipTextField.getText();

final String remotePort=remotePortTF.getText();

if(ipAddress==null||ipAddress.trim().equals("")||remotePort==null||remotePort.trim().equals("")){

JOptionPane.showMessageDialog(lt.this,"请输入IP地址和端口号");

return;

}

if(datagramSoket==null||datagramSoket.isClosed()){

JOptionPane.showMessageDialog(lt.this,"监听未成功");

return;

}

String sendContent=inputTextArea.getText();

byte\[\] buf=sendContent.getBytes();

try{

centerTextArea.append("我对"+ipAddress+":"+remotePort+"说:\n"+inputTextArea.getText()+"\n\n");

centerTextArea.setCaretPosition(centerTextArea.getText().length());

datagramSoket.send(new DatagramPacket(buf,buf.length,InetAddress.getByName(ipAddress),Integer.parseInt(remotePort)));

inputTextArea.setText("");

}catch(IOException e1){

JOptionPane.showMessageDialog(lt.this, "出错了,发送不成功");

e1.printStackTrace();

}

};

});

clearBT.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

centerTextArea.setText("");

}

});

}

private void initSocket(){

int port=DEFAULT_PORT;

while(true){

try{

if(datagramSoket!=null&&!datagramSoket.isConnected()){

datagramSoket.close();

}

try{

port=Integer.parseInt(JOptionPane.showInputDialog(this,"请输入端口号","端口号",JOptionPane.QUESTION_MESSAGE));

if(port<1||port>65535){

throw new RuntimeException("端口号超出范围");

}

}catch(Exception e){

JOptionPane.showMessageDialog(null,"你输入的端口不正确,请输入1~65535之间的数");

continue;

}

datagramSoket=new DatagramSocket(port);

startListen();

stateLB.setText("已在"+port+"端口监听");

break;

}catch(SocketException e){

JOptionPane.showMessageDialog(this, "端口号被占用,请重新设置端口");

stateLB.setText("当前未启动监听");

}

}

}

private void startListen(){

new Thread(){

private DatagramPacket p;

public void run(){

byte\[\] buf=new byte1024;

p=new DatagramPacket(buf,buf.length);

while(!datagramSoket.isConnected()){

try{

datagramSoket.receive(p);

centerTextArea.append(p.getAddress().getHostAddress()+":"+((InetSocketAddress)p.getSocketAddress()).getPort()+"对我说:\n"+new String(p.getData(),0,p.getLength())+"\n\n");

centerTextArea.setCaretPosition(centerTextArea.getText().length());

}catch(IOException e){

e.printStackTrace();

}

}

}

}.start();

}

public static void main(String\[\] args) {

lt a=new lt();

a.setUpUI();

a.initSocket();

a.setListener();

}

}

相关推荐
码完就睡10 分钟前
Linux——以太网协议
运维·服务器·网络·1024程序员节
CV爱数码28 分钟前
论文精读|RED-CNN:残差编解码网络,低剂量 CT 去噪的经典基线
网络·人工智能·算法·cnn·ct·重建·x-ray
星恒讯工业路由器34 分钟前
Modbus透传和Modbus网关有什么区别?从字节转发到协议翻译的选型思路
网络·物联网·modbus·工业物联网·工业网关·rs485·协议转换
李&@杰1 小时前
交换机PoE供电不足导致AP反复重启故障处理
网络·华为
香蕉大香蕉(06敬畏市场)1 小时前
高频八股 · Java 并发编程(10):异步、ThreadLocal 与虚拟线程(收官)
java·开发语言·网络·jvm·python·ai·实习
jaysee-sjc1 小时前
【苍穹外卖】Day10:定时任务与 WebSocket 实时提醒实战
java·开发语言·javascript·数据库·vue.js·websocket·网络协议
钱工的门窗技术笔记2 小时前
铝木复合门窗中木材的角色分类与界面可分解性:结构判定、失效路径与维护设计
网络·物联网·数据分析
xianyuCcCcCCCcc2 小时前
Kubernetes Service 与网络代理
网络·云原生·kubernetes
未来之窗软件服务2 小时前
诡异的网络IP/域名/ICMP通应用层失败骨干网分层拦截分析-东方仙盟
网络·tcp/ip·智能路由器·dns劫持·仙盟创梦ide·东方仙盟
星恒随风3 小时前
Linux Socket UDP编程(一):手写UDP回显服务器与客户端,从socket、bind、recvfrom到网络字典服务器完整实战
linux·服务器·网络·笔记