JAVA网络编程_基本代码实现_SXL

JAVA网络编程_SXL

  • 引言
  • 单个服务器端和单个客户端消息互发
  • 服务器接收多个客户端请求
  • 总结


引言

做个俗人,贪财好色,一身正气。

本人注意: 整理网络编程基础代码,后续继续展开。


单个服务器端和单个客户端消息互发

Server端

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
     

    public static void main(String[] args) throws IOException {
     
		// close等流关闭代码暂时忽略
        // 搭建服务器
        ServerSocket server = new ServerSocket(50000);
        System.out.println("=====服务器启动完毕=====");
        // 等待客户端的连接
        Socket socket = server.accept();

        // 此线程用于接收客户端信息
        new Thread(() -> {
     

            try {
     
                // 客户端传来的消息
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                while (true) {
     
                    String text = br.readLine();
                    System.out.println("客户端:" + text);
                }
            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }).start();

        // 此线程用于发送客户端信息
        new Thread(() -> {
     
            try {
     
                // 发送给客户端消息
                while (true) {
     
                    Scanner sc = new Scanner(System.in);
                    // 输入回复的内容:
                    PrintStream ps = new PrintStream(socket.getOutputStream());
                    ps.println(sc.nextLine());
                }
            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }).start();

    }

}

Client端

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
     

    public static void main(String[] args) throws IOException {
     
		// close等流关闭代码暂时忽略
        // 客户端
        Socket socket = new Socket("127.0.0.1", 50000);

        OutputStream mos = socket.getOutputStream();
        PrintStream mps = new PrintStream(mos);
        mps.println("客户端已连接");
        System.out.println("=====客户端连接成功=====");

        // 此线程用于接收客户端信息
        new Thread(() -> {
     
            try {
     
                // 服务器传来的消息
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                while (true) {
     
                    String text = br.readLine();
                    System.out.println("服务器:" + text);
                }
            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }).start();

        // 此线程用于发送服务器信息
        new Thread(() -> {
     
            try {
     
                // 发送给服务器消息
                while (true) {
     
                    Scanner sc = new Scanner(System.in);
                    // 输入回复的内容:
                    PrintStream ps = new PrintStream(socket.getOutputStream());
                    ps.println(sc.nextLine());
                }
            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }).start();
    }
}

服务器接收多个客户端请求

Server端

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
     

    public static void main(String[] args) throws IOException {
     
		// close等流关闭代码暂时忽略
        // 搭建服务器
        ServerSocket server = new ServerSocket(50000);
        System.out.println("=====服务器启动完毕=====");

        while (true) {
     
            // 等待多个客户端的连接
            Socket socket = server.accept();
            // 此线程用于接收客户端信息
            new Thread(() -> {
     

                try {
     
                    // 客户端传来的消息(接收一次)
                    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String text = br.readLine();
                    System.out.println("客户端:" + text);
                    PrintStream ps = new PrintStream(socket.getOutputStream());
                    ps.println(Thread.currentThread().getName());
                    Thread.sleep(10000);
                } catch (IOException | InterruptedException e) {
     
                    // 此处逻辑待完善
                    e.printStackTrace();
                }
            }).start();

        }

    }

}

Client端

import java.io.*;
import java.net.Socket;

public class Client {
     

    public static void main(String[] args) throws IOException {
     
		// close等流关闭代码暂时忽略
        // 客户端
        Socket socket = new Socket("127.0.0.1", 50000);

        OutputStream mos = socket.getOutputStream();
        PrintStream mps = new PrintStream(mos);
        mps.println("客户端已连接");
        System.out.println("=====客户端连接成功=====");

        // 服务器传来的消息
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String text = br.readLine();
        System.out.println("服务器:" + text);

    }

}

总结

人生无限,缓缓起航,修正改错,在满足完成任务的条件下,追求完善,全身而退。

你可能感兴趣的:(java,java,socket,网络)