Java从遗忘到入门——Day08

今天学习了Java网络编程TCP

1.TCP基本操作

TCP的基本操作可以用如下代码演示,Client表示客户端,Server表示服务器。

public class Server {
    public static void main(String[] args) throws IOException {
        System.out.println("———————服务端——————");
        //1.指定端口 使用serversocket创建服务器
        ServerSocket server = new ServerSocket(8888);
        //2.阻塞式等待连接 accept
        Socket client = server.accept();
        System.out.println("(一个客户端创建了连接)");
        //3.输入输出流操作
        DataInputStream dis = new DataInputStream(client.getInputStream());
        String str = dis.readUTF();
        System.out.println("服务端接收到的内容是:" + str);
        //4.释放连接
        if (dis != null) {
            dis.close();
        }
        if (client != null) {
            client.close();
        }
        if (server != null) {
            server.close();//一般服务器不关闭
        }
    }
}

public class Client {
    public static void main(String[] args) throws IOException {
        System.out.println("———————客户端——————");
        //1.建立连接:使用socket建立客户端+服务的地址和端口
        Socket client = new Socket("localhost", 8888);
        //2.输入输出流操作
        DataOutputStream dos = new DataOutputStream(client.getOutputStream());
        String str = "hello";
        dos.writeUTF(str);
        dos.flush();
        System.out.println("发送端已发送");
        //3.释放连接
        if (dos != null) {
            dos.close();
        }
        if (client != null) {
            client.close();
        }
    }
}

先运行服务器再运行客服端的结果为:
在这里插入图片描述
在这里插入图片描述

2.客户机服务器的双向操作

登陆操作流程:

  1. 客户端向服务器发送用户名和密码;
  2. 在服务器端验证并且将结果返回给客户端。
public class Server {
    public static void main(String[] args) throws IOException {
        System.out.println("———————服务端——————");
        //1.指定端口 使用serversocket创建服务器
        ServerSocket server = new ServerSocket(8888);
        //2.阻塞式等待连接 accept
        Socket client = server.accept();
        //3.输入输出流操作
        DataInputStream dis = new DataInputStream(client.getInputStream());
        String str = dis.readUTF();
        String[] strs = str.split("&");
        System.out.println("用户名:" + strs[0]);
        System.out.println("密码:" + strs[1]);
        String result = "";
        if (strs[0].equals("zd") && strs[1].equals("123456")) {
            result = "登陆成功!";
        } else {
            result = "用户名或密码错误!";
        }

        DataOutputStream dos = new DataOutputStream(client.getOutputStream());
        dos.writeUTF(result);
        dos.flush();
        System.out.println(result);

        //4.释放连接
        dos.close();
        dis.close();
        client.close();
        server.close();//一般服务器不关闭
    }
}

public class Client {
    public static void main(String[] args) throws IOException {
        System.out.println("———————客户端——————");
        //1.建立连接:使用socket建立客户端+服务的地址和端口
        Socket client = new Socket("localhost", 8888);
        //2.输入输出流操作
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入用户名:");
        String name = br.readLine();
        System.out.println("请输入密码:");
        String password = br.readLine();
        String str = name + "&" + password;
        DataOutputStream dos = new DataOutputStream(client.getOutputStream());
        dos.writeUTF(str);
        dos.flush();

        DataInputStream dis = new DataInputStream(client.getInputStream());
        String result = dis.readUTF();
        System.out.println(result);

        //3.释放连接
        dis.close();
        br.close();
        dos.close();
        client.close();
    }
}

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

3.文件上传

把客户端的图片上传到服务器端。

public class Client {
    public static void main(String[] args) throws IOException {
        System.out.println("———————客户端——————");
        //1.建立连接:使用socket建立客户端+服务的地址和端口
        Socket client = new Socket("localhost", 8888);
        //2.输入输出流操作
        InputStream is = new BufferedInputStream(new FileInputStream("src//timg.jpg"));
        OutputStream os = new BufferedOutputStream(client.getOutputStream());
        byte[] data = new byte[1024];
        int len = -1;
        while ((len = is.read(data)) != -1) {
            os.write(data, 0, len);
            os.flush();
        }
        os.flush();
        //3.释放连接
        is.close();
        os.close();
        client.close();
    }
}

public class Server {
    public static void main(String[] args) throws IOException {
        System.out.println("———————服务端——————");
        //1.指定端口 使用serversocket创建服务器
        ServerSocket server = new ServerSocket(8888);
        //2.阻塞式等待连接 accept
        Socket client = server.accept();
        //3.输入输出流操作
        InputStream is = new BufferedInputStream(client.getInputStream());
        OutputStream os = new BufferedOutputStream(new FileOutputStream("src//picture.jpg"));
        byte[] data = new byte[1024];
        int len = -1;
        while ((len = is.read(data)) != -1) {
            os.write(data, 0, len);
            os.flush();
        }
        os.flush();

        //4.释放连接
        os.close();
        is.close();
        client.close();
        server.close();//一般服务器不关闭
    }
}
发布了41 篇原创文章 · 获赞 9 · 访问量 9839

猜你喜欢

转载自blog.csdn.net/Serena0814/article/details/105126750