这是一个客户端服务器GUI应用程序,在这里我想在它们之间发送和接收消息。条件是,当客户端向服务器发送“ Hello”时,服务器仅以该消息“ Hello Client,I am server”答复。 那么,在这种情况下我该如何使用?
public void startRunning() throws ClassNotFoundException
{
try
{
server = new ServerSocket(port, totalClients);
while(true)
{
try
{
status.setText(" Waiting for Someone to Connect...");
connection = server.accept();
status.setText(" Now Connected to " + connection.getInetAddress().getHostName());
output = new ObjectOutputStream(connection.getOutputStream());
//output.flush();
input = new ObjectInputStream(connection.getInputStream());
whileChatting();
}catch(EOFException eofException)
{
}
}
}
catch(IOException ioException)
{
}
}
private void whileChatting() throws IOException, ClassNotFoundException
{
String message="";
jTextField1.setEditable(true);
do{
message = (String) input.readObject();
chatArea.append("\n"+message);
}
while(!message.equalsIgnoreCase("Client - Quit"));
}
private void sendMessage(String message)
{
try
{
output.writeObject("Server - " + message);
output.flush();
chatArea.append("\nServer - " + message);
}
catch(IOException ioException)
{
chatArea.append("\n Unable to Send Message");
JOptionPane.showMessageDialog(null,"Unable to Send Message", "Warning", JOptionPane.WARNING_MESSAGE);
}
}
答案 0 :(得分:0)
查看您的代码,然后尝试找出接收和处理消息的位置。
使用if语句检查“ Hello”,然后通过
发回邮件sendMessage("Hello Client, I am server")