Hi Everyone,
I'm making a Java chat application, but it freezes whenever I click "Connect." My code is attached below. Thanks.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class CinnaChat extends JFrame
{
JLabel lblServerIP = new JLabel("Server IP:");
JTextField serverIP = new JTextField(15);
JLabel lblPort = new JLabel("Port Num:");
JTextField port = new JTextField(15);
JLabel lblName = new JLabel("NickName:");
JTextField name = new JTextField(15);
JRadioButton serverBTN = new JRadioButton("Server (Host)");
JRadioButton clientBTN = new JRadioButton("Client (Guest)");
JButton connect = new JButton("Connect");
JButton disconnect = new JButton("Disconnect");
JTextArea messages = new JTextArea("",10,21);
JTextField mssg = new JTextField(15);
JButton send = new JButton("Send");
Font btn = new Font("times new roman",Font.BOLD,20);
Socket skt = null;
Socket skt2 = null;
ServerSocket srvr = null;
boolean server = true;
public CinnaChat()
{
super("CinnaChat");
setSize(500,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container contentArea = getContentPane();
contentArea.setBackground(Color.lightGray);
GridLayout flowManager = new GridLayout(1,2);
contentArea.setLayout(flowManager);
JPanel pnl = new JPanel();
pnl.add(lblServerIP);
pnl.add(serverIP);
pnl.add(lblPort);
pnl.add(port);
pnl.add(lblName);
pnl.add(name);
ButtonGroup type = new ButtonGroup();
type.add(serverBTN);
serverBTN.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
server = true;
}
}
);
type.add(clientBTN);
clientBTN.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
server = false;
}
}
);
pnl.add(serverBTN);
pnl.add(clientBTN);
pnl.add(connect);
connect.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
connect();
}
}
);
connect.setFont(btn);
pnl.add(disconnect);
disconnect.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
disconnect();
}
}
);
disconnect.setFont(btn);
contentArea.add(pnl);
JPanel pnl2 = new JPanel();
pnl2.add(new JScrollPane(messages,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
pnl2.add(mssg);
pnl2.add(send);
send.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
send();
}
}
);
contentArea.add(pnl2);
setContentPane(contentArea);
}
public void connect()
{
try
{
if(server)
{
int portNum = Integer.parseInt(port.getText());
srvr = new ServerSocket(portNum);
skt = srvr.accept();
messages.append("\nServer, "+name.getText()+", has connected.\n\n");
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
while (!in.ready()) {}
messages.append(in.readLine()+"\n");
in.close();
skt.close();
srvr.close();
}
else
{
int portNum = Integer.parseInt(port.getText());
skt2 = new Socket(serverIP.getText(), portNum);
BufferedReader in = new BufferedReader(new
InputStreamReader(skt2.getInputStream()));
while (!in.ready()) {}
messages.append(in.readLine()+"\n");
in.close();
}
}
catch(Exception e1)
{
JOptionPane.showMessageDialog(null, "Error: Connection failed.\n"+
"Please retry. Check IP Adress\n"+
"and the port number.", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
public void disconnect()
{
try{
skt.close();
skt2.close();
srvr.close();
}
catch(Exception e2)
{
}
}
public void send()
{
try
{
if(server)
{
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
messages.append(name.getText() + ": " + mssg.getText() + "\n");
out.print(name.getText() + ": " + mssg.getText() + "\n");
out.close();
}
else
{
PrintWriter out = new PrintWriter(skt2.getOutputStream(), true);
messages.append(name.getText() + ": " + mssg.getText() + "\n");
out.print(name.getText() + ": " + mssg.getText() + "\n");
out.close();
}
}
catch(Exception e3)
{
}
}
public static void main (String [] args)
{
new CinnaChat();
}
}