简易聊天室--UDP协议.doc_第1页
简易聊天室--UDP协议.doc_第2页
简易聊天室--UDP协议.doc_第3页
简易聊天室--UDP协议.doc_第4页
简易聊天室--UDP协议.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

import .DatagramPacket;import .DatagramSocket;import .UnknownHostException;import .SocketException;import .InetAddress;import .Socket;import .ServerSocket;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.Color;import java.awt.Font;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JLabel;import javax.swing.JButton;import javax.swing.JTextField;import javax.swing.JTextArea;import javax.swing.JScrollPane;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;class QQ_UDP implements ActionListener,RunnableJFrame JFm = new JFrame(腾讯QQ-UDP协议Alex.。制作:);JTextArea dialogArea = new JTextArea();JScrollPane JSP = new JScrollPane(dialogArea);/为聊天区,添加滚动 窗格,如果不是自动的换行,就必须用滚动窗JLabel Goal_IP_lable = new JLabel(目标地址:);JLabel Send_port_lable = new JLabel(发送端口:);JLabel Receive_port_lable = new JLabel(接收端口:);JTextField msgField,sendPortField,receivePortField,Goal_IP_Field;JButton startJBt = new JButton(开始);JButton sendJBt = new JButton(发送);JPanel Jpl_North,Jpl_South;/南北面板Thread Td = null;DatagramPacket sendPack,receivePack;DatagramSocket sendSocket,receiveSocket;InetAddress goal_IP;int sendPort,receivePort;byte inBuf = new byte1024;byte outBuf = new byte1024;Socket ToQQClient;ServerSocket serverSocket;DataInputStream inFromQQClient;DataOutputStream outToQQClient;public QQ_UDP()dialogArea.setEditable(false);/不可编辑dialogArea.setLineWrap(true);/自动换行dialogArea.setBackground(Color.MAGENTA);dialogArea.setFont(new Font(宋体,Font.BOLD,18);sendPortField = new JTextField();sendPortField.setColumns(5);receivePortField = new JTextField();receivePortField.setColumns(5);Goal_IP_Field = new JTextField();Goal_IP_Field.setColumns(8);startJBt.addActionListener(this);Jpl_North = new JPanel();Jpl_North.setLayout(new FlowLayout();Jpl_North.setBackground(Color.gray);Jpl_North.add(Receive_port_lable);Jpl_North.add(receivePortField);Jpl_North.add(Send_port_lable);Jpl_North.add(sendPortField);Jpl_North.add(Goal_IP_lable);Jpl_North.add(Goal_IP_Field);Jpl_North.add(startJBt);msgField = new JTextField();msgField.setColumns(40);msgField.setEditable(false);msgField.addActionListener(this);sendJBt.setEnabled(false);sendJBt.addActionListener(this);Jpl_South = new JPanel();Jpl_South.setLayout(new FlowLayout();Jpl_South.setBackground(Color.CYAN);Jpl_South.add(msgField);Jpl_South.add(sendJBt);JFm.add(JSP,BorderLayout.CENTER);JFm.add(Jpl_North,BorderLayout.NORTH);JFm.add(Jpl_South,BorderLayout.SOUTH);JFm.setSize(600,400);JFm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JFm.setVisible(true);public void actionPerformed(ActionEvent e)tryif(e.getSource()=startJBt)/点击开始sendPort = Integer.parseInt(sendPortField.getText();goal_IP = InetAddress.getByName(Goal_IP_Field.getText();sendSocket = new DatagramSocket();/构造数据报套接字并将其绑定到本地主机上任何可用的端口。receivePort = Integer.parseInt(receivePortField.getText();receivePack = new DatagramPacket(inBuf,1024);/ 构造 DatagramPacket,用来接收长度为 length 的数据包receiveSocket = new DatagramSocket(receivePort);/创建数据报套接字并将其绑定到本地主机上的指定端口。Td = new Thread(this);Td.setPriority(Thread.MIN_PRIORITY);Td.start();startJBt.setEnabled(false);sendJBt.setEnabled(true);msgField.setEditable(true);else/点击发送或者 按回车outBuf = msgField.getText().getBytes();sendPack = new DatagramPacket(outBuf,outBuf.length,goal_IP,sendPort);/用来将长度为 length 的包发送到指定主机上的指定端口号sendSocket.send(sendPack);dialogArea.append(我:+msgField.getText()+n);msgField.setText(null);catch(UnknownHostException UE)dialogArea.append(无法连接到指定地址n);catch(SocketException SE)dialogArea.append(无法打开指定端口n);catch(IOException IE)dialogArea.append(发送数据失败n);catch(Exception E)dialogArea.append(端口初始化失败n);public void run()String St;while(true)tryreceiveSocket.receive(receivePack);St = new String(receivePack.getData(),0,receivePack.getLength();di

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论