try {
while(true) {
connection = new Socket(InetAddress.getByName(ip), port);
output = new ObjectOutputStream(connection.getOutputStream());
input = new ObjectInputStream(connection.getInputStream());
JOptionPane.showMessageDialog(null, (String)input.readObject());
send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == send) {
sendData(inputchat.getText());
createOutput();
}
}});
}
} catch (UnknownHostException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
} catch (HeadlessException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
}
public static void main(String[] args) {
//new Thread(new Server()).start();
new Thread(new Main("Beer chat")).start();;
}
public Main(String name) {
super(name);
add(logo);
add(lab);
add(log);
add(login);
add(author);
add(ipserv);
setLayout(new FlowLayout());
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == login) {
username = log.getText();
System.out.println(username);
lab.hide();
log.hide();
login.hide();
add(inputchat);
add(send);
add(exit);
try {
//output.flush();
output.writeObject("Client connected: " + username);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == exit) {
sendData("disconnected");
System.exit(0);
}
}});
}
public void run() {
try {
while(true) {
connection = new Socket(InetAddress.getByName(ip), port);
output = new ObjectOutputStream(connection.getOutputStream());
input = new ObjectInputStream(connection.getInputStream());
JOptionPane.showMessageDialog(null, (String)input.readObject());;
send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == send) {
sendData(inputchat.getText());
createOutput();
}
}});
}
} catch (UnknownHostException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
} catch (HeadlessException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "Connection problem: " + e);
e.printStackTrace();
}
}
public static void sendData(Object obj) {
try {
//output.flush();
output.writeObject(username + ": " + obj);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createOutput() {
try {
output = new ObjectOutputStream(connection.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}