第一个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。