UDP编程

来源:互联网 发布:js文件 syntax error 编辑:程序博客网 时间:2024/05/29 14:41
package chatswing;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.KeyEventDispatcher;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;//import com.chat.task.ReciveMsgTask;//import com.chat.utils.SwingUtil;public class GroupChatView extends JFrame implements ActionListener {public static final int PORT = 8888;//消息列表 文本域JTextArea msgListText;//聊天者姓名JTextField name;//消息输入框JTextField msg;//发送按钮JButton sendbnt;public GroupChatView() {this.setTitle("畅聊吧");this.setSize(650, 700);// 650-80=570-10=560this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);//SwingUtil.toScreenCenter(this);msgListText = new JTextArea();//滚动面板,使消息列表带滚动的JScrollPane msgScroll = new JScrollPane(msgListText);this.getContentPane().add(msgScroll);//底部条:姓名框-消息框-发送按钮JPanel bottomBar = new JPanel(new FlowLayout(FlowLayout.LEFT));name = new JTextField("wtyy");name.setPreferredSize(new Dimension(80, 35));bottomBar.add(name);msg = new JTextField();//设置组件的显示大小(适用于所有的组件)msg.setPreferredSize(new Dimension(430, 35));//添加一个键盘事件,判断是回车就发送消息msg.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {if (e.getKeyCode() == KeyEvent.VK_ENTER) {sendMsg();}}});//发送按钮,点击发送就发送消息bottomBar.add(msg);sendbnt = new JButton("发送");sendbnt.setPreferredSize(new Dimension(80, 35));sendbnt.addActionListener(this);bottomBar.add(sendbnt);this.getContentPane().add(bottomBar, BorderLayout.SOUTH);// 开启一个线程接收消息,一直在接收信息new Thread(new ReciveMsgTask(msgListText)).start();}public static void main(String[] args) {GroupChatView gv = new GroupChatView();gv.setVisible(true);}//监听点击按钮的时间@Overridepublic void actionPerformed(ActionEvent e) {sendMsg();}//发送消息的方法private void sendMsg() {try {DatagramSocket ds = new DatagramSocket();String msgTxt = name.getText() + ":" + msg.getText();msg.setText("");byte[] bu = msgTxt.getBytes();DatagramPacket p = new DatagramPacket(bu, bu.length, InetAddress.getByName("255.255.255.255"), PORT);ds.send(p);} catch (Exception ex) {System.out.println("发送失败");}}}

接收消息的类:

package chatswing;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import javax.swing.JTextArea;//import com.chat.main.GroupChatView;public class ReciveMsgTask implements Runnable {private JTextArea msgListText;public ReciveMsgTask(JTextArea msgListText) {this.msgListText = msgListText;}@Overridepublic void run() {try {DatagramSocket ds = new DatagramSocket(GroupChatView.PORT);//线程一直在接收信息while (true) {byte[] by = new byte[1024];DatagramPacket rp = new DatagramPacket(by, by.length);ds.receive(rp);String msgTxt = new String(by, 0, rp.getLength());InetAddress addres = rp.getAddress();//获取发送信息的人的机器信息msgTxt = addres.getHostAddress() + "(" + addres.getHostName() + "\r\n" + msgTxt;//处理历史信息为空的情况不加换行String historyMsg = msgListText.getText();if (historyMsg != null && !historyMsg.equals("")) {msgTxt = historyMsg + "\r\n" + msgTxt;}msgListText.setText(msgTxt);//让文本域,滚到最底部msgListText.setCaretPosition(msgListText.getDocument().getLength());}} catch (Exception e) {e.printStackTrace();}}}

这就实现了群聊。

原创粉丝点击