Client Program:
Import java. AWT. borderlayout;
Import java. AWT. container;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;
Import java. Io. bufferedinputstream;
Import java. Io. bufferedoutputstream;
Import java. Io. bufferedreader;
Import java. Io. datainputstream;
Import java. Io. dataoutputstream;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. objectinputstream;
Import java. Io. objectoutputstream;
Import java. Io. outputstream;
Import java. Io. printwriter;
Import java.net .*;
Import java. util. Concurrent .*;
Import javax. Swing. box;
Import javax. Swing. jbutton;
Import javax. Swing. jfilechooser;
Import javax. Swing. jframe;
Import javax. Swing. jpanel;
Import javax. Swing. jscrollpane;
Import javax. Swing. jtextarea;
Import javax. Swing. jtextfield;
Public class Client extends jframe {
Private objectinputstream m_input;
Private objectoutputstream m_output;
Private jtextfield m_enter;
Private jtextarea m_display;
Private int m_clientnumber = 0;
Public client () throws ioexception {
Super ("chat client ");
Container c = getcontentpane ();
M_enter = new jtextfield ();
M_enter.setenabled (false );
M_enter.addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent event ){
Try {
String S = event. getactioncommand ();
Desencrypt des = new desencrypt (); // instantiate an object
Des. getkey ("aadd"); // generate a key
String strenc = des. getencstring (s); // encrypted string, returns the ciphertext of string
M_output.writeobject (strenc );
M_output.flush ();
Mb_displayappend ("client" + S );
M_enter.settext ("");
} Catch (exception e ){
System. Err. println ("exception:" + E );
E. printstacktrace ();
}
}
});
C. Add (m_enter, borderlayout. North );
M_display = new jtextarea ();
C. Add (New jscrollpane (m_display), borderlayout. center );
}
Public void mb_displayappend (string s ){
M_display.append (S + "/N ");
M_display.setcaretposition (m_display.gettext (). Length ());
M_enter.requestfocusinwindow ();
}
Public Boolean mb_isendsession (string m ){
If (M. equalsignorecase ("Q "))
Return (true );
If (M. equalsignorecase ("quit "))
Return (true );
If (M. equalsignorecase ("exit "))
Return (true );
If (M. equalsignorecase ("end "))
Return (true );
If (M. signorecase ("end "))
Return (true );
Return (false );
}
Public void mb_run (string host, int port ){
Try {
Mb_displayappend ("try to connect ");
Socket S = new socket (host, Port );
String m;
String strdes;
String senq;
M_output = new objectoutputstream (S. getoutputstream ());
M_input = new objectinputstream (S. getinputstream ());
M_enter.setenabled (true );
M = (string) m_input.readobject ();
Mb_displayappend (m );
Do {
M = (string) m_input.readobject ();
Desencrypt des = new desencrypt (); // instantiate an object
Des. getkey ("aadd"); // generate a key
Strdes = des. getdesstring (m); // decrypt the string-type ciphertext
Senq = des. getencstring ("Q ");
Mb_displayappend ("the message sent by the server before decryption is" + M );
Mb_displayappend ("server sending message:" + strdes );
} While (! Mb_isendsession (strdes ));
M_output.writeobject (senq );
M_output.flush ();
M_output.close ();
System. Exit (0 );
} Catch (exception e ){
System. Err. println ("exception:" + E );
E. printstacktrace ();
Mb_displayappend ("connection [" + m_clientnumber + "] exception ");
}
}
Public static void main (string [] ARGs) throws ioexception {
Client APP = new client ();
App. setdefaclocloseoperation (jframe. exit_on_close );
App. setsize (500,400 );
App. setvisible (true );
If (ARGs. Length = 0)
App. mb_run ("localhost", 5000 );
Else
App. mb_run (ARGs [0], 5000 );
}
}