我无法让PrintStream正常工作。我用命令建立了一个基本的聊天室(请原谅我的代码,如果它的混乱,我只是把它扔在一起)但输入你的名字打印语句只在输入名称后运行。因此它会挂起光标,直到提交文本然后打印
Test (This is the input)
Enter your name:
Welcome to the channel Test. Current members: Test,
第一行是输入,第二行和第三行是生成的print语句。我缺少一个解决方案吗? (已导入所有适当的类别)
public class FinalProjectClientThread extends Thread
{
public DataInputStream is = null;
public PrintStream os = null;
public Socket clientSocket = null;
public final FinalProjectClientThread[] threads;
public int clientCount;
public String ClientName;
private boolean isModerator;
private boolean isMuted;
public FinalProjectClientThread(Socket clientSocket, FinalProjectClientThread[] threads)
{
this.clientSocket = clientSocket;
this.threads = threads;
clientCount = threads.length;
isModerator = false;
}
public String getClientName()
{
return ClientName;
}
private void setClientName(String n)
{
ClientName = n;
}
public void stopServer()
{
System.exit(0);
}
public void kickUser()
{
try
{
clientSocket.close();
}
catch(IOException e)
{
}
}
public void muteUser()
{
isMuted = true;
}
public void run()
{
int maxClients = this.clientCount;
FinalProjectClientThread[] threads = this.threads;
try
{
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
os.flush();
os.print("");
os.flush();
os.print("Enter your name: ");
os.flush();
String name = is.readLine();
this.setClientName(name);
os.print("\nWelcome to the channel " + name+". ");
os.flush();
//Count number of users in channel
int count = 0;
for(int i = 0; i < maxClients; i++)
{
if(threads[i]!=null&&threads[i].getClientName() != null)
{
count++;
}
}
//Print text based on number
if(count == 0)
{
os.println("There are no other members.");
os.flush();
}
else
{
os.print("Current members: ");
os.flush();
}
//Print member's names
for(int i = 0; i < maxClients; i++)
{
if(threads[i]!=null&&threads[i].getClientName() != null)
{
os.print(threads[i].getClientName()+ ", ");
os.flush();
}
}
os.println();
os.flush();
for(int i = 0; i < maxClients; i++)
{
if (threads[i] != null && threads[i] != this)
{
threads[i].os.println(name + " has joined the channel");
}
}
while (true)
{
String line = is.readLine();
if (line.equals("/leave"))
{
break;
}
else if(line.equals("/mod"))
{
os.println("Enter the moderator code: ");
String line2 = is.readLine();
if(line2.equals("hurricanemod"))
{
isModerator = true;
for (int i = 0; i < maxClients; i++)
{
if (threads[i] != null)
{
threads[i].os.println(name+" is now a moderator");
}
}
}
else
{
os.println("That code is incorrect");
}
}
else if(line.equals("/help"))
{
os.print("The following commands are valid: leave, help, mod, kick, mute, unmute");
}
else if(line.indexOf("/kick") == 0)
{
if(isModerator)
{
for(int i = 0; i < maxClients; i++)
{
if(threads[i] != null && threads[i].getClientName().equals(line.substring(6, line.length())))
{
threads[i].kickUser();
}
}
os.println("User kicked");
}
else
{
os.println("You must be a moderator to kick people. Use /mod and the code provided by the server hoster");
}
}
else if(line.equals("/stop"))
{
if(isModerator)
{
this.stopServer();
}
else
{
os.println("You must be a moderator to stop the server. Use /mod and the code provided by the server hoster");
}
}
else if(line.indexOf("/mute") == 0)
{
if(isModerator)
{
for(int i = 0; i < maxClients; i++)
{
if(threads[i] != null && threads[i].getClientName().equals(line.substring(6, line.length())))
{
threads[i].muteUser();
}
}
os.println(line.substring(6, line.length())+ " was muted.");
}
else
{
os.println("You must be a moderator to kick people. Use /mod and the code provided by the server hoster");
}
}
else
{
if(!isMuted)
{
for (int i = 0; i < maxClients; i++)
{
if (threads[i] != null)
{
threads[i].os.println(name+": " + line);
}
}
}
else
{
os.println("You are muted");
}
}
}
for (int i = 0; i < maxClients; i++)
{
if (threads[i] != null && threads[i] != this)
{
threads[i].os.println(name + " left the channel");
}
}
os.println("You have left the channel");
for (int i = 0; i < maxClients; i++)
{
if (threads[i] == this)
{
threads[i] = null;
}
}
is.close();
os.close();
clientSocket.close();
}
catch (IOException e)
{
System.err.println("IOExcepetion(Thread)");
}
}
}
服务器
public class FinalProjectServer
{
private static Scanner kb = new Scanner (System.in);
private static ServerSocket serverSocket = null;
private static Socket clientSocket = null;
private static int clientCount = 10;
private static FinalProjectClientThread[] clientList = new FinalProjectClientThread[clientCount];
public static void main(String[] args)
{
/*System.out.print("Enter port for server to run on: ");
int port = kb.nextInt();*/
int port = 25565;
try
{
serverSocket = new ServerSocket(port);
}
catch(IOException e)
{
System.err.println("IOException");
}
while(true)
{
try
{
clientSocket = serverSocket.accept();
int i = 0;
for(i = 0; i < clientCount; i++)
{
if(clientList[i] == null)
{
clientList[i] = new FinalProjectClientThread(clientSocket,clientList);
clientList[i].start();
break;
}
}
if(i == clientCount)
{
PrintStream os = new PrintStream(clientSocket.getOutputStream());
os.println("Max number of client reached. Please contact server host.");
os.close();
clientSocket.close();
}
}
catch(IOException e)
{
System.err.println(e);
}
}
}
}
客户端
public class FinalProjectClient implements Runnable
{
private static Scanner kb = new Scanner(System.in);
// The client socket
private static Socket clientSocket = null;
// The output stream
private static PrintStream os = null;
// The input stream
private static DataInputStream is = null;
private static BufferedReader inputLine = null;
private static boolean closed = false;
public static void main(String[] args)
{
System.out.print("Enter the IP of the host computer: ");
String IP = kb.nextLine();
System.out.print("Enter the port number: ");
int port = kb.nextInt();
try
{
clientSocket = new Socket(IP, port);
inputLine = new BufferedReader(new InputStreamReader(System.in));
os = new PrintStream(clientSocket.getOutputStream());
is = new DataInputStream(clientSocket.getInputStream());
}
catch(UnknownHostException e)
{
System.err.println("Can not find host, Unknown");
}
catch(IOException e)
{
System.err.println("IOException");
}
try
{
new Thread(new FinalProjectClient()).start();
while(!closed)
{
os.println(inputLine.readLine());
}
os.close();
is.close();
clientSocket.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
public void run()
{
String reply;
try
{
while((reply = is.readLine()) != null)
{
System.out.println(reply);
if(reply.indexOf("/leave") != -1)
break;
}
closed = true;
}
catch(IOException e)
{
System.err.println(e);
}
}
}