关闭

UDP_program

349人阅读 评论(0) 收藏 举报

package comm;
/**
 * @docRoot:UDP程序
 * @author gsp
 * @date:2007-09-18
 * @telephone:80889574
 */
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class DataSocket  extends JFrame implements Runnable{
 private static final long serialVersionUID = 1L;
 
 /**程序的显示图标**/
 private ImageIcon icon = new ImageIcon(DataSocket.class.getResource("qq.gif"));
 /**中间的面板**/
 private JPanel pan_center;
 /**聊天记录的滑块**/
 private JScrollPane jsp_scroll_records;
 /**聊天内容**/
 private JTextArea text_chatRecord;
 /**下面的面板**/
 private JPanel pan_bottom;
 /**需要发送的消息**/
 private JTextField text_message;
 /**发送按钮**/
 private JButton btn_send;
 /**接受到的数据**/
 byte[] buf = new byte[256];
 /**建立接受的套间字**/
 private DatagramPacket dp = new DatagramPacket(buf,buf.length);
 
 /**构造方法**/
 public DataSocket()
 {
  try{
   jinint();
  }catch(Exception e)
  {
   JOptionPane.showMessageDialog(this,"应用程序初始化失败");
  }
 }
 /**应用程序初始化**/
 private void jinint()
 {
  /**添加聊天记录**/
  text_chatRecord = new JTextArea(12,35);
  text_chatRecord.setFont(new Font("黑体",1,12));
  text_chatRecord.setEditable(false);
  jsp_scroll_records = new JScrollPane(text_chatRecord);
  pan_center = new JPanel();
  pan_center.add(jsp_scroll_records);
  this.getContentPane().add(pan_center,BorderLayout.CENTER);
  /**添加发送消息的模块**/
  pan_bottom = new JPanel();
  text_message = new JTextField(20);
  text_message.setFont(new Font("黑体",1,12));
  pan_bottom.add(text_message);
  btn_send = new JButton("发送消息");
  pan_bottom.add(btn_send);
  this.getContentPane().add(pan_bottom,BorderLayout.SOUTH);
  /**窗体的外观**/
  this.setTitle("网络聊天程序");
  this.setIconImage(icon.getImage());
  Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  this.setLocation((screen.width-400)/2,(screen.height-300)/2);
  this.setSize(400,300);
  this.setResizable(false);
  /**添加关闭的监听**/
  this.addWindowListener(new WindowListener(){
   public void windowOpened(WindowEvent arg0) {}
   public void windowClosing(WindowEvent arg0) {}
   public void windowClosed(WindowEvent arg0) {
    dispose();
    System.exit(0);
   }
   public void windowIconified(WindowEvent arg0) {}
   public void windowDeiconified(WindowEvent arg0) {}
   public void windowActivated(WindowEvent arg0) {}
   public void windowDeactivated(WindowEvent arg0) {}
  });
  /**添加btn_send的监听**/
  btn_send.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0) {
      sendMessage();
   }
  });  
  this.setVisible(true);
  Thread thread = new Thread(this);
  thread.start();
 }
 /**发送消息**/
 private void sendMessage(){
  try
  {
   if(!text_message.getText().equalsIgnoreCase(""))
   {
          byte[] send_message_str = text_message.getText().getBytes();
          InetAddress address = InetAddress.getByName("localhost");
          DatagramPacket paket = new DatagramPacket(send_message_str,send_message_str.length,address,3000);
          DatagramSocket socket = new DatagramSocket();
          Calendar calendar = Calendar.getInstance();
          String  year = calendar.get(Calendar.YEAR)+"-";
          String month = calendar.get(Calendar.MONTH)<10?"0"+calendar.get(Calendar.MONTH)+"-":calendar.get(Calendar.MONTH)+"-";
          String day = calendar.get(Calendar.DATE) <10?"0"+calendar.get(Calendar.DATE)+"  ":calendar.get(Calendar.DATE)+"  ";
          String hour = calendar.get(Calendar.HOUR_OF_DAY) < 10?"0"+calendar.get(Calendar.HOUR_OF_DAY)+":":calendar.get(Calendar.HOUR_OF_DAY)+":";
          String minute = calendar.get(Calendar.MINUTE)<10?"0"+ calendar.get(Calendar.MINUTE)+":":calendar.get(Calendar.MINUTE)+":";
          String second = calendar.get(Calendar.SECOND)<10?"0"+calendar.get(Calendar.SECOND)+"":calendar.get(Calendar.SECOND)+"  ";
          text_chatRecord.append(year+month+day+hour+minute+second+"DataSocket:/n"+text_message.getText()+"/n");
          socket.send(paket);
   }
  }catch(Exception e)
  {}
 }
 /**test**/
 public static  void  main(String[] args)
 {
  new DataSocket();
 }
 /**线程自动自行的方法**/
 public void run() {
  try
  {
   DatagramSocket socket = new DatagramSocket(3001);
   while(true)
   {
    socket.receive(dp);
    String receive_message = new String(dp.getData());
    Calendar calendar = Calendar.getInstance();
       String  year = calendar.get(Calendar.YEAR)+"-";
       String month = calendar.get(Calendar.MONTH)<10?"0"+calendar.get(Calendar.MONTH)+"-":calendar.get(Calendar.MONTH)+"-";
       String day = calendar.get(Calendar.DATE) <10?"0"+calendar.get(Calendar.DATE)+"  ":calendar.get(Calendar.DATE)+"  ";
       String hour = calendar.get(Calendar.HOUR_OF_DAY) < 10?"0"+calendar.get(Calendar.HOUR_OF_DAY)+":":calendar.get(Calendar.HOUR_OF_DAY)+":";
       String minute = calendar.get(Calendar.MINUTE)<10?"0"+ calendar.get(Calendar.MINUTE)+":":calendar.get(Calendar.MINUTE)+":";
       String second = calendar.get(Calendar.SECOND)<10?"0"+calendar.get(Calendar.SECOND)+"":calendar.get(Calendar.SECOND)+"  ";
       text_chatRecord.append(year+month+day+hour+minute+second+"DataSocket1:/n"+receive_message+"/n");
   }
  }catch(Exception e){}
 }
}  

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:132857次
    • 积分:2918
    • 等级:
    • 排名:第12032名
    • 原创:163篇
    • 转载:1篇
    • 译文:0篇
    • 评论:5条
    文章分类