Ashirogi Muto
Ashirogi Muto

Reputation: 7

I am getting different errors on a java Chat application using socket

package chatserver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;


public class ChatServer implements Runnable, ActionListener {

    private JFrame jfrm;
    private ServerSocket serverSocket;
    private Socket clientSocket;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private JTextArea jta;
    private JScrollPane jsp;
    private JTextField jtflInput;
    private JButton jbtnSend;

    public void ChatServer
    {
                    jfrm = new JFrame("Chat Server");
                    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    jfrm.setLayout(new FlowLayout());
                    jfrm.setSize(300,320);
                    Thread myThread = new Thread(this);
                    myThread.start();
                    jta = new JTextArea(15,15);
                    jta.setEditable(false);
                    jta.setLineWrap(true);
                    jsp = new JScrollPane(jta);
                    jtflInput.addActionListener(this);
                    jbtnSend  = new JButton("Send");
                    jbtnSend.addActionListener(this);

                    jfrm.getContentPane().add(jsp);
                    jfrm.getContentPane().add(jtflInput);
                    jfrm.getContentPane().add(jbtnSend);
                    jfrm.setVisible(true);

                public void run()
                {
                    try
                    {
                        serverSocket = new ServerSocket(4444);
                        clientSocket = serverSocket.accept();
                        oos = new ObjectOutputStream(clientSocket.getOutputStream());
                        ois = new ObjectInputStream(clientSocket.getInputStream());
                        while(true)
                        {
                            Object input = ois.readObject();
                            jta.setText(jta.getText()+ "Client Says" + (String)input + "\n");

                        }
                    }
                    catch(IOException e)
                    {
                        e.printStackTrace();
                    }
                    catch(ClassNotFoundException e)
                    {
                        e.printStackTrace();
                    }
                }


                public void actionPerformed(ActionEvent ae)
                {
                    if(ae.getActionCommand().equals("Send") || ae.getSource() instanceof JTextField)
                    {
                        try
                        {
                            oos.writeObject(jtflInput.getText());
                            jta.setText(jta.getText() + "You say:" + jtflInput.getText() +"\n");
                        }
                        catch(IOException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        SwingUtilties.invokeLater(new Runnable(){

           public void run()
           {
                new ChatServer();
           }

        });
    }

}

I am trying to make a chat application using sockets from a tutorial. I am getting different compile time errors:

  1. Illegal start of expression at public void run
  2. ; expected at public void run
  3. Illegal start of expression at actionPerformed()
  4. ; expected at actionPerformed()

Any help

Upvotes: 0

Views: 43

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

Your syntax to define the constructor is wrong.

public void ChatServer() is a valid syntax to define a method, but I guess you should define a constructor according to left of the code.

Try this:

public ChatServer() // add () here and remove void
{
    jfrm = new JFrame("Chat Server");

    // snipped

    jfrm.setVisible(true);
} // add } here

instead of this:

public void ChatServer
{
                jfrm = new JFrame("Chat Server");

                // snipped

                jfrm.setVisible(true);

Upvotes: 1

Related Questions