import java.net.Socket;Again we import the Socket class, and the io package. We will also need to import the awt package to use the GUI (Graphical User Interface) classes, and we import the awt.event package, so we are able to find out when the return key is pressed and to close the window.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Client extends Frame implements Runnable,ActionListener{Like I said, we will extend the Frame class. This class makes windows that look like the windows in win 95. We also implement the Runnable interface, this is necesary to do multithreading. And we implement the ActionListener interface, this interface listens for action (not a good defenition). That means it invokes a method called ActionPerformed, whenever a button is clicked or return is hit whithin a textfield (as long as the ActionListener (this class) is registered with the object).Socket sock;
InputStream in;
OutputStream out;
static TextArea area;
TextField txt;
public Client(int port,String host){Of course we need to pass the port number and the client to this method, so we know where to connect to. In the try/catch clause we initialize the socket, Input and Output streams. Then we initialize the the textfield, the int that we pass on to the constructor is the number of characters that the textfield will contain. This isn't actually necesary, because the size of the textfield will be adjusted to fit the frame. Then we add an actionlistener, this is a class which implements the ActionListener interface, this class does so we use the "this" keyword. Then we use the component method "add" to add it to the frame, the position where we add it is south which is at the bottom of the page.try{
sock=new Socket(host,port);
in=sock.getInputStream();
out=sock.getOutputStream();
}
catch(IOException ex){}txt=new TextField(20);
txt.addActionListener(this);
add("South",txt);area=new TextArea(20,40);
area.setEditable(false);
add("Center",area);
}
public void run(){Since we implement the Runnable interface, we must define a method called run (just like when we extended thread in the server), This is why we can use it as a thread. In the try clause we go into an infinite loop. In the next while loop we read from the server, if there aint anything to read, the read method will block the thread. This is something we want to do, because then an exception will be thrown if the connection is closed from the server, and this gives us a way to get out of the loop. When we finally recieves the newline character (this has nothing to do with the finally statement at the end), we append the line to the textarea followed by a newline character, and then we make the String ready to recieve a new line from the server.String temp="";
try{
while(true){
int tmp=0;
while((tmp=in.read())!=(int)'\n'){
temp+=String.valueOf((char)tmp);
}
area.append(temp+"\n");
temp="";
}//end while
}
catch(IOException ex){}
finally{try{
area.append("<< Connection closed by server >>");
in.close();
out.close();
sock.close();
}
catch(IOException e){}}
}
public void actionPerformed(ActionEvent ae){This method must be implemented by all ActionListeners. The only way to enter this method is to hit return while in the textfield, so we just call the send method with the contents of the textfield as the argument (The getText() method from TextField class returns the string content of the textfield). Then we want to remove the text from the textfield (we don't need it anymore), so we set the text in the field to the empty string.send(txt.getText()+"\n");
txt.setText("");
}
public void send(String line){All we do here is to send the text to the server. We run through the for loop, and write the int representation of every character including the newline character we included to the string when we called this method.try{
int x=line.length();
for(int i=0;i<x;i++)
out.write((int)line.charAt(i));
}
catch(IOException e){}
}
public static void usage(){This is just a method to write the usage of this program, in case the user tried to start it the wrong way, then it cals exit, to stop execution.System.out.println("\nWrong number of arguments");
System.out.print("Usage:\n\t");
System.out.print("java Client [host] [port]");
System.out.flush();
System.exit(0);
}
public static void main(String args[]){If the user has entered any other than two arguments at the command line, we will have to call the usage method to inform the user how to start the program. The try clause initializes the port and host variables.int port=0;
String host="";
if(args.length!=2)
usage();
try{
port=Integer.parseInt(args[1]);
host=args[0];
}
catch(NumberFormatException e){usage();}Client c=new Client(port,host);
area.setText("Welcome to the chat client\n");
c.pack();
c.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});
c.setVisible(true);
Thread t=new Thread(c);
t.start();
}
}
Remember to visit the sponsors on the bottom of my homepage