TalkServer 的问题

第一个TO DO 是要先print出 在STDOUT中从client 发出的信息,再print 出STDIN发给client 的信息。我尝试着写出第二个print,但不知道第一个print怎么写
第二份TO DO 是要求status return IP 地址和port 的string。 格式是<IP地址>:,我不知道怎么找到hostname,port number也尝试了一下,但感觉不对
第三个 TODO 是要求先print 出在STDOUT中发出的信息,再print 出STDIN发给client 的信息。我不知道怎么写
*请在这段代码的基础上添加代码,不需要另写一段,谢谢

package main.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.net.ssl.HostnameVerifier;
import javax.sound.sampled.Port;

import com.google.common.net.HostAndPort;

/**
 * A server implementation of the talk interface that prints messages from the talk client on
 * STDOUT and prints messages from STDIN to the talk client.
 */
public class TalkServer implements BasicTalkInterface {

  private Socket client;
  private ServerSocket server;
  private BufferedReader sockin;
  private PrintWriter sockout;
  private BufferedReader stdin;

  /**
   * Constructs a socket listening on the specified port.
   * @param portnumber the port to listen for connections on
   */
  public TalkServer(int portnumber) throws IOException {
    this(new ServerSocket(portnumber));
  }

  /**
   * Constructs a talk server from the specified socket server.
   * @param server a connected socket to the use for the server
   */
  public TalkServer(ServerSocket server) throws IOException {
    this.server = server;
    this.client = this.server.accept();
    this.sockin = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
    this.sockout = new PrintWriter(this.client.getOutputStream(), true);
    this.stdin = new BufferedReader(new InputStreamReader(System.in));
  }

  /**
   * Performs asynchronous IO using polling. Should print messages from the client on STDOUT and
   * print messages from STDIN to the client. Messages printed on STDOUT should be prepended with
   * "[remote] ".
   */
  public void asyncIO() throws IOException {

    sockout.println(client.getRemoteSocketAddress()); //自己写的,此处应该先print出 在STDOUT中从client 发出的信息,再print 出STDIN发给client 的信息
    
    // TODO: complete asyncIO 
  }

  /** Closes the socket and frees its resources. */
  public void close() throws IOException {
    this.stdin.close();
    this.sockout.close();
    this.sockin.close();
    this.client.close();
    this.server.close();
  }

  /**
   * Returns the status of the current socket connection as a String. Must include IP addresses
   * and ports. Each IP address and port should be combined as {@code <IPaddress>:<port>}.
   */
  public String status() {
    System.out.println("<" + hostname + ">:<" + ServerSocket.toString(portnumber) +">"); //自己写的,此处要求return status return IP 地址和port 的string。 格式是<IP地址>:<port>
    
 
    // TODO: complete status
    return "";
  }

  /**
   * Performs synchronous IO by blocking on input. Should print messages from the client on STDOUT
   * and print messages from STDIN to the client. Messages printed on STDOUT should be prepended
   * with "[remote] ".
   */
  public void syncIO() throws IOException {
    while (!this.sockin.ready()) {} // blocking with busy waiting
    System.out.printf("[remote] %s\n", this.sockin.readLine()); // readLine() also blocks
    // 此处要求先print 出在STDOUT中发出的信息,再print 出STDIN发给client 的信息
    // TODO: print messsages from STDIN to the client with blocking on input
  }
}


这个是talk的小程序,总共有三个小程序,这是sever的代码,还有俩个是talk和talkclient。

package main.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * A server implementation of the talk interface that prints messages from the talk client on
 * STDOUT and prints messages from STDIN to the talk client.
 */
public class TalkServer implements BasicTalkInterface {

    private Socket client;
    private ServerSocket server;
    private BufferedReader sockin;
    private PrintWriter sockout;
    private BufferedReader stdin;

    /**
     * Constructs a socket listening on the specified port.
     *
     * @param portnumber the port to listen for connections on
     */
    public TalkServer(int portnumber) throws IOException {
        this(new ServerSocket(portnumber));
    }

    /**
     * Constructs a talk server from the specified socket server.
     *
     * @param server a connected socket to the use for the server
     */
    public TalkServer(ServerSocket server) throws IOException {
        this.server = server;
        this.client = this.server.accept();
        this.sockin = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
        this.sockout = new PrintWriter(this.client.getOutputStream());
        this.stdin = new BufferedReader(new InputStreamReader(System.in));

        this.sockout.write(this.status());
        this.sockout.flush();
        this.asyncIO();

    }

    /**
     * Performs asynchronous IO using polling. Should print messages from the client on STDOUT and
     * print messages from STDIN to the client. Messages printed on STDOUT should be prepended with
     * "[remote] ".
     */
    public void asyncIO() throws IOException {
        new Thread(() -> {
            while (true) {
                try {
                    String stdInputString = this.stdin.readLine();
                    this.sockout.write(stdInputString + System.getProperty("line.separator"));
                    this.sockout.flush();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
        new Thread(() -> {
            while (true) {
                try {
                    String inputString = this.sockin.readLine();
                    if (inputString == null) {
                        return;
                    }
                    System.out.println("[remote] " + inputString);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
        // TODO: complete asyncIO
    }

    /**
     * Closes the socket and frees its resources.
     */
    public void close() throws IOException {
        this.stdin.close();
        this.sockout.close();
        this.sockin.close();
        this.client.close();
        this.server.close();
    }

    /**
     * Returns the status of the current socket connection as a String. Must include IP addresses
     * and ports. Each IP address and port should be combined as {@code <IPaddress>:<port>}.
     */
    public String status() {
        String serverInfo = "<" + this.server.getInetAddress().getHostAddress() + ">:<" + this.server.getLocalPort() + ">\n";


        // TODO: complete status
        return serverInfo;
    }

    /**
     * Performs synchronous IO by blocking on input. Should print messages from the client on STDOUT
     * and print messages from STDIN to the client. Messages printed on STDOUT should be prepended
     * with "[remote] ".
     */
    public void syncIO() throws IOException {
        while (true) {
            String inputString = this.sockin.readLine();
            System.out.println("[remote] " + inputString);
            String stdInputString = this.stdin.readLine();
            this.sockout.write(stdInputString + System.getProperty("line.separator"));
            this.sockout.flush();
        }
    }
}



最好再给出 Talk 和 TalkClient 的程序,这样才能一起调试。
如果自己会一些的话,可以参考这些

来进一步完成。