Java study 1:The note of studying Socket which based UDP

  1. UDP concept:

      UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参考模型中一种无连接传输层协议,提供面向事务的简单不可靠信息传送服务,IETF RFC 768是UDP的正式规范。UDP在IP报文的协议号是17。(from baike)

  2. UDP program:

      因爲UDP協議是一種無連接的協議,所以

        (1)、每次發送數據並不需要綁定,只需要使用DatagramPacket()構造好對應的數據包就可以了;

        (2)、沒有像TCP一樣的ServerSocket 與Socket之分,雙方都用DatagramPacket初始化;

         (3)、發送接收用send(數據包),receive(數據包)

  下面上代碼:

  *UDP並無Server與Client之分,但是代碼一個發送一個接受就起了這名字(這並不重要~逃)

  *發送成功之後雙方就建立了一個鏈接(虛),可以互發數據

 1 import java.io.IOException;
 2 import java.net.DatagramPacket;
 3 import java.net.DatagramSocket;
 4 import java.net.SocketException;
 5 
 6 public class UdpServer {
 7     public static void main(String[] args) {
 8         DatagramSocket dgs = null;
 9         try {
10             dgs=new DatagramSocket(8888);
11             //receive
12             byte[] buf=new byte[256];
13             int length=256;
14             DatagramPacket dgp=new DatagramPacket(buf, length);
15             dgs.receive(dgp);
16             System.out.println("receive from client:"+new String(buf, 0, length));
17             
18             //send
19             String str="yeah i have receivered~";
20             System.out.println(dgp.getAddress());//use last time's packet to get the address 
21             System.out.println(dgp.getPort());
22             DatagramPacket dgp_send=new DatagramPacket(str.getBytes(),str.length(), dgp.getAddress(), dgp.getPort());
23             dgs.send(dgp_send);
24             dgs.close();
25         } catch (SocketException e) {
26             e.printStackTrace();
27         } catch (IOException e) {
28             e.printStackTrace();
29         }finally{
30             dgs.close();
31         }
32     }
33 }
UdpServer
 1 import java.net.DatagramPacket;
 2 import java.net.DatagramSocket;
 3 import java.net.InetAddress;
 4 
 5 public class UdpClient {
 6     public static void main(String[] args) {
 7         DatagramSocket dgs = null;
 8         try {
 9             dgs=new DatagramSocket();
10             //send->server
11             String str="so dirty haha~";
12             DatagramPacket dgp_send=new DatagramPacket(str.getBytes(),0,str.length(),InetAddress.getByName("localhost"),8888);
13             dgs.send(dgp_send);
14             //receive from server
15             byte[] buf=new byte[256];
16             DatagramPacket dgp2_receive=new DatagramPacket(buf, 256);
17             dgs.receive(dgp2_receive);
18             System.out.println("receive from server:"+new String(buf,0,256));
19             dgs.close();
20             
21             
22         } catch (Exception e) {
23             System.out.println("error:"+e);
24         }
25     }
26 }
UdpClient


p.s.I:Server 的 send 部分使用之前用來接收數據的包dgp來取得發送者的address以及port,然後再用新的DatagramPacket來發送數據

p.s.II:DatagramPacket的搆造函數中,byte[]用來放數據,length用來放長度,程序簡單所以數字亂寫的(ha

  

posted @ 2015-04-11 17:09  七喜哥哥仔  阅读(135)  评论(0编辑  收藏  举报