CSDNU1234 2022-10-06 21:07 采纳率: 0%
浏览 38
已结题

TalkClient

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

package main.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.channels.AcceptPendingException;

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

  private Socket socket;
  private BufferedReader sockin;
  private PrintWriter sockout;
  private BufferedReader stdin;

  /**
   * Constructs a socket that connects to the specified host on the specified port.
   * @param hostname the host to connect to
   * @param portnumber the port to connect to or listen on
   */
  public TalkClient(String hostname, int portnumber) throws IOException {
    this(new Socket(hostname, portnumber));
  }

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

  /**
   * Performs asynchronous IO using polling. Should print messages from STDIN to the server
   * and print messages from the server on STDOUT. Messages printed on STDOUT should be prepended
   * with "[remote] ".
   */
  public void asyncIO() throws IOException {
    
   
    // 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.socket.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.socket.getInetAddress().getHostAddress() + ">:<" + this.socket.getLocalPort() +">");

    // TODO: complete status
    
    return "";
  }

  /**
   * Performs synchronous IO by blocking on input. Should print messages from STDIN to the server
   * and print messages from the server on STDOUT. Messages printed on STDOUT should be prepended
   * with "[remote] ".
   */
  public void syncIO() throws IOException {
    while (!this.stdin.ready()) {} // blocking with busy waiting
    this.sockout.println(this.stdin.readLine()); // readLine() also blocks
    

    // TODO: print messages from the server on STDOUT with blocking
  }
}


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

  • 写回答

1条回答 默认 最新

  • 游一游走一走 2022-10-06 22:51
    关注
    package main.java;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    /**
     * A client implementation of the talk interface that prints messages from STDIN to the talk server
     * and prints messages from the talk server on STDOUT.
     */
    public class TalkClient implements BasicTalkInterface {
    
        private Socket socket;
        private BufferedReader sockin;
        private PrintWriter sockout;
        private BufferedReader stdin;
    
        /**
         * Constructs a socket that connects to the specified host on the specified port.
         *
         * @param hostname   the host to connect to
         * @param portnumber the port to connect to or listen on
         */
        public TalkClient(String hostname, int portnumber) throws IOException {
            this(new Socket(hostname, portnumber));
        }
    
        /**
         * Constructs a talk client from the specified socket.
         *
         * @param socket a connected socket to use for the client connection
         */
        private TalkClient(Socket socket) throws IOException {
            this.socket = socket;
            this.sockin = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            this.sockout = new PrintWriter(this.socket.getOutputStream());
            this.stdin = new BufferedReader(new InputStreamReader(System.in));
            this.asyncIO();
        }
    
        /**
         * Performs asynchronous IO using polling. Should print messages from STDIN to the server
         * and print messages from the server on STDOUT. 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) {
                        e.printStackTrace();
                        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) {
                        e.printStackTrace();
                        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.socket.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.socket.getInetAddress().getHostAddress() + ">:<" + this.socket.getLocalPort() + ">");
    
            // TODO: complete status
    
            return serverInfo;
        }
    
        /**
         * Performs synchronous IO by blocking on input. Should print messages from STDIN to the server
         * and print messages from the server on STDOUT. Messages printed on STDOUT should be prepended
         * with "[remote] ".
         */
        public void syncIO() throws IOException {
            while (true) {
                String stdInputString = this.stdin.readLine();
                this.sockout.write(stdInputString + System.getProperty("line.separator"));
                this.sockout.flush();
                String inputString = this.sockin.readLine();
                System.out.println("[remote] " + inputString);
            }
        }
    }
    
    
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 10月14日
  • 创建了问题 10月6日

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)