import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.xml.ws.handler.MessageContext;
public class client extends JFrame {
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutput output;
private ObjectInputStream input;
private String message= "";
private String serverIp;
private Socket connection;
//constructor
public client(String host){
super("CLINENT MOFO !!!!");
serverIp = host;
userText.setEditable(false);
userText.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText,BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow),BorderLayout.CENTER);
setSize(300,150);
setVisible(true);
}
//connect to server
public void startRunning(){
try{
connectToServer();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Client terminated connection");
}catch(IOException ioException){
ioException.printStackTrace();
}finally{
closeCrap();
}
}
// connect to server
private void connectToServer() throws IOException{
showMessage("Attempting connection.....\n");
connection = new Socket(InetAddress.getByName(serverIp),6789);
showMessage("Connected to : " + connection.getInetAddress().getHostName());
}
//setting up streams to recive massages
private void setupStreams() throws IOException{
output= new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n dude your streams are now good to go");
}
//while chatting method
private void whileChatting() throws IOException{
ableToType(true);
do{
try{
message=(String) input.readObject();
showMessage("\n" + message);
}catch(ClassNotFoundException classNotFoundException){
showMessage("\n i dont know that object type");
}
}while(!message.equals("SERVER - END"));
}
//close crap to close everything
private void closeCrap(){
showMessage("\n closing crap down...");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//send massages to server
private void sendMessage(String message){
try{
output.writeObject("CLIENT - " + message);
output.flush();
showMessage("\n CLIENT - " + message);
}catch(IOException ioException){
chatWindow.append("\n something messed up with sending massages");
}
}
//show message to show all massage to window
private void showMessage(final String m){
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
chatWindow.append(m);
}
}
);
}
// give user permission to type
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
userText.setEditable(tof);
}
}
);
}
}