0

This is a Client-Server GUI Application, here I want to send and receive messages between them. the condition is , when the client send "Hello" to server , the server only reply with this message "Hello Client, I am server". so, how can I use if else in this case?

    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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Sort this out in command line apps. and it will become obvious it has **nothing to do with** Swing or AWT. – Andrew Thompson May 14 '19 at 11:55
  • 1
    How does the sample support the question? It looks like a server, but the message as indicated in the question (i am server) is not in the code. Also, what do you mean by "*how can I use if else in this case?*"? – GMc May 14 '19 at 12:02
  • Possible duplicate of [if-else condition using java 8 stream](https://stackoverflow.com/questions/48283748/if-else-condition-using-java-8-stream) – Feras Al Sous May 26 '19 at 12:34

1 Answers1

0

Look at your code and try to find out where the receiving and processing of the message happens.

Check with an if statement for "Hello" and send back your message via

sendMessage("Hello Client, I am server")
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
J Mers
  • 67
  • 6