黑马程序员——Java网络编程之UDP传输

时间:2022-12-18 00:19:22

                                     android培训、java培训、期待与您交流!  

网络编程

网络模型
黑马程序员——Java网络编程之UDP传输
通讯要素: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培训、期待与您交流!