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();

}

}

相关推荐
青草地溪水旁36 分钟前
CAN通道的“一对一”与“选择性”:物理线束、CanSM通道与网络报文实践解析
网络·autosar cp
拾光Ծ36 分钟前
【Linux网络】计算机网络入门:网络产生与协议
linux·网络·网络协议·tcp/ip·计算机网络
稳联技术老娜7 小时前
DeviceNet主站怎么连接西门子PLC,Profinet网关配置手册(那智机器人)
服务器·网络·数据库
志栋智能9 小时前
AI驱动无代码:降低巡检超自动化的门槛
大数据·运维·网络·人工智能·自动化
专业机床数据采集9 小时前
C# 精雕数控 数据采集 Demo|实时读取精雕机床坐标、主轴、负载、加工工时全量参数
网络·网络协议·tcp/ip·mes·精雕数控数据采集
AOwhisky10 小时前
Ceph系列第六期:Ceph 文件系统(CephFS)精讲
linux·运维·网络·笔记·ceph
我爱C编程10 小时前
基于ECC簇内分组密钥管理算法的无线传感器网络matlab性能仿真
网络·matlab·ecc·密钥管理·无线传感器网络·簇内分组
Sagittarius_A*11 小时前
H3CSE 高性能园区网:园区网安全体系详解
网络·计算机网络·安全·h3cse
安全检测中14 小时前
探讨一个OSPF中NSSA类型的问题
网络
luj_176814 小时前
R语言生态优势与学习曲线分析
c语言·开发语言·网络·经验分享·算法