我正在從一個簡單的教程,我發現一個TCP聊天服務器,我是一種新的Java。我最終將創建一個客戶端類,但我一直在通過telnet測試它。我有幾個問題。我已經設置了服務器以從客戶端獲取命令。例如關閉客戶端連接的「EXIT」和打印「OK」的「用戶名」。如下圖所示:簡單的Java聊天服務器
用戶1:TIM
Welcome to Bob's Chat Server!
Please Enter a User Name:
Tim
Welcome Tim we hope you enjoy your chat today
Bob:Hello
Hi Bob
Tim
OK
EXIT
Closing Connection . . . Goodbye
用戶2:BOB
Welcome to Bob's Chat Server!
Please Enter a User Name:
Bob
Welcome Bob we hope you enjoy your chat today
Hello
Tim:Hi Bob
Tim:Tim
我有,我想解決三個問題:
你會注意到,當USER 1 t用戶名「Tim」(他的用戶名),它表示「OK」,就像我想要的一樣,但它也向用戶2打印了「Tim」。是否有辦法讓它不能發送我輸入的命令?所以當我輸入「Tim」時,它不會將「Tim」打印到USER 2?
當郵件發送給其他用戶時,它顯示誰說的。有沒有辦法在兩個連接上打印名稱?例如,當用戶2說「你好」時,它在兩個連接上看起來更像是「Bob:Hello」?
是否有一種方法可以跟蹤整個聊天會話中所說的所有內容,並將任何用戶請求的聊天內容打印出來?
這裏是我的服務器代碼:
// Chat Server runs at port no. 9020
import java.io.*;
import java.util.*;
import java.net.*;
import static java.lang.System.out;
public class TCPServer
{
Vector<String> users = new Vector<String>();
Vector<HandleClient> clients = new Vector<HandleClient>();
int PORT = 9020;
int NumClients = 10;
public void process() throws Exception
{
ServerSocket server = new ServerSocket(PORT,NumClients);
out.println("Server Connected...");
while(true)
{
Socket client = server.accept();
HandleClient c = new HandleClient(client);
clients.add(c);
} // end of while
}
public static void main(String ... args) throws Exception
{
new TCPServer().process();
} // end of main
public void boradcast(String user, String message)
{
// send message to all connected users
for (HandleClient c : clients)
if (!c.getUserName().equals(user))
{
c.sendMessage(user,message);
}
}
class HandleClient extends Thread
{
String name = "";
BufferedReader input;
PrintWriter output;
public HandleClient(Socket client) throws Exception
{
// get input and output streams
input = new BufferedReader(new InputStreamReader(client.getInputStream())) ;
output = new PrintWriter (client.getOutputStream(),true);
output.println("Welcome to Bob's Chat Server!\n");
// read name
output.println("Please Enter a User Name: ");
name = input.readLine();
users.add(name); // add to vector
output.println("Welcome "+name+" we hope you enjoy your chat today");
start();
}
public void sendMessage(String uname,String msg)
{
output.println(uname + ":" + msg);
}
public String getUserName()
{
return name;
}
public void run()
{
String line;
try
{
while(true)
{
line = input.readLine();
if("EXIT".equals(line))
{
output.println("Closing Connection . . . Goodbye");
clients.remove(this);
users.remove(name);
break;
}
else if(name.equals(line))
{
output.println("OK");
}
boradcast(name,line); // method of outer class - send messages to all
}// end of while
} // try
catch(Exception e)
{
System.out.println(e.getMessage());
}
} // end of run()
} // end of inner class
} // end of Server
任何幫助表示讚賞。謝謝。
感謝這些建議,他們幫助了很多。我有一個更快的問題。如何讓它在光標前顯示用戶的名字?例如,在用戶輸入名稱後,光標始終顯示「用戶名:」,然後光標開始後綴。 – user1174834 2012-01-30 04:33:51
@ user1174834:如果你使用控制檯,這有點困難,請檢查我最近的編輯,這可以爲你做些什麼。 Regards – 2012-01-30 05:38:26
感謝您的幫助。 – user1174834 2012-01-30 15:45:04