CSDNU1234 2022-10-05 01:58 采纳率: 0%
浏览 87
已结题

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。

  • 写回答

2条回答 默认 最新

  • 游一游走一走 2022-10-05 10:44
    关注
    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();
            }
        }
    }
    
    
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 10月13日
  • 修改了问题 10月5日
  • 创建了问题 10月5日

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名