zoukankan      html  css  js  c++  java
  • UDP与TCP

    网络模型分为OSI参考模型和TCP/IP参考模型

    两台主机之间传输联系

    TCP和UDP就是传输层的东西。叫做传输控制协议和用户数据报协议两者的区别是

    网络通信底层通过Socket实现。

    UDP:Socket对象用DatagramSocket封装,构造方法如下

    数据报对象用DatagramPacket封装

    TCP  客户端 用 Sokect类进行封装。

        服务器端用 SeverSokect类进行封装。

    而网络互联需要Ip地址,ip地址在java里面用InetAddress类进行封装。

     UDP

     UDP我们平常用的QQ就是这样,他不需要知道你在不在线,只要把东西发给你就好。基于UDP做一个小的聊天室,代码贴在下面

     UDP的数据源和目的都封装在数据报中,只需要发送和接收数据报就可以

    //发送方
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    public class Send implements Runnable {
        private DatagramSocket ds;
    
        public Send(DatagramSocket ds) {
            this.ds = ds;
        }
    
        public void run() {
            try {
                BufferedReader bf = new BufferedReader(new InputStreamReader(
                        System.in));
                String line = null;
    
                while ((line = bf.readLine()) != null) {
                    byte[] buf=line.getBytes();
                    DatagramPacket dp = new DatagramPacket(buf, buf.length,
                            InetAddress.getByName("localhost"), 12002);
                    ds.send(dp);
                    if("再见".equals(line))
                        break;
    
                }
                ds.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
    //接收方
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    
    public class Recive implements Runnable {
        private DatagramSocket ds;
    
        public Recive(DatagramSocket ds) {
            this.ds = ds;
        }
    
        public void run() {
            try {
                while (true) {
                    byte[] buf = new byte[1024];
                    DatagramPacket dp = new DatagramPacket(buf, buf.length);
                    ds.receive(dp);
                    String ip = dp.getAddress().getHostAddress();
                    String text=new String(dp.getData(), 0, dp.getLength());
                    System.out.println(ip + ":" + text);
                    if (text.equals("再见")) {
                        System.out.println(ip + "退出聊天室");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }
    import java.net.DatagramSocket;
    import java.net.SocketException;
    
    public class Run {
    public static void main(String[] args) throws SocketException  {
         DatagramSocket send=new DatagramSocket();
         DatagramSocket recive=new DatagramSocket(12002);
        new Thread(new Send(send)).start();
        new Thread(new Recive(recive)).start();
    }
    }

    基于多线程的实现,在一个窗口内既可以接收也可以发送。

    TCP

    TCP协议呢需要建立连接,建立连接以后才能通信,那怎么通信呢?必然是通过流。

     建立一个基于TCP协议来实现的小聊天室。

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class Client{
     public static void main(String[] args) throws UnknownHostException, IOException{
         Socket s=new Socket(InetAddress.getByName("localHost"), 10002);
         
         PrintWriter p=new PrintWriter(s.getOutputStream(),true);
         BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
         String line=null;
         while((line=bf.readLine())!=null){
             p.println(line);
             if("再见".equals(line))
                 break;
             
         }
         s.close();
         bf.close();
     }
    }

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class Server{
     public static void main(String[] args) throws UnknownHostException, IOException{
         Socket s=new Socket(InetAddress.getByName("localHost"), 10002);
         
         PrintWriter p=new PrintWriter(s.getOutputStream(),true);
         BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
         String line=null;
         while((line=bf.readLine())!=null){
             p.println(line);
             if("再见".equals(line))
                 break;
             
         }
         s.close();
         bf.close();
     }
    }
  • 相关阅读:
    TabControl 切换 内嵌web页面直接响应滚动事件
    进程、应用程序域和对象上下文
    CSharp中的多线程——线程同步基础
    CSharp中的多线程——入门
    注重实效的程序员之快速参考指南
    学习语言技术快速入门——五步骤
    利用jQuery选择将被操作的元素
    CSharp中的多线程——使用多线程
    android开发文件介绍
    三角函数公式
  • 原文地址:https://www.cnblogs.com/wxw7blog/p/7679265.html
Copyright © 2011-2022 走看看