网络编程

对象流的使用

  1. ObjectInputStream 和 ObjectOutputStream
  2. 作用:
    ObjectOutputStream: 内存中的对象–>存储中的文件、通过网络传输出去:序列化过程
    ObjectInputStream: 存储中的文件、通过网络接收过来 -->内存中的对象:反序列化过程
  3. 要想一个对象可序列化,必须要实现Serializable
  4. 序列化机制:
    对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久保存在硬盘上,或通过网络将这种二进制流传输到另一个网络节点。当其他程序获取了这种二进制流,就可以恢复成原来的Java对象

序列化

/*
    * 序列化过程:将内存中的java对象保存到磁盘中或通过网络传输出去
    * 使用ObjectOutputStream实现
    * */
    @Test
    public void testObjectOutputStream(){
        ObjectOutputStream oos= null;
        try {
            //1.
            oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
            //2.
            oos.writeObject(new String("china"));

            oos.flush();

            oos.writeObject(new Person("lee",22));

            oos.flush();

            oos.writeObject(new Person("v",21,new Account(5000)));

            oos.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //3.
            if (oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

反序列化

/*
    * 反序列化:将磁盘文件中的对象还原为内存层面的一个java对象
    * */
    @Test
    public void testObjectInputStream(){
        ObjectInputStream ois= null;
        try {
            ois = new ObjectInputStream(new FileInputStream("object.dat"));

            Object obj = ois.readObject();
            String str=(String)obj;

            Person p=(Person)ois.readObject();
            Person p1=(Person)ois.readObject();

            System.out.println(str);
            System.out.println(p);
            System.out.println(p1);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  1. 实现序列化对象所属的类需要满足:
    1. 需要实现接口:Serializable
    2. 当前类提供一个全局常量:serialVersionUID
    3. 除了当前类需要实现Serializable接口外,还必须保证其内部的所有属性可序列化(默认情况下,基本数据类型可序列化)

补充:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员

RandomAccessFile

  1. RandomAccessFile直接继承于java.lang.Object类,实现了DataInput和DataOutput接口
  2. RandomAccessFile既可以作为一个输入流,又可以作为一个输出流
  3. 如果RandomAccessFile作为输出流时,
    写出的文件不存在,直接创建
    写出的文件存在,从头覆盖
  4. 可以通过相关的操作,实现RandomAccessFile"输入"数据的情况。 seek(int pos)
    @Test
    public void test1()  {

        RandomAccessFile raf1= null;
        RandomAccessFile raf2= null;
        try {
            raf1 = new RandomAccessFile(new File("2101608177836_.pic_hd.jpg"),"r");
            raf2 = new RandomAccessFile(new File("2.jpg"),"rw");

            byte[] buffer=new byte[1024];
            int len;
            while ((len=raf1.read(buffer))!=-1){
                raf2.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (raf1!=null){
                try {
                    raf1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (raf2!=null){
                try {
                    raf2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /*
    * 使用RandomAccessFile实现数据的插入效果
    * */
    @Test
    public void test3() throws IOException {
        RandomAccessFile raf1 = new RandomAccessFile(new File("hello.txt"),"r");

        raf1.seek(3); // 将指针调到角标为3的位置
        // 保存指针3后面的所有数据到StringBuilder中
        StringBuilder builder=new StringBuilder((int) new File("hello.txt").length());
        byte[] buffer=new byte[20];
        int len;
        while ((len=raf1.read(buffer))!=-1){
            builder.append(new String(buffer,0,len));
        }
        // 调回指针,写入xyz
        raf1.seek(3);
        raf1.write("xyz".getBytes());

        // 将StringBuilder中的数据写入到文件中
        raf1.write(builder.toString().getBytes());

        raf1.close();
    }

网络编程

一、网络编程中有两个主要的问题:

  1. 如何准确定位网络上一台或多台主句:定位主机上特定的应用
  2. 找到主机后如何可靠高效地进行传输

二、网络编程的两个要素:

  1. 对应问题一:IP和端口号
  2. 对应问题二:提供网络通信协议:TCP/IP参考模型(应用层,传输层,网络层,物理+数据链路层)

三、通信要素一:IP和端口号

  1. IP: 唯一的标识 Internet上的计算机(通信实体)
  2. 在Java中使用InetAddress类代表IP
  3. IP分类:IPv4, IPv6; 万维网,局域网
  4. 域名:www.baidu.com
  5. 本地回路地址:127.0.0.1 对应:localhost

InetAddress类:此类的一个对象就代表一个具体的IP地址

  1. 如何实例化InetAddress: 两个方法:getByName(String host), getLocalHost()
    两个常用方法:getHostName()/ getHostAddress()
  2. 端口号:正在计算机上运行的进程
    要求:不同进程有不同的端口号
    范围:被规定为一个16位的整数0~65535
  3. 端口号和IP地址组合得出一个网络套接字:Socket

网络通信协议

  1. 分型模型
    在这里插入图片描述
  2. TCP, UDP的区别
    在这里插入图片描述

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

/**
 * 实现TCP的网络编程
 * 例子1:客户端发送信息给服务端,服务端将数据显示在控制台上
 * @create 2021-01-12 下午2:36
 */
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("hi,客户端".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())!=-1){
//            String str=new String(buffer,0,len);
//            System.out.print(str);
//        }

            //4. 读取输入流中的数据
            baos = new ByteArrayOutputStream();
            byte[] buffer=new byte[5];
            int len;
            while ((len=is.read(buffer))!=-1){
                baos.write(buffer,0,len);
            }
            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:客户端给服务器端发文件,服务器端保存到本地

/**
 *
 * 实现TCP的网络编程
 * 例题2:客户端发送文件给服务端,服务端将文件保存在本地
 *
 * @create 2021-01-12 下午4:41
 */
public class TCPTest2 {
    @Test
    public void client() throws IOException {
        //1
        Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),9090);
        //2
        OutputStream os = socket.getOutputStream();
        //3
        FileInputStream fis=new FileInputStream("pic.png");
        //4
        byte[] buffer=new byte[1024];
        int len;
        while ((len=fis.read(buffer))!=-1){
            os.write(buffer);
        }
        //5
        fis.close();
        os.close();
        socket.close();

    }

    @Test
    public void server() throws IOException {
        //1. 造一个服务端的接口
        ServerSocket ss=new ServerSocket(9090);
        //2. 获取客户端的接口
        Socket socket=ss.accept();
        //3. 获取输入流
        InputStream is = socket.getInputStream();
        //4. 造一个输出流
        FileOutputStream fos=new FileOutputStream(new File("pic1.png"));
        //5. 输出到本地
        byte[] buffer=new byte[1024];
        int len;
        while ((len=is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        //6. 关闭资源
        fos.close();
        is.close();
        socket.close();
    }
}

例子3:从客户端发送文件给服务端,服务端保存到本地并返回"发送成功"给客户端。

public class TCPTest3 {
    @Test
    public void client() throws IOException {
        //1
        Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),9090);
        //2
        OutputStream os = socket.getOutputStream();
        //3
        FileInputStream fis=new FileInputStream("pic.png");
        //4
        byte[] buffer=new byte[1024];
        int len;
        while ((len=fis.read(buffer))!=-1){
            os.write(buffer);
        }
        //关闭数据的输出
        socket.shutdownOutput();

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

        //6.
        fis.close();
        os.close();
        socket.close();
        baos.close();

    }

    @Test
    public void server() throws IOException {
        //1. 造一个服务端的接口
        ServerSocket ss=new ServerSocket(9090);
        //2. 获取客户端的接口
        Socket socket=ss.accept();
        //3. 获取输入流
        InputStream is = socket.getInputStream();
        //4. 造一个输出流
        FileOutputStream fos=new FileOutputStream(new File("pic2.png"));
        //5. 输出到本地
        byte[] buffer=new byte[1024];
        int len;
        while ((len=is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }

        //6. 服务器端给客户端反馈
        OutputStream os = socket.getOutputStream();
        os.write("发送成功".getBytes());

        //7. 关闭资源
        fos.close();
        is.close();
        socket.close();
        os.close();
    }
}

URL

URL网络编程

  1. URL:统一资源定位符,对应着互联网的某一资源地址
  2. 格式:
    http://localhost:8000/examples/pic.jpg?username=tom
    协议 主机名 端口号 资源地址 参数列表
  3. 实例化:
URL url=new URL("http://localhost:8000/examples/pic.jpg?username=tom");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值