第一个TO DO 是要在asyncIO先print 出STDIN发给server的信息,再print出 在STDOUT中从server 发出的信息。
第二份TO DO 是要求status return IP 地址和port 的string。 格式是
第三个 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 :}.
*/
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。
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);
}
}
}