黑馬程序員——Java網絡編程之UDP傳輸

                                     android培訓java培訓、期待與您交流!  java

網絡編程android

網絡模型
通信要素:InetAddress(對象):ip地址,網絡中設備的標識,不可記憶,可用主機名,本地迴環地址:127.0.0.1主機名localhost
端口號
傳輸協議:UDP,將數據的源及目的封裝成數據包中,不須要創建鏈接,每一個數據包的大小限制在64K內,無鏈接,是不可靠協議,不須要創建鏈接,速度快。力求速度,不求數據的準確性。好比聊天軟件,網絡會議。
TCP:創建鏈接,造成傳輸數據的通道,在鏈接中進行大數據量傳輸,經過三次握手完成鏈接,是可靠協議,必須創建鏈接效率稍低。
Socket
Socket就是爲網絡服務提供的一種機制,通訊的兩端都要有Socket。網絡間的通訊其實就是Socket間的通訊,網絡通訊就是兩個Socket間經過I/O傳輸。
UDP傳輸:DatagramSocket和DatagramPacket根據包中的信息從一臺機器投遞到另外一臺機器。凡是帶端口的都是用來構造發送數據包的。寫Socket要理解流程。
class Send implements Runnable{
           //1.經過DatagramSocket,創建 Scoket端點
private DatagramSocket s ;
public Send( DatagramSocket s){
           thisss;
}
           public void run() {
                    //獲取控制檯數據
                   BufferedReader buffreader =new BufferedReader(new InputStreamReader(System.in)) ;
                   String str ;
                    byte[] buff =new byte[ 1024];
                    try {
                              while((str =buffreader .readLine ())!= null){
                                       if("886" .equals (str))
                                                 break;
                                                          buff =str .getBytes ();
                              //2.將數據經過DatagramPacket將數據封裝成數據包,要包含數據,目標IP和目標端口
                                      DatagramPacket data=
                                                           new DatagramPacket( buffbuff.length,InetAddress.getByName("127.0.0.1") ,10071 );
                                       //3.調用Socket的send方法,發送數據。
                                       ssend(data );
                                       sclose();
                              }        
                    } catch (Exception e ) {
                              // TODO: handle exception
                              throw new RuntimeException("chulibuliao" );
                    }
           }
          
}
class Rece implements Runnable{
           //1.經過DatagramSocket創建Soket服務。接收端創建Socket服務的時候通常要監聽一個端口。
           private DatagramSocket s ;
           public Rece( DatagramSocket s){
                    thisss;
           }
           public void run() {
                    // TODO Auto-generated method stub
                    try {
                              while ( true){
                                       //2.定義數據包,用來接受源傳送過來的數據包
                                                 byte[] buff =new byte[ 1024];
                                                                   DatagramPacket dp=new DatagramPacket(buffbuff.length) ;
                                       //3.接收源的數據包,裝入預先定義好的數據包
                                                                    sreceive(dp );
                                       //4.將不一樣的數據和信息分別取出
                                                                   String ip=dp.getAddress() .getHostAddress ();
                                                                                      String data=new String(dp.getData() ,0 ,dp .getLength ());
                                                                    System .out.println( ip">>>"+data );
                                                                    sclose();
                              }
                    } catch (Exception e ) {
                              // TODO: handle exception
                              throw new RuntimeException("shicu" );
                    }
           }
}
public class WeChat {
 
           public static void main (String[] args) throws Exception {
                    // TODO Auto-generated method stub
DatagramSocket sendSocket=new DatagramSocket();
DatagramSocket receSocket=new DatagramSocket(10071 )//接收端須要監聽窗口
new Thread( new Send( sendSocket)).start() ;
new Thread( new Rece( receSocket)).start() ;
           }
 
}
 

                                                        android培訓java培訓、期待與您交流!  編程

相關文章
相關標籤/搜索