Chatserver. Java
Import Java. io. datainputstream; import Java. io. eofexception; import Java. io. ioexception; import java.net. *; public class chatserver {Boolean started = false; serversocket Ss = NULL; public static void main (string [] ARGs) {New chatserver (). start ();} public void start () {try {// start the Service SS = new serversocket (8888); started = true;} catch (bindexception e) {system. out. println ("port occupied! "); System. Out. println (" please turn off the application and restart the server! "); System. exit (0); // end the application} catch (ioexception e) {e. printstacktrace (); system. exit (0); // end application} Try {// accept client connection while (started) {socket S = ss. accept (); client c = new client (s); system. out. println ("A Client Connected! "); // Place the debugging Statement on the rightmost side of New thread (c ). start (); // dis. close () ;}} catch (ioexception e) {e. printstacktrace ();} finally {try {ss. close ();} catch (ioexception e) {e. printstacktrace () ;}} class client implements runnable {private socket s; private datainputstream Dis = NULL; private Boolean bconnected = false; Public client (socket s) {This. S = s; try {Dis = new datainputstream (S. getinputstream (); bconnected = true;} Cat CH (ioexception e) {e. printstacktrace () ;}} public void run () {try {While (bconnected) {string STR = dis. readutf (); // blocking... system. out. println (STR) ;}} catch (eofexception e) {system. out. println ("client closed! ");} Catch (ioexception e) {e. printstacktrace ();} finally {try {If (DIS! = NULL) dis. Close (); If (s! = NULL) S. Close () ;}catch (ioexception El) {El. printstacktrace ();}}}}}
Chatclient. Java
Import Java. AWT. *; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. AWT. event. windowadapter; import Java. AWT. event. using wevent; import Java. io. ioexception; import java.net. *; import Java. io. *; public class chatclient extends frame {/*** version 0.7 */socket S = NULL; dataoutputstream dos = NULL; textfield tftxt = new textfield (); // There are three methods to respond to actionevent events when you press Enter: the class of the horizontal column, the anonymous class, and the internal class textar. EA tacontent = new textarea (); Public void launchframe () {setlocation (400,300); // set the location this. setsize (300,300); add (tftxt, borderlayout. south); add (tacontent, borderlayout. north); Pack (); this. addwindowlistener (New windowadapter () {// Anonymous class @ overridepublic void windowclosing (invalid wevent e) {// todo auto-generated method stubdisconnect (); system. exit (0); // pass 0 to exit normally}); tftxt. addactionlistener (New tflistener (); This. setvisible (true); Connect ();} private class tflistener implements actionlistener {@ overridepublic void actionreceivmed (actionevent e) {string STR = tftxt. gettext (). trim (); // remove the space tacontent on both sides of the TRIM () method in string. settext (STR); tftxt. settext (""); // send to the server try {dos. writeutf (STR); dos. flush (); // dos. close () ;}catch (ioexception E1) {e1.printstacktrace () ;}}// Method for connecting to the server public void connect () {try {S = new socke T ("127.0.0.1", 8888); // The output stream dos = new dataoutputstream (S. getoutputstream (); system. Out. println ("connected! ");} Catch (unknownhostexception e) {// host e cannot be found. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} public void disconnect () {try {dos. close (); S. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} public static void main (string [] ARGs) {// todo auto-generated method stubnew chatclient (). launchframe ();}}