0
我正在修改關於如何創建接受多個客戶端的TCP聊天服務器的教程。我最終還會創建一個客戶端類,但到目前爲止,我正在使用TELNET進行測試。TCP聊天服務器
我希望服務器不斷檢查輸入,以便我可以使用關鍵字預先形成服務器功能。因此,字符串單詞「EXIT」斷開客戶端和字符串單詞「Name:」以打印「OK」。
這是我在想什麼,但它不工作:
public void run()
{
String line;
try
{
while(true)
{
if (input.readline("EXIT"))//Should close and remove client
{
clients.remove(this);
users.remove(name);
break;
}
if(input.readline("Name:"))//Should print OK with username
{
System.out.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()
}
}
這裏是整個服務器類
// 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)
{
if (input.readline("EXIT"))
{
clients.remove(this);
users.remove(name);
break;
}
if(input.readline(name))
{
System.out.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
請根據「*不起作用*」進行展開。編譯錯誤?運行時錯誤?意外的功能? (你看到什麼,與你期望的是什麼?) – ziesemer 2012-01-28 19:48:03
現在,這是我編譯時得到的結果: TCPServer.java:79:找不到符號 symbol:method readline(java.lang.String ) 位置:類java.io.BufferedReader中 \t \t如果(input.readline( 「EXIT」)) \t \t^ TCPServer.java:85:找不到符號 符號:方法的ReadLine(java.lang.String中) 位置:類java.io.BufferedReader中 \t \t如果(input.readline( 「姓名:」)) \t \t^ 2錯誤 – user1174834 2012-01-28 19:57:03
您應該使用「WebSockets」代替聊天服務器。這是現代化的做法。 – djangofan 2012-01-28 20:01:33