java网络编程,编写客户端和服务端的socket程序,客户端向服务端发送字符串” hello”,服务端将收到字符串的长度打印到控制台 要求: Server和Client通信使用8888端口 Serv

编写客户端和服务端的socket程序,客户端向服务端发送字符串” hello”,服务端将收到字符串的长度打印到控制台 要求:

  • Server和Client通信使用8888端口

  • Server收到消息打出的信息前需要加上“server收到:”,同理Client收到消息打出的信息前需要加上“client收到:”

  • Client先发送”how are you” Server 收到后,打印client的消息,并回复”Fine, thank you. And you?”

  • Client 收到后,打印Server的消息,并回复”I’m fine, too.”

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Task1 {
    
        private static class Server extends Thread {
            @Override
            public void run() {
                ServerSocket ss = null;
                try {
                    ss = new ServerSocket(8888);
                    Socket s = ss.accept();
                    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    System.out.println("server收到:" + br.readLine());
                    PrintStream ps = new PrintStream(s.getOutputStream());
                    ps.println("Fine, thank you. And you?");
                    System.out.println("server收到:" + br.readLine());
                    ss.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        private static class Client extends Thread {
            @Override
            public void run() {
                try {
                    Socket s = new Socket("localhost", 8888);
                    PrintStream ps = new PrintStream(s.getOutputStream());
                    ps.println("How are you?");
                    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    System.out.println("client收到:" + br.readLine());
                    ps.println("I'm fine, too.");
                    s.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) {
            new Server().start();
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
            new Client().start();
        }
    }
    

    2.将上面的通信改为Client向Server持续发送消息,Server收到消息后打出,并将消息反序后返回给Client 直到Client发送的消息是”goodbye”时,程序运行结束退出

  • import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class Task2 {
    
        private static class Server extends Thread {
            @Override
            public void run() {
                ServerSocket ss = null;
                try {
                    ss = new ServerSocket(8888);
                    Socket s = ss.accept();
                    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    PrintStream ps = new PrintStream(s.getOutputStream());
                    while (true) {
                        String line = br.readLine();
                        System.out.println("server收到:" + line);
                        if ("goodbye".equals(line)) {
                            break;
                        }
                        ps.println(new StringBuffer(line).reverse().toString());
                    }
                    ss.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        private static class Client extends Thread {
            @Override
            public void run() {
                try {
                    Socket s = new Socket("localhost", 8888);
                    PrintStream ps = new PrintStream(s.getOutputStream());
                    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    Scanner scanner = new Scanner(System.in);
                    System.out.println(">>");
                    while (true) {
                        String str = scanner.nextLine().trim();
                        ps.println(str);
                        if ("goodbye".equals(str)) {
                            break;
                        }
                        System.out.println("client收到:" + br.readLine() + "\n>>");
                    }
                    scanner.close();
                    s.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) {
            new Server().start();
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
            new Client().start();
        }
    }
    

你可能感兴趣的:(java)