Java基础进阶 网络编程初级 TCP通信传输文件及信息实例 D190407

Java基础进阶 网络编程初级 TCP通信传输文件及信息实例

01.第一章:网络编程入门_网络编程三要素

1).IP地址:计算机在互联网上的唯一地址--相当于收信地址;
	1).127.0.0.1:不在公网上使用,用作本机回环地址;
	2).192.168.xxx.xxx:不在公网上使用,用作局域网;
	3).10.xxx.xxx.xxx:不在公网上使用,用作局域网;
	4).常用的DOS命令:
		1).ipconfig:查看本机IP
		2).ping 对方ip/域名 -t:查看本机与目标电脑是否连通。
2).端口号:指虚拟端口--相当于:收信人;
	![在这里插入图片描述](https://img-blog.csdnimg.cn/20190408224802153.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hjOTY1NzQ2NTUw,size_16,color_FFFFFF,t_70)


3).协议:指互联网信息交互的不同的数据格式--写信的语言/格式;
	http,UDP协议,TCP协议,https,ftp....
	1).UDP协议的特点:
		1).数据要打包发送;
		2).数据包大小有限制:64K;
		3).发送时,可以没有接收端--不安全的协议;
			例如:公屏软件、网络视频会议系统....
	2).TCP协议的特点:【今天需要掌握】
		1).数据不需要打包发送;
		2).数据大小无限制;
		3).发送时必须要有接收端,否则抛异常;
			例如:文件上传、下载,点对点聊天....

02.第二章:TCP通信程序_TCP发送端实现

public class Client {
    public static void main(String[] args) throws IOException {
        //1.创建一个Socket对象--连接服务器
        Socket socket = new Socket("192.168.156.82",9999);//目标的IP和端口
        //2.获取一个输出流
        OutputStream out = socket.getOutputStream();
        //3.输出信息
        out.write("你好服务器,呵呵!!".getBytes());

        //4.关闭流
        socket.close();
    }
}

03.第二章:TCP通信程序_TCP服务器端接收实现

public class Demo {
    public static void main(String[] args) throws IOException {
        //1.创建一个ServerSocket
        ServerSocket server = new ServerSocket(9999);
        //2.等待连接
        System.out.println("等待连接....");
        Socket socket = server.accept();
        //3.获取输入流,接收信息
        InputStream in = socket.getInputStream();
        byte[] byteArray = new byte[1024];
        int len = in.read(byteArray);
        System.out.println("【服务器】接收到的信息:" + new String(byteArray, 0, len));

        //4.关闭流
        socket.close();
        server.close();
    }
}

04.第二章:TCP通信程序_TCP发送与服务器的原理图
在这里插入图片描述

05.第二章:TCP通信程序_练习_TCP服务器端发送反馈
1).客户端:

public class Client {
    public static void main(String[] args) throws IOException {
        //1.创建一个Socket对象--连接服务器
        Socket socket = new Socket("192.168.156.82",9999);//目标的IP和端口
        //2.获取一个输出流
        OutputStream out = socket.getOutputStream();
        //3.输出信息
        out.write("你好服务器,呵呵!!".getBytes());

        //接收反馈
        InputStream in = socket.getInputStream();
        byte[] byteArray = new byte[1024];
        int len = in.read(byteArray);
        System.out.println("【客户端收到反馈】" + new String(byteArray, 0, len));
        //4.关闭流
        socket.close();
    }
}

2).服务器端:

public class Server {
    public static void main(String[] args) throws IOException {
        //1.创建一个ServerSocket
        ServerSocket server = new ServerSocket(9999);
        //2.等待连接
        System.out.println("等待连接....");
        Socket socket = server.accept();
        //3.获取输入流,接收信息
        InputStream in = socket.getInputStream();
        byte[] byteArray = new byte[1024];
        int len = in.read(byteArray);
        System.out.println("【服务器】接收到的信息:" +
                new String(byteArray, 0, len));

        //发送反馈
        OutputStream out = socket.getOutputStream();
        out.write("你傻笑啥呀!!".getBytes());
        //4.关闭流
        socket.close();
        server.close();
    }
}

多线程点对点聊天图示:
在这里插入图片描述


06.第三章:综合案例_单用户文件上传并发送反馈
1).客户端:

public class Client {
    public static void main(String[] args) throws IOException {
        //1.连接服务器
        Socket socket = new Socket("127.0.0.1",9999);
        //2.构造文件输入流
        FileInputStream fileIn = new FileInputStream("d:\\视频.itcast");
        //3.构造一个网络输出流
        OutputStream netOut = socket.getOutputStream();
        //4.循环从文件读取字节数组,并发送字节数组
        byte[] byteArray = new byte[1024];
        int len = 0;
        while ((len = fileIn.read(byteArray)) != -1) {
            netOut.write(byteArray,0,len);
        }
        //发送一个结束标记,促使服务器端的循环结束
        socket.shutdownOutput();

        System.out.println("上传完毕,接收反馈....");
        //接收反馈
        InputStream netIn = socket.getInputStream();
        len = netIn.read(byteArray);
        System.out.println("【客户端收到反馈】" +
                            new String(byteArray, 0, len));
        //5.关闭流
        fileIn.close();
        socket.close();
        System.out.println("【客户端】上传完毕!");
    }
}

2).服务器端:

public class Server {
    public static void main(String[] args) throws IOException {
        //1.构造一个ServerSocket
        ServerSocket server = new ServerSocket(9999);
        //2.等待连接
        Socket socket = server.accept();
        //3.构造文件的输出流
        FileOutputStream out = new FileOutputStream("e:\\视频.itcast");
        //4.获取一个网络输入流
        InputStream netIn = socket.getInputStream();
        //5.循环从网络读取
        byte[] byteArray = new byte[1024];
        int len = 0;
        System.out.println("服务器开始处理上传....");
        while ((len = netIn.read(byteArray)) != -1) {
            out.write(byteArray, 0, len);
        }
        System.out.println("服务器上传完毕,发送反馈....");
        //发送反馈
        OutputStream netOut = socket.getOutputStream();
        netOut.write("收到!!".getBytes());

        //6.关闭流
        out.close();
        socket.close();
        server.close();
        System.out.println("【服务器端】完毕!");
    }
}

07.第三章:综合案例_多用户文件上传并发送反馈

原理图示:
在这里插入图片描述
1).客户端:

public class Client {
    public static void main(String[] args) throws IOException {
        for (int i = 0; i < 10; i++) {
            new Thread(){
                @Override
                public void run() {
                    //1.连接服务器
                    Socket socket = null;
                    try {
                        socket = new Socket("127.0.0.1",9999);
                        //2.构造文件输入流
                        FileInputStream fileIn = new FileInputStream("d:\\视频.itcast");
                        //3.构造一个网络输出流
                        OutputStream netOut = socket.getOutputStream();
                        //4.循环从文件读取字节数组,并发送字节数组
                        byte[] byteArray = new byte[1024];
                        int len = 0;
                        while ((len = fileIn.read(byteArray)) != -1) {
                            netOut.write(byteArray,0,len);
                        }
                        //发送一个结束标记,促使服务器端的循环结束
                        socket.shutdownOutput();

                        System.out.println("上传完毕,接收反馈....");
                        //接收反馈
                        InputStream netIn = socket.getInputStream();
                        len = netIn.read(byteArray);
                        System.out.println("【客户端收到反馈】" +
                                new String(byteArray, 0, len));
                        //5.关闭流
                        fileIn.close();
                        socket.close();
                        System.out.println("【客户端】上传完毕!");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }
}

2).服务器端

   public class Server {
    public static void main(String[] args) throws IOException {
        //1.构造一个ServerSocket
        ServerSocket server = new ServerSocket(9999);
        int index = 0;
        while (true) {
            //2.等待连接
            Socket socket = server.accept();
            //3.启动线程
            new ServerThread(socket,++index).start();
        }
    }
}

3).服务器线程类

    public class ServerThread extends Thread {
    private Socket socket;
    private int index;

    public ServerThread(Socket socket,int index) {
        this.socket = socket;
        this.index = index;
    }

    @Override
    public void run() {
        //4.获取一个网络输入流
        InputStream netIn = null;
        try {
            netIn = socket.getInputStream();
            //5.循环从网络读取
            byte[] byteArray = new byte[1024];
            int len = 0;
            FileOutputStream out = new FileOutputStream("e:\\视频" + index + ".itcast");
            while ((len = netIn.read(byteArray)) != -1) {
                out.write(byteArray, 0, len);
            }
            //发送反馈
            OutputStream netOut = socket.getOutputStream();
            netOut.write("收到!!".getBytes());
            //6.关闭流
            out.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

08.第三章:综合案例3_模拟WEB服务器_案例说明
图示原理:
在这里插入图片描述

09.第三章:综合案例3_模拟WEB服务器_案例实现
1).我们只需要实现服务器,客户端由浏览器充当。
代码如下:

public class Server {
    public static void main(String[] args) throws IOException {
        //1.创建一个ServerSocket对象
        ServerSocket server = new ServerSocket(9999);
        //2.等待连接
        while (true) {

            System.out.println("等待连接...");
            Socket socket = server.accept();
            //3.由于浏览器是"按行"发送信息,所以服务器要按行接收
            new Thread(){
                @Override
                public void run() {
                    BufferedReader bufIn = null;
                    try {
                        bufIn = new BufferedReader(
                                new InputStreamReader(socket.getInputStream()));
                        //4.只读取第一行
                        String row = bufIn.readLine();//"GET /img/mn1.jpg HTTP/1.1"
                        //5.取出路径
                        String path = row.substring(row.indexOf(" ") + 1,row.lastIndexOf(" "));//"/img/mn1.jpg"
                        //6.判断
                        if(!path.startsWith("/img/")){
                            socket.close();
                            return;
                        }
                        //7.提取文件名
                        String fileName = path.substring(path.lastIndexOf("/") + 1);//mn1.jpg
                        //8.产生响应
                        //1.响应头--固定写法
                        OutputStream out = socket.getOutputStream();
                        out.write("HTTP/1.1 200 OK\r\n".getBytes());
                        out.write("content-type:image/jpeg\r\n".getBytes());//表示返回的是一个图片,让浏览器按图片显示
                        out.write("\r\n".getBytes());
                        //2.向客户端输出图片
                        FileInputStream fileIn = new FileInputStream("img/" + fileName);
                        byte[] byteArray = new byte[1024];
                        int len = 0;
                        while ((len = fileIn.read(byteArray)) != -1) {
                            out.write(byteArray,0,len);
                        }
                        //关闭流
                        fileIn.close();
                        socket.close();
                    } catch (IOException e) {
                    }
                }
            }.start();
        }
    }
}

========================================================
学习目标总结:

01.能够辨别UDP和TCP协议特点

1).UDP的特点:
		1).需要打包发送
		2).数据包大小有限制,64K;
		3).发送时,可以没有接收端--不安全的协议;
			例如:公屏软件
	2).TCP的特点:
		1).不需要打包发送;
		2).数据大小无限制;
		3).发送时,必须要有接收端--安全的协议;
			例如:文件上传、下载,点对点聊天

02.能够说出TCP协议下两个常用类名称

1).客户端:Socket(“ip”,int 端口)
2).服务器端:ServerSocket(int 端口)

03.能够编写TCP协议下字符串数据传输程序
在这里插入图片描述
04.能够理解TCP协议下文件上传案例

05.能够理解TCP协议下模拟B\S服务器案例

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章