12. Socket

Socket(了解)

服务于服务器。

1.计算机网络

计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在==网络操作系统,网络管理软件及网络通信协议的管理==和协调下,实现资源共享和信息传递的计算机系统。

目的: 资源传输

购买一台笔记本:
   系统软件:
 在百度访问数据:
    百度服务器开启----> http://www.baidu.com   域名: ip+端口   http://192.168.1.1:7878

2. 网络编程

2.1 IP

每台计算机都有唯一的ip与之相对应的   每一台计算机都有默认ip  127.0.0.1 == localhost(映射出来的域名)
 查看计算机的ip:  ipconfig
ipv4: 
   255.255.255.255  A-D
       192.168.*.*
                     
ipv6: fe80::35d6:50e0:186c:e2bb%15   

2.2 端口

0-65535  0-1023 系统自动保留
    一个端口就是一个应用程序。
 http:80
 https:443
 tomcat:8080
 mysql:3306    

2.3 协议

河南:
 开封  南阳  (河南话)
河南省  广东省
 (河南话)  (广东话)  ---> 普通话
     
中国    美国
  汉语   英语  ---> 英语
     
IP: 计算机协议   制定的规则。
    
tcp/ip: 传输控制协议/计算机协议(最多) https://www.baidu.com/  打电话  必须在连接成功的状态下  执行数据交互
  http/ftp/smtp/pop3
    http: 超文本传输协议
    https: 数字安全证书    

UDP: 用户数据报协议  发短信    不管对方是否应当  都会将数据发送成功
       
  1.传输内容大小  tcp/ip: 内容无限的    UDP: 64kb
  2.传输效率 tcp/ip: 慢    UDP:3.安全性  tcp/ip(4/3次握手)   UDP: 低   http 
  4.数据传输方式 tcp/ip:  IO     UDP: 包   丢包
  5.使用场景:    
      tcp/ip: http  b/s:  browser/server: 浏览器和服务器
      java: 服务端语言  web(后端)开发     整个web项目: 前端   +  后端  
          
    C/S: client/server   (大前端)客户端:  pc端 ios  android  一般都是写接口
        
        
 前后没有分离: jsp+servlet+vue
 半分离的web项目:   后端写js  SSH   SSM
 完全分离的web项目: 前后端完全分离 程序员专心写接口  ssm      

3. Socket

两个进程间可以通过一个双向的网络通信连接实现数据交换,这种通信链路的端点被称为“套接字”(Socket)

3.1 InetAddress

java.net.* 
IP地址是由IP使用的32位或128位无符号数字
    
/Enumeration 等同于 Iterator
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();

            while(interfaces.hasMoreElements()){
                NetworkInterface anInterface = interfaces.nextElement();
                //获得列表里面的所有ip
                Enumeration<InetAddress> inetAddresses = anInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()){
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if(inetAddress instanceof Inet4Address){
                        System.out.println("ipv4:"+inetAddress);
                    }else if(inetAddress instanceof  Inet6Address){
                        System.out.println("ipv6:"+inetAddress);
                    }

                }
                System.out.println("----------------------------------");
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }

3.2 URL

代表万维网资源指针。 统一资源定位符

调用接口----> 接口就是一个路径

private static void demo1() {

        String path = "ftp://www.baidu.com/demo/user/login.html?pass=1234&name=admin#aaa";
        try {
            URL url = new URL(path);

            //获得资源路径的组成部分的内容
            System.out.println(url.getAuthority());
            System.out.println(url.getProtocol());
            System.out.println(url.getPort());// 没有端口  -1
            System.out.println(url.getDefaultPort());//协议默认端口  80
            System.out.println(url.getFile());///demo/user/login.html?pass=1234&name=admin
            System.out.println(url.getHost());
            System.out.println(url.getQuery());//pass=1234&name=admin ?
            System.out.println(url.getRef());//#后面的数据   锚记  a
            System.out.println(url.getContent());
            System.out.println(url.openStream());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
private static void demo2() {
        //调用接口获得数据
        //动态获得指定城市每天天气数据
        String weatherPath = "http://t.weather.itboy.net/api/weather/city/101010500";
        try (
                BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(weatherPath).openStream()));
        ) {
            String s = "";
            while ((s = reader.readLine()) != null) {
                System.out.println(s);
            }
            //HttpClient
            //后期将字符串转json  (转map集合)

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3.3 Socket编程

2个进程 一个客户端 一个服务端

1. 基于TCP/IP

http: 连接安全可靠  无状态   IO   read/write
  1. 创建服务端程序   服务器开启  ServerSocket  ServerSocket(int port)   0-65535  
  public static void main(String[] args) {

        final String serverName = "server:";
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            Scanner scanner = new Scanner(System.in);
            System.out.println("------------服务端开启-------------");
            while (true) {
                Socket client = serverSocket.accept();

                while (true) {
                    //写
                    DataOutput dataOutput = new DataOutputStream(client.getOutputStream());
                    System.out.print(serverName);
                    dataOutput.writeUTF(serverName + scanner.nextLine());

                    //读
                    DataInput dataInput = new DataInputStream(client.getInputStream());
                    System.out.println(dataInput.readUTF());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}    
1. 创建客户端  主动连接服务端程序
public static void main(String[] args) {
        final String clientName = "client:";
        Scanner scanner = new Scanner(System.in);

        try {
            Socket socket = new Socket("localhost", 8888);
            while (true) {
                //读
                DataInput dataInput = new DataInputStream(socket.getInputStream());
                System.out.println(dataInput.readUTF());

                //写
                DataOutput dataOutput = new DataOutputStream(socket.getOutputStream());
                System.out.print(clientName);
                dataOutput.writeUTF(clientName + scanner.nextLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2. 基于UDP

DatagramSocket
类表示用于发送和接收数据报数据包的套接字。 
DatagramPacket
该类表示数据报包。
    DatagramPacket(byte[] buf, int length, InetAddress address, int port) 发送数据
    DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port) 发送数据
    DatagramPacket(byte[] buf, int length)  接收数据
    DatagramPacket(byte[] buf, int offset, int length) 接收数据
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值