0
点赞
收藏
分享

微信扫一扫

Java的网络编程---初步了解

云竹文斋 2022-02-07 阅读 34

概念

  • 计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
  • 网络编程的目的是进行数据交换和通信
  • 定位到网络上的一台主机的 ip:端口可以定位到这台计算机上的某个资源

网络通信的要素

  • 实现网络通信,需要知道通信双方的地址:
  1. ip
  2. 端口号
  • 需要规则:网络通信协议:
  1. TCP/IP参考模型
    在这里插入图片描述

IP

  • ip地址用于唯一定位网络上计算机
  • 127.0.0.1:本机localhost
  • ip地址的分类:
  1. ipv4/ipv6:
    IPV4 127.0.0.1,4个字节组成,0255,42亿
    IPV6 :128位。8个无符号整数
2001:0bb2:aaaa:0015:0000:0000:1aaa:1323
  1. 公网(互联网)-私网(局域网)
    192.168.xx.xx
  • 域名:记忆IP问题
package com.akita;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

//测试IP
public class network {
    public static void main(String[] args) {
        try {

            //查询本季地址
            InetAddress byName = InetAddress.getByName("127.0.0.1");
            System.out.println(byName);
            InetAddress localhost = InetAddress.getByName("localhost");
            System.out.println(localhost);
//            InetAddress localHost = InetAddress.getLocalHost();
//            System.out.println(localHost);

            System.out.println("==========================");

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

            //常用方法
            System.out.println("常用方法");
            System.out.println(baidu.getCanonicalHostName());     //规范的名字
            System.out.println(baidu.getHostAddress());           //ip
            System.out.println(baidu.getHostName());              //域名
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

运行结果
在这里插入图片描述

端口

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

  • 不同的进程有不同的端口号,用于来区分软件
  • 被规定0~65535
  • TCP和UDP端口可以相同:65535✖️2。 TCP:80与UDP:80是可以的,单个协议下,端口号不能冲突
  • 端口分类
    公有端口:0~1023
    HTTP:80
    HTTPS:443
    FTP:21
    Telent:23
    程序注册端口:1024~49151,分配给用户或者程序
    Tomcat:8080
    MySQL:3306
    Oracle:1521
    动态、私有端口:49152~65535
netstat -ano                     //查看所有的端口
package com.akita;

import java.net.InetSocketAddress;

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

        System.out.println(inetSocketAddress.getAddress());
        System.out.println(inetSocketAddress.getHostName());
        System.out.println(inetSocketAddress.getPort());

    }
}

运行效果
在这里插入图片描述

通信协议

  • 协议:约定。
  • 网络通信协议:速率,传输码率,代码结构,传输控制。。。
  • 分层:TCP/IP协议簇
    重要:TCP:用户传输协议;UDP:用户数据报协议
  • TCP与UDP对比

TCP:打电话式

  1. 连接,稳定
  2. 三次握手,四次挥手
  3. 客户端服务端
  4. 传输完成,释放连接,效率低

UDP:发短信式

  1. 不连接,不稳定
  2. 客户端与服务端没有明确的界限
  3. 不管有没有准备好,都可以发送

TCP

客户端

  1. 连接服务器Socket
  2. 发送消息
package com.akita.lesson1;

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 outputStream = null;
        try {
            //需要知道服务端的地址,端口号
            InetAddress serverIP = InetAddress.getByName("127.0.0.1");
            int port = 5417;
            //创建一个socket连接
            socket = new Socket(serverIP, port);
            //发送消息
            outputStream = socket.getOutputStream();
            outputStream.write("你好".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}

服务器

  1. 建立服务端口 ServerSocket()
  2. 等待用户的连接,通过 accept()
  3. 接受用户的消息
package com.akita.lesson1;

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 accept = null;
        InputStream inputStream = null;
        ByteArrayOutputStream baos = null;
        try {
            //需要有一个地址
            serverSocket = new ServerSocket(5417);
            while (true) {

                //等待客户端连接
                accept = serverSocket.accept();
                //读取客户端的消息
                inputStream = accept.getInputStream();
                //管道流
                baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while ((len = inputStream.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }
                System.out.println(baos.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept != null) {
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

文件上传

举报

相关推荐

0 条评论