网络编程

118 阅读4分钟

计算机网络详解

一. 计算机网络

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

二. 网络编程的目的

传播交流信息,数据交换,通讯

三. 想要达到的效果需要做到什么

  1. 如何准确的定位到网络上的一台主机,定位到计算机上的某个资源。
  2. 找到了主机,如何传输数据?

网络通信的要素

  1. 获得双方地址
  • ip
  • 端口号
  1. 规则 :网络通信协议

    TCP/IP参考模型

9f55c0f6625647d5dc324fd08571516.jpg

  1. 小结:

    1. 网络编程中有两个主要的问题

      如何准确的定位到网络上的一台或多台主机

      找到主机后如何进行通讯

    2. 网络编程要素

      IP和端口号

      网络通讯协议

IP地址

ip地址:InetAddress

  • 唯一定位一台网络上的计算机

  • 127.0.0.1 本机的localhost

  • IP地址的分类

    1. IP地址分网 IPV4/IPV6
    • IPV4 127.0.0.1 ,四个字节组成,0~255 ,42亿

    • IPV6 128位 , 8个无符号整数

      2001:0bb2:aaaa:0015:0000:0000:1aaa:1312
      
    1. 公网(互联网)-私网(局域网)
    • ABCD类地址
    • 192.168.xx.xx , 专门给组织内部使用
  • 域名:记忆IP问题!

    1. IP : www.baidu.com
package lesson01;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class TestInetAddress {
    public static void main(String[] args) {
        try {
            // 查询本机地址
            InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress1);
            InetAddress inetAddress3 = InetAddress.getByName("localhost");
            System.out.println(inetAddress3);
            InetAddress inetAddress4 = InetAddress.getLocalHost();
            System.out.println(inetAddress4);

            // 查询网站地址
            InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress2);

            // 常用方法
            System.out.println(inetAddress2.getCanonicalHostName()); // 规范的名字
            System.out.println(inetAddress2.getHostAddress());  // ip
            System.out.println(inetAddress2.getHostName());  // 域名,或者电脑自己的名字
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }
}

端口

端口表示计算机上的一个程序的进程

  • 不同的进程有不同的端口号,用来区分软件

  • 被规定0~65535

  • 分为TCP,UDP端口:端口数65535 * 2 ,不同协议端口号可以一致,单个协议端口号不能冲突

  • 端口分类

    • 公有端口: 0~1023

    • 程序注册端口: 1014~49151,分给用户或者程序

      • Tomcat:8080
      • MySQL:3306
      • Oracle:1521
  • 动态,私有: 49532~65535

    natstat -ano  查看所有的端口
    natstat -ano|findstr "5900" 查看指定端口
    tastlist|findstr "8696"  查看指定端口的进程
    
package lesson01;

import java.net.InetSocketAddress;

public class TestInetSocketAddress {
    public static void main(String[] args) {
        InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
        InetSocketAddress socketAddress2 = new InetSocketAddress("location", 8080);
        System.out.println(socketAddress);
        System.out.println(socketAddress2);

        System.out.println(socketAddress.getAddress());
        System.out.println(socketAddress.getHostName());  // 地址 127.0.0.1
        System.out.println(socketAddress.getPort());  // 端口 8080
    }
}

通信协议

网络通信协议:速率,传输码率,代码结构……

TCP/IP协议簇:实际上是一种协议

重要:

  • TIC:用户传输协议(类似打电话,链接稳定,“三次握手,四次挥手”,分为客户端和服务端,传输完成后释放连接效率低)
  • UDP:用户数据报协议 (类似发短信,不连接不稳定,也分为客户端和服务端,不管有无准备都可以发送)
  • IP :网络互联协议

TCP

客户端

package lesson01;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

// 客户端
public class TcpClientDemo01 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        try {
            // 1.要知道服务器地址
            InetAddress serverIP = InetAddress.getByName("127.0.0.1");
            // 2. 端口号
            int port = 9999;
            // 3. 创建一个socket链接
            socket = new Socket(serverIP,port);
            // 4.发送消息
            os = socket.getOutputStream();
            os.write("你好,欢迎学习".getBytes());
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }
}

服务器

package lesson01;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

// 服务器
public class TcpServerDemo01 {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            // 1.我得有一个地址
            serverSocket = new ServerSocket(9999);
            // 2.等待客户端连接
            socket = serverSocket.accept();
            // 3.读取客户端的消息
            is = socket.getInputStream();
            // 管道流
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer))!=-1){
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭资源
            if (baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            try {
                is.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                socket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                serverSocket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

Tomcat

服务器

  • 自定义
  • Tomcat服务器 :java后台开发!

客户端

  • 自定义
  • 浏览器

UDP

发短信:不用链接但要地址

发送端

package lesson03;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

// 不需要连接服务器
public class UdpClientDome01 {
    public static void main(String[] args) throws Exception {
        // 1.建立一个socket
        DatagramSocket socket = new DatagramSocket();
        // 2. 建个包
        String msg = "你好啊,服务器";

        // 发送给谁
        InetAddress localhost = InetAddress.getByName("localhost");
        int port = 9090;
        // 数据,数据长度的起始,发送给谁
        DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
        // 3.发送包
        socket.send(packet);
        // 4.关闭流
        socket.close();
    }
}

接收端

package lesson03;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpServerDemo01 {
    public static void main(String[] args) throws Exception {
        // 开放端口
        DatagramSocket socket = new DatagramSocket(9090);
        // 接收数据包
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);

        socket.receive(packet);
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        socket.close();
    }
}