在让我的客户端服务器聊天应用程序的客户端正常工作时,我遇到了问题。 服务器端工作正常。如果我通过telnet连接到它,它可以将消息重定向到我想要的客户端,他们会收到很好的消息。 问题是,当我尝试在客户端项目中创建客户端套接字时,我没有收到来自服务器的任何消息,也无法发送来自客户端的消息。 我在网上搜索了解决方案,但是所有解决方案几乎都是相同的,当我实施该解决方案时,它根本无法正常工作。第一次构建此类应用程序,因此我不能说对此很有经验,这可能就是为什么我无法使其正常工作的原因。
以下是与问题相关的代码: ClientSocket类
public class ClientSocket
{
private Socket m_Socket;
private DataOutputStream m_DataOutputStream;
private DataInputStream m_DataInputStream;
public ClientSocket()
{
try
{
m_Socket = new Socket("localhost", 8828);
m_DataOutputStream = new DataOutputStream(m_Socket.getOutputStream());
m_DataInputStream = new DataInputStream(m_Socket.getInputStream());
ReadMessage.start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void WriteMessage(String line)
{
try
{
m_DataOutputStream.writeUTF(line);
}
catch (IOException e)
{
e.printStackTrace();
}
}
Thread ReadMessage = new Thread()
{
public void run()
{
System.out.println("anyád");
String line = "";
while (true)
{
try
{
line = m_DataInputStream.readUTF();
m_DataOutputStream.writeUTF(line);
System.out.println("printed: " + line);
}
catch(IOException i)
{
System.out.println(i);
}
}
}
};
}
登录按钮点击监听器
button_ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(m_Login)
{
Integer userID;
if((userID = DataManager.Login(textField_username.getText(), textField_password.getText())) != -1)
{
System.out.println("Login successful!");
m_WindowLogin.setVisible(false);
m_ClientSocket = new ClientSocket();
m_ClientSocket.WriteMessage("login " + userID);
WindowMain windowMain = new WindowMain(userID);
windowMain.setVisible(true);
}
else
{
System.out.println("Login failed!");
}
}
else
{
if(DataManager.Register(textField_username.getText(), textField_password.getText()))
{
System.out.println("Registration successful!");
}
else
{
System.out.println("Registration failed!");
}
}
}
}
);
服务器类
public class Server extends Thread
{
private final int m_Port;
private ArrayList<Client> m_Clients;
public Server(int port)
{
m_Port = port;
m_Clients = new ArrayList<Client>();
}
public ArrayList<Client> GetClients()
{
return m_Clients;
}
public void run()
{
try
{
ServerSocket serverSocket = new ServerSocket(m_Port);
while(true)
{
Socket clientSocket = serverSocket.accept();
Client client = new Client(clientSocket, this);
m_Clients.add(client);
client.start();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
服务器端客户端类
public class Client extends Thread
{
private final Socket m_ClientSocket;
private final Server m_Server;
private DataOutputStream m_DataOutputStream;
private DataInputStream m_DataInputStream;
private Integer m_UserID;
public Client(Socket clientSocket, Server server)
{
m_ClientSocket = clientSocket;
m_Server = server;
try
{
DataManager.DBConnect("localhost", "3306", "chat", "root", "");
m_DataOutputStream = new DataOutputStream(clientSocket.getOutputStream());
m_DataInputStream = new DataInputStream(clientSocket.getInputStream());
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void run()
{
try {
HandleClient();
} catch (IOException e) {
e.printStackTrace();
}
}
public Integer GetUserID()
{
return m_UserID;
}
public void Send(String message)
{
try
{
m_DataOutputStream.writeUTF(message);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void HandleClient() throws IOException
{
try
{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(m_DataInputStream));
String line;
while((line = bufferedReader.readLine()) != null)
{
String[] tokens = line.split(" ");
if(tokens != null && tokens.length > 0)
{
String command = tokens[0];
if(command.equalsIgnoreCase("quit"))
{
Send("quit");
}
else if(command.equalsIgnoreCase("msg"))
{
SendMessage(tokens);
}
else if(command.equalsIgnoreCase("login"))
{
Login(tokens);
}
}
/*
for(Client client : m_Server.GetClients())
{
if(client != this)
{
client.Send(line);
}
}
*/
}
m_ClientSocket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private boolean Login(String[] tokens)
{
if(tokens.length == 2)
{
m_UserID = Integer.parseInt(tokens[1]);
return true;
}
return false;
}
private void SendMessage(String[] tokens)
{
if(m_UserID != null)
{
if(tokens.length >= 4)
{
Integer roomID = Integer.parseInt(tokens[1]);
HashMap<Integer, String> users = DataManager.GetUsersInRoom(roomID);
for(Client client : m_Server.GetClients())
{
if(users.containsKey(client.GetUserID()))
{
client.Send(String.join(" ", tokens));
}
}
}
}
}
}
压痕有点奇怪,对此感到抱歉。 谢谢您的提前帮助!