我对此代码有疑问:
public ServerClient(Socket s, int id, String name)throws IOException{
this.name = name;
this.id = id;
this.mysocket = s;
this.out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
this.in = new BufferedReader(new InputStreamReader(s.getInputStream()));
IgnoreList = new ArrayList<ServerClient>();
}
private void print(String s){
//System.out.println(s);
this.out.print(s);
}
问题是,当我在构造函数中编写'this.out.print(“SomeString”)'并执行程序时,PrintWriter会完成它的工作并写入OutputStream。当我在下面的方法中完全相同时,它什么也没打印出来。我在print方法中检查过String,它不只是“”。 现在我不知道为什么会这样......
以下是该程序的完整代码,一个连接客户端的聊天室:
import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
public class Server {
private String name;
private ConcurrentHashMap<Integer,ServerClient> chm;
private ServerSocket socket;
private int ClientID;
public Server(String name,int portnr) throws IOException{
this.name = name;
this.socket = new ServerSocket(portnr);
this.ClientID =0;
chm = new ConcurrentHashMap<Integer,ServerClient>();
}
public static void main(String[] args){
try{
new Server("Chatroom", 6000).work();
}catch(IOException e){
System.out.println("IO Exception: "+e.getMessage());
}
}
public void work()throws IOException{
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run() {
try{
if(socket != null){
socket.close();
}
}catch(IOException e){
System.out.println("IO Exception: "+e.getMessage());
}
}
});
while(true){
Socket client = socket.accept();
int ClientID = this.ClientID++;
ServerClient servant = new ServerClient(client,ClientID,"Client "+ClientID);
this.chm.put(ClientID,servant);
Thread t = new Thread(servant);
t.start();
servant.sendPersonalMessage("Beigetreten");
}
}
private void writeMessage(String s, ServerClient client){
String message = String.format("[%s]: %s",client.name,s);
for(ServerClient sc : this.chm.values()){
if(!sc.IgnoreList.contains(client)){
sc.print(message);
}
}
}
class ServerClient implements Runnable{
private ArrayList<ServerClient> IgnoreList;
private String name;
private int id;
private Socket mysocket;
private PrintWriter out;
private BufferedReader in;
public ServerClient(Socket s, int id, String name)throws IOException{
this.name = name;
this.id = id;
this.mysocket = s;
this.out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
this.in = new BufferedReader(new InputStreamReader(s.getInputStream()));
IgnoreList = new ArrayList<ServerClient>();
}
private void print(String s){
//System.out.println(s);
this.out.print(s);
}
@Override
public void run(){
boolean exit = false;
writeMessage("Entering Chatroom",this);
try{
String read;
while(!exit && (read=in.readLine()) != null){
String[] command = read.split(" ");
switch (command[0]){
case "CNAME" :
if(command.length != 2){
sendPersonalMessage("Korrekte Eingabe bitte: CNAME [neuer username]");
}else this.name = command[1];
break;
case "IGNORE" :
if(command.length != 2){
sendPersonalMessage("Korrekte Eingabe bitte: IGNORE [zu ignorierender username]");
}else{
ServerClient ignore = null;
ServerClient actual = this;
Iterator<ServerClient> candidates = chm.values().iterator();
while(ignore == null && candidates.hasNext()){
actual = candidates.next();
if(actual.name.matches(command[1])){
ignore = actual;
IgnoreList.add(actual);
}
}
}
break;
case "LEAVE" :
try{
closeConnection();
}catch(IOException e){
System.out.println("IO Exception: "+e.getMessage());
exit = true;
}
break;
default : {
writeMessage(read,this);
}
break;
}
}
}catch(IOException e){
System.out.println("IO Exception: "+e.getMessage());
}finally{
exit = true;
try{
this.closeConnection();
}catch(IOException e){
System.out.println("IO Exception: "+e.getMessage());
}
}
}
public void sendPersonalMessage(String s){
this.out.println(s);
this.out.flush();
}
private void closeConnection() throws IOException{
if(!mysocket.isClosed()){
chm.remove(this.id);
mysocket.close();
in.close();
out.close();
writeMessage(name+" has left the Chatroom",this);
}
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ServerClient other = (ServerClient) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (id != other.id)
return false;
return true;
}
private Server getOuterType(){
return Server.this;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + id;
return result;
}
}
}