package chatclient;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.*;
public class MainFrame extends javax.swing.JFrame {
public String name;
public Socket socket;
public BufferedReader in;
public PrintWriter out;
public MainFrame() {
//show nickname dialog
name = JOptionPane.showInputDialog(null, "Name", "Enter your name", JOptionPane.PLAIN_MESSAGE);
//connect to server
try {
socket = new Socket("wha.tis.my.ip?", 4444);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error connecting to server. Got Exception: "
+ ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
System.exit(1);
}
//open IO streams
try {
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error opening IO streams. Got Exception: "
+ ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
//send client name
out.println(name);
//should I comment about the following command? ROFL
initComponents();
//create the SwingWorker thread to manage incoming messages without fucking up the GUI
new SwingWorker<Void, Void>()
{
protected Void doInBackground()
{
String s = null;
while(true)
{
try {
if ((s = in.readLine()) != null)
msgTextArea.append(s);
} catch (Exception ex) {
System.err.println("Error reading server messages. Got exception: " + ex.getMessage());
ex.printStackTrace();
}
Thread.yield();
}
}
}.execute();
//assing a WindowListener to close all that socket stuff when the window gets closed
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
//here I close the socket but the in the server the socket doenst closes =o
try{
out.close();
in.close();
socket.close();
}catch(Exception ex){}
}
});
//show main JFrame which runs the client messaging system
setVisible(true);
}
private void initComponents() {
//...I did the GUI in NetBeans...
}
//when the user clicks this button the message in the textarea is sent to server. This works, the message is actually sent...
private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {
String msg = clientMsgTextArea.getText();
clientMsgTextArea.setText(null);
out.println(msg);
}
public static void main(String[] args)
{
new MainFrame();
}
// Variables declaration - do not modify
private javax.swing.JPanel clientMsgPanel;
private javax.swing.JScrollPane clientMsgScrollPane;
private javax.swing.JTextArea clientMsgTextArea;
private javax.swing.JPanel msgPanel;
private javax.swing.JScrollPane msgScrollPane;
private javax.swing.JTextArea msgTextArea;
private javax.swing.JButton sendButton;
private javax.swing.JSplitPane splitPane;
// End of variables declaration
}
package chatserver;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
//stores client information
class Client
{
public String name;
public Socket socket;
public PrintWriter out;
public BufferedReader in;
}
public class Main
{
public static ServerSocket serverSocket;
public static ArrayList<Client> client;
public static final String systemName = "[__system__]";
//prints the String s in the output stream of all clients
public static void printAll(String s)
{
for(Client c: client)
c.out.print(s);//this doenst works. It silently fails. The message is not received by the client
}
public static void main(String[] args)
{
try {
//create server socket to accept connections
serverSocket = new ServerSocket(4444);
} catch (Exception ex) {
System.err.println("Failed to create server socket. Got exception: " + ex.getMessage());
System.exit(1);
}
//arraylist to hold information about every connected client
client = new ArrayList<Client>();
//simple thread to accept new connections
new Thread("ConnectionT"){
public void run()
{
while(true)
{
try {
Socket socket = serverSocket.accept();
System.out.println("[Server]: New connection.");
Client c = new Client();
c.socket = socket;
c.out = new PrintWriter(socket.getOutputStream(), true);
c.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
c.name = c.in.readLine();
client.add(c);
printAll(systemName + ": " + c.name + " gets in.\n");
} catch (Exception ex) {
System.err.println("Failed to accept new connection. Got exception: " + ex.getMessage());
}
Thread.yield();
}
}
}.start();
//main loop. Receives and dispatches messages for all clients
while(true)
{
String batch = new String();//the message batch
String s;
//fill in the batch
for(Client c: client)
{
try {
if(!c.socket.isClosed())
{
if(c.in.ready())
if ((s = c.in.readLine()) != null) //the client messages are perfectly received here
batch += c.name + ": " + s + "\n";
}
else
{
c.socket.close();
client.remove(c);
printAll(systemName + ": " + c.name + "gets out.\n");
}
} catch (Exception ex) {
c.out.println(systemName + ": your message could not be sent. Got exception: " + ex.getMessage());
}
}
//dispatch messages if any
if(batch.length() > 0)
printAll(batch);
Thread.yield();
}
}
}