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

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条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

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

      悬赏问题

      • ¥15 关于Java文件处理的问题
      • ¥15 想问一下,这个图怎么写程序才能实现左边两位0到99秒计时啊?或者是左边两99秒到0右边两位显示当天日期
      • ¥30 用Rsync出现报错
      • ¥15 Java建模,处理文件和对象
      • ¥15 linux磁盘怎样重新挂载到原挂载路径下
      • ¥15 Java的类和静态方法
      • ¥15 存在一定规律的数据转置和整理
      • ¥15 关于flowable工作流开发的一个问题
      • ¥20 VBA如何用代码在onedrive创建文件夹?
      • ¥15 多理发师睡眠问题,2个理发师,n个座椅