1. 程式人生 > >關於Java的UDP通訊連線實現

關於Java的UDP通訊連線實現

知識點:

1.關於TCP與UDP的主要區別:
TCP—傳輸控制協議,提供的是面向連線、可靠的位元組流服務。當客戶和伺服器彼此交換資料前,必須先在雙方之間建立一個TCP連線,之後才能傳輸資料。TCP提供超時重發,丟棄重複資料,檢驗資料,流量控制等功能,保證資料能從一端傳到另一端。
UDP—使用者資料報協議,是一個簡單的面向資料報的運輸層協議。UDP不提供可靠性,它只是把應用程式傳給IP層的資料報傳送出去,但是並不能保證它們能到達目的地。由於UDP在傳輸資料報前不用在客戶和伺服器之間建立一個連線,且沒有超時重發等機制,故而傳輸速度很快.

2.關於InetAddress的用法:


IP地址是IP使用的32位(IPv4)或者128位(IPv6)位無符號數字,它是傳輸層協議TCP,UDP的基礎。InetAddress是Java 對IP地址的封裝,在java.net中有許多類都使用到了InetAddress,包括 ServerSocket,Socket,DatagramSocket等等。
InetAddress的例項物件包含以數字形式儲存的 IP地址,同時還可能包含主機名(如果使用主機名來獲取InetAddress的例項,或者使用數字來構造,並且啟用了反向主機名解析的功能)。 InetAddress類提供了將主機名解析為IP地址(或反之)的方法。
InetAddress對域名進行解析是使用本地機器配置或者網路命名服務(如域名系統(Domain Name System,DNS)和網路資訊服務(Network Information Service,NIS))來實現。對於DNS來說,本地需要向DNS伺服器傳送查詢的請求,然後伺服器根據一系列的操作,返回對應的IP地址,為了提高效率,通常本地會快取一些主機名與IP地址的對映,這樣訪問相同的地址,就不需要重複傳送DNS請求了。在java.net.InetAddress類同樣採用了這種策略。在預設情況下,會快取一段有限時間的對映,對於主機名解析不成功的結果,會快取非常短的時間(10秒)來提高效能。

3.Java中的DatagramPacket與DatagramSocket的初步
DatagramPacket表示存放資料的資料報,DatagramSocket表示接受或傳送資料報的套接字

程式碼實現

1.構建UDP伺服器
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package udp;

import java.io.IOException;
import java.net.DatagramPacket;
import
java.net.DatagramSocket; /** * * @author Administrator */ public class EchoUDPServer { private int port=6666; private DatagramSocket socket; public EchoUDPServer() throws IOException{ socket =new DatagramSocket(port); System.out.println("UDP伺服器已啟動....."); } public String echo(String msg){ return "server :"+msg; } public void service(){ while(true){ try{ DatagramPacket packet =new DatagramPacket(new byte[512],512); socket.receive(packet); String msg = new String(packet.getData(),0,packet.getLength(),"GB2312"); System.out.println(packet.getAddress()+" : "+packet.getPort()+">"+msg); packet.setData(echo(msg).getBytes("GB2312")); socket.send(packet); }catch(IOException e){ System.out.println(e); } } } public static void main(String args[])throws IOException{ new EchoUDPServer().service(); } }
2.UDP客戶端
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 *
 * @author Administrator
 */
public class EchoUDPClient {
  private int       remotePort;
  private InetAddress remoteIP;

  private DatagramSocket socket; //UDP套接字 

  public EchoUDPClient(String ip,String port)throws IOException{
      this.remotePort=Integer.parseInt(port);
      this.remoteIP=InetAddress.getByName(ip);

      //建立一個UDP套接字,與本地任意一個未使用的UDP埠繫結
      socket=new DatagramSocket(); 
      //與本地一個固定的UDP埠繫結
  //   socket=new DatagramSocket(9000);       
  }

  //定義一個數據的傳送方法。
  public void send(String msg){
    try {
        //先準備一個待發送的資料報
        byte[] outputData=msg.getBytes("GB2312");
        //構建一個數據報文。  
        DatagramPacket outputPacket=new DatagramPacket(outputData,
                                 outputData.length,remoteIP,remotePort);
        //給EchoUDPServer傳送資料報
        socket.send(outputPacket);  //給EchoUDPServer傳送資料報
    } catch (IOException ex) { }
  }

  //定義一個數據的接收方法。
  public String receive(){//throws IOException{
    String msg;
    //先準備一個空資料報文
    DatagramPacket inputPacket=new DatagramPacket(new byte[512],512);
      try {
          //阻塞語句,有資料就裝包,以裝完或裝滿為此.
          socket.receive(inputPacket);
          //從報文中取出位元組資料並裝飾成字元。
          msg=new String(inputPacket.getData(),
                         0,inputPacket.getLength(),"GB2312");
      } catch (IOException ex) {
        msg=null;
      }
    return msg;
  }

  public void close(){
    if(socket!=null)  
       socket.close();//釋放本地埠.
  }
}

3.使用者介面

這裡寫圖片描述

    程式碼:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package udp;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class EchoUDPClinetFrame extends javax.swing.JFrame {
    private EchoUDPClient ec;
    private String msg;
    /**
     * Creates new form IOJFrame
     */
    public EchoUDPClinetFrame() throws IOException {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("IP地址");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel2.setText("埠");

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jButton2.setText("連線");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTextArea1.setEditable(false);
        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jTextArea1CaretUpdate(evt);
            }
        });
        jTextArea1.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                jTextArea1AncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jScrollPane1.setViewportView(jTextArea1);

        jLabel3.setText("資訊顯示區");

        jLabel4.setText("資訊錄入區");

        jScrollPane2.setAutoscrolls(true);

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jTextArea2.setAutoscrolls(false);
        jScrollPane2.setViewportView(jTextArea2);

        jButton3.setText("傳送");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("退出");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jScrollPane2))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton3)
                        .addGap(38, 38, 38)
                        .addComponent(jButton4)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addGap(8, 8, 8)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        //傳送按鈕
        msg=jTextArea2.getText();
        jTextArea1.append("已傳送 : "+msg+"\n");
        jTextArea2.setText(null);
        try {
            ec.send(msg);//傳送一串字元。
        } catch (Exception ex) {
            Logger.getLogger(EchoUDPClinetFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        //連線按鈕
        String ip;
        String port;
         if(!jTextField1.getText().equals("")){
          ip=jTextField1.getText();
          port=jTextField2.getText();
         }
         else{
             ip = "222.201.101.15";
             port = "6666";
         }
    try {
        ec=new EchoUDPClient(ip,port);
        jTextArea1.append("UDP建立成功.\r\n");
        Thread receiver = new Thread(){
            @Override
            public void run(){
                String msg = null;
                while(true){
                    try{
                        msg = ec.receive();
                } catch(Exception ex){
                    jTextArea1.append("套接字異常關閉"+"\n");
                }
                if(msg!=null){
                    jTextArea1.append(msg+"\n");
                }
                else{
                    jTextArea1.append("你的對話已關閉!"+"\n");
                    break;
                }
            }
        }};
        receiver.start();
    } catch (IOException ex) {
       jTextArea1.append("UDP建立失敗.\r\n");
    } 

    }                                        

    private void jTextArea1CaretUpdate(javax.swing.event.CaretEvent evt) {                                       
        // TODO add your handling code her
    }                                      

    private void jTextArea1AncestorAdded(javax.swing.event.AncestorEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws IOException {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EchoUDPClinetFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EchoUDPClinetFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EchoUDPClinetFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EchoUDPClinetFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                   EchoUDPClinetFrame ioj =new EchoUDPClinetFrame();
                    ioj.setVisible(true);

                } catch (IOException ex) {
                    Logger.getLogger(EchoUDPClinetFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration
}