Reputation: 7
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:
public void run
;
expected at public void run
actionPerformed()
;
expected at actionPerformed()
Any help
Upvotes: 0
Views: 43
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