|
Import java. awt .*;
Import java. awt. event .*;
Import java. io .*;
Import java. lang .*;
Import java.net .*;
Public class HeartClient extends Frame {
/*
* Member method appearance...
*/
Private TextField tfText;
Private TextArea taContent;
Private Socket s;
Private DataOutputStream dos;
Private DataInputStream dis;
/**
* Note: The entry... ^
* @ Param args
*/
Public static void main (String [] args ){
New HeartClient (). launchFrame ();
}
/**
* Loading GU
*/
Public void launchFrame (){
TfText = new TextField ();
TaContent = new TextArea ();
This. setSize (300,300 );
This. setLocation (300,300 );
This. tfText. addActionListener (new TFListener ());
This. add (tfText, BorderLayout. SOUTH );
This. add (taContent, BorderLayout. NORTH );
This. addWindowListener (new WindowAdapter (){
@ Override
Public void windowClosing (WindowEvent e ){
System. exit (0 );
}});
This. pack ();
This. connect ();
This. setVisible (true );
}
/**
* I am trying to connect to the server...
*/
Public void connect (){
Try {
S = new Socket ("127.0.0.1", 1720 );
Dos = new DataOutputStream (s. getOutputStream ());
Dis = new DataInputStream (s. getInputStream ());
New Thread (new SendThread (). start ();
// Dos. writeUTF ("Hello, I find u! ");
} Catch (UnknownHostException e ){
System. out. println ("UnknownHostException ");
E. printStackTrace ();
} Catch (IOException e ){
System. out. println ("IOException ");
E. printStackTrace ();
} Finally {
// Disable Donnie...
}
}
/**
* Don't wait for tfText (the listener class of TextField tfText)
*/
Class TFListener implements ActionListener {
Private String str;
@ Override
Public void actionreceivmed (ActionEvent e ){
Str = tfText. getText (). trim ();
TfText. setText ("");
Try {
Dos. writeUTF (str );
} Catch (IOException e1 ){
System. out. println ("IOException ");
E1.printStackTrace ();
}
}
}
/**
* The thread in which the client receives messages...
*
*/
Class SendThread implements Runnable {
Private String str;
Private boolean iConnect = false;
Public void run (){
IConnect = true;
RecMsg ();
}
/**
* Message, look at moves, where to run... (implementation of the client to receive messages)
* @ Throws IOException
*/
Public void recMsg (){
Try {
While (iConnect ){
Str = dis. readUTF ();
TaContent. setText (str );
}
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
} |