【Java】网络编程

网络通信要素

  1. 如何实现网络中的主机互相通信
  • 通信双方地址
    IP:网络当中唯一定位的一台主机
    端口号:定位一个主机上不同的应用程序
  • 一定的规则(即:网络通信协议。有两套参考模型)
    OSI参考模型
    TCP/IP参考模型(用的是这个)

通信要素1:IP和端口号

IP

  1. IP:唯一的标识Internet上的计算机
  2. 在Java中使用InetAddress类代表IP
  3. IP分类的两种方式
  • 分类方式1:IPV4和IPV6
  • 分类方式2:万维网(公网地址)和局域网(私有地址)
  1. 域名
    可以通过域名来访问, 会把域名再解析成ip地址
  2. 本地回路地址
    127.0.0.1 表示的是本机,对应localhost
InetAddress类
实例化
  1. getByName(String host)
    host可以是具体的ip地址,也可以是域名
try {
    //用ip地址
    InetAddress inet1 = InetAddress.getByName("192.168.10.13");
    System.out.println(inet1);// /192.168.10.13
    //用域名
    InetAddress inet2 = InetAddress.getByName("www.sina.com");
    System.out.println(inet2);// www.sina.com/112.48.153.229
    //本地
    InetAddress inet3 = InetAddress.getByName("127.0.0.1");
    System.out.println(inet3);// /127.0.0.1

} catch (UnknownHostException e) {
    e.printStackTrace();
}
  1. getLocalHost()
    获取本机的ip地址
InetAddress inet4 = InetAddress.getLocalHost();
System.out.println(inet4);//DESKTOP-02DMF3C/10.30.164.174 因为是局域网所以显示的不是127.0.0.1
常用方法:getHostName()

获取域名

System.out.println(inet2.getHostName());//www.sina.com
常用方法:getHostAddress()

获取ip地址

System.out.println(inet2.getHostAddress());//112.48.153.229

端口号

  1. 端口号标识正在计算机上运行的进程(程序)
  2. 要求不同的进程有不同的端口号
  3. 范围:被规定为一个16位的整数0~65535
  4. 端口号和IP地址的组合得出一个网络套接字Socket

通信要素2:网络协议

在这里插入图片描述
在这里插入图片描述

TCP网络编程

TCP 效率低 但安全 生活中的案例:打电话
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

例子1:客户端发送信息给服务端,服务端将数据显示在控制台上

要先运行服务器端,再运行客户端

客户端步骤:

  1. 创建Socket对象,指明服务器端的ip和端口号
  2. 获取输出流,用于输出数据
  3. 写数据
  4. 资源关闭

服务器端步骤:

  1. 创建服务器端的ServerSocket,指明自己的端口号
  2. 调用accept()表明接收来自于客户端的socket
  3. 获取输入流
  4. 读取输入流的数据
  5. 资源的关闭
public class TCPTest1 {

    //客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        try {
            //1. 创建Socket对象,指明服务器端的ip和端口号
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet,8899);
            //2. 获取输出流,用于输出数据
            os = socket.getOutputStream();
            //3. 写数据
            os.write("您好,我是客户端".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4. 资源关闭
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    @Test
    public void server() {
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            // 1. 创建服务器端的ServerSocket,指明自己的端口号
            ss = new ServerSocket(8899);
            //2. 调用accept()表明接收来自于客户端的socket
            socket = ss.accept();
            //3. 获取输入流
            is = socket.getInputStream();
            //不建议这样写,可能会有乱码 因为可能会把一个汉字有三个字节 劈成两半
//        byte[] buffer = new byte[20];
//        int len;
//        while((len=is.read(buffer))!=-1){
//            String str = new String(buffer,0,len);
//            System.out.println(str);
//        }
            //4. 读取输入流的数据
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[5];
            int len;
            while((len=is.read(buffer))!=-1){
                baos.write(buffer,0,len); //ByteArrayOutputStream类里面有数组,不够会自动扩 所以就不用怕把汉字批两半
            }
            System.out.println(baos.toString());
            System.out.println("收到了来自于:"+socket.getInetAddress().getHostAddress()+"的数据");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5. 资源的关闭
            if(baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ss!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }





    }
}
例子2:客户端发送文件给服务端,服务端将文件保存在本地

和例子1的区别就是多了一个文件读写的步骤

public class FileReaderWriterTest {

    //客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            //1. 创建Socket对象,指明服务器端的ip和端口号
            socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
            //2. 获取输出流,用于输出数据
            os = socket.getOutputStream();
            //3. 写数据
            fis = new FileInputStream(new File("ddd.png"));
            byte[] buffer = new byte[1024];
            int len;
            while((len=fis.read(buffer))!=-1){
                os.write(buffer,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4. 资源关闭
            if(fis!=null){
                try {
                    fis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    @Test
    public void server() {
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        FileOutputStream fos = null;
        try {
            // 1. 创建服务器端的ServerSocket,指明自己的端口号
            ss = new ServerSocket(9090);
            //2. 调用accept()表明接收来自于客户端的socket
            socket = ss.accept();
            //3. 获取输入流
            is = socket.getInputStream();

            //4. 读取输入流的数据
            fos = new FileOutputStream(new File("dddserver.png"));
            byte[] buffer = new byte[1024];
            int len;
            while((len=is.read(buffer))!=-1){//把从客户端读到的 存在文件中
                fos.write(buffer,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5. 资源的关闭
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ss!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }





    }
}

例子3:返回“发送成功”给客户端
public class FileReaderWriterTest {

    //客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;

        try {
            //1. 创建Socket对象,指明服务器端的ip和端口号
            socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
            //2. 获取输出流,用于输出数据
            os = socket.getOutputStream();
            //3. 写数据
            fis = new FileInputStream(new File("ddd.png"));
            byte[] buffer = new byte[1024];
            int len;
            while((len=fis.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
            //关闭数据的输出
            socket.shutdownOutput();

            //4. 接收来自于服务器端的数据,并显示到控制台上
            is = socket.getInputStream();
            baos = new ByteArrayOutputStream();
            byte[] buffer1 = new byte[20];
            int len1;
            while((len1=is.read(buffer1))!=-1){
                baos.write(buffer1,0,len1);
            }
            System.out.println(baos.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 5. 资源关闭
            if(fis!=null){
                try {
                    fis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    @Test
    public void server() {
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        FileOutputStream fos = null;
        OutputStream os = null;
        try {
            // 1. 创建服务器端的ServerSocket,指明自己的端口号
            ss = new ServerSocket(9090);
            //2. 调用accept()表明接收来自于客户端的socket
            socket = ss.accept();
            //3. 获取输入流
            is = socket.getInputStream();

            //4. 读取输入流的数据
            fos = new FileOutputStream(new File("dddserver1.png"));
            byte[] buffer = new byte[1024];
            int len;
            while((len=is.read(buffer))!=-1){//把从客户端读到的 存在文件中
                fos.write(buffer,0,len);
            }

            //5. 服务器端给客户端反馈
            os = socket.getOutputStream();
            os.write("文件已收到".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //6. 资源的关闭
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ss!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }





    }
}

UDP网络编程

UDP生活中案例:发短信 发电报
在这里插入图片描述

public class UDPTest1 {

    //发送端
    @Test
    public void send() throws IOException {
        DatagramSocket socket = new DatagramSocket();
        String str = "我是UDP方式发送的字符串";
        byte[] data = str.getBytes();
        InetAddress inet = InetAddress.getByName("127.0.0.1");
        DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
        socket.send(packet);

        socket.close();
    }
    //接收端
    @Test
    public void receive() throws IOException {
        DatagramSocket socket = new DatagramSocket(9090);
        byte[] buffer = new byte[100];
        DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
        socket.receive(packet);
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        socket.close();
    }
}

URL编程

  1. URL(Uniform Resource Locator) : 统一资源定位符,对应着互联网的某一资源地址
  2. 格式:
    在这里插入图片描述

URL 常用方法

在这里插入图片描述

示例 从服务器下载图片到本地

要用try-catch-finally抛异常,这里为了看起来方便就直接throws了。但这样是不对的
在这里插入图片描述
这里是用了try-catch-finally
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值