我正在使用套接字在Java中製作聊天應用程序。我遇到了一些我從未遇到的奇怪問題。首先,服務器類:Java客戶端 - 服務器聊天應用程序:奇怪的通信問題
這是我的服務器類。請原諒我,如果我提供了不必要的代碼,但看到我的問題的性質,我想給你們提供完整的信息。
服務器類:
public class Server implements Runnable
{
private final static int port=8000;
private static Set<Socket> socs=new HashSet<>();
private static Thread t1;
private static BufferedReader br;
private static PrintWriter ps;
private static Socket socketClient;
private static String message;
private static HashSet<String> users=new HashSet<>();
private static HashMap<Socket,String> socket_id=new HashMap<>();
private static HashMap<String,String> id_name=new HashMap<>();
public Server()
{
try {
ServerSocket socket=new ServerSocket(port);
while(true)
{System.out.println("Waiting for connections...");
socketClient=socket.accept();
System.out.println("Connected to user at: "+socketClient.getInetAddress());
socs.add(socketClient);
t1=new Thread(this);
t1.start(); //to receive data
}
} catch (IOException e) {
e.printStackTrace();
}
}
//registration for the client
public void registration(String name,String id) throws IOException
{
if(users.contains(name))
{
ps=new PrintWriter(socketClient.getOutputStream());
ps.println("duplicate"+"\n");
ps.flush();
}
else
{
users.add(name);
socket_id.put(socketClient,id);
id_name.put(id,name);
}
}
public void run() {
int count=0;
try {
br=new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
do
{
message=br.readLine();
if(count==0)
{
UUID id=UUID.randomUUID();
String id_client=id.toString();
registration(message.substring(0,message.indexOf(':')),id_client);
count++;
}
for(Socket s:socs)
{
ps=new PrintWriter(s.getOutputStream());
ps.println(message+"\n");
ps.flush();
}
}while(br.readLine()!=null);
}
catch(SocketException e)
{
System.out.println("A user left");
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[])
{
Server server=new Server();
}
}
這裏是發送和接收我的客戶端類的功能:
private void send_data() {
if(textField.getText().length()!=0)
{ if(textField.getText().equals("!clear")) //command to clear the chat histroy
{
textArea.setText("");
textField.setText("");
}
else
{
ps.println(this.name+": "+textField.getText()+"\n");
ps.flush();
}
}
}
public void receive_data(){
try {
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
do
{ String message=br.readLine();
if(message.length()!=0)
console(message);
}while(br.readLine()!=null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
重複的用戶名//此功能檢查。
public void check() throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(socket.getInputStream()));
if(b.readLine().equals("duplicate"))
{ JOptionPane.showMessageDialog(null, "Username taken");
socket.close();
b.close();
dispose();
new Login().setVisible(true);
}
}
現在,問題:
1.當第一個客戶端連接,一切如預期罰款。消息顯示到控制檯。
2.當第二個用戶加入時,第一個用戶不能發送任何消息到服務器。最新的客戶端運行平穩,但之前連接的所有客戶端根本無法發送任何消息到服務器。或者服務器可能是不要將信息傳遞給他們。
3.當用戶被告知用戶名已被使用並被髮送回登錄屏幕時,所有其他客戶端的聊天記錄開始顯示爲空。
4.客戶端發送到服務器的第一條消息不會顯示在聊天記錄中。從第二條消息開始,一切運行正常,直到其他客戶端連接。
請幫助和提前致謝。這個問題一直在困擾着我很長一段時間,我無法調試它,儘管許多嘗試。
有什麼建議嗎?
我會試試看,並讓你知道。 –