Simple Chat System chat1.0

Source: Internet
Author: User

Chatserver. Java

Import Java. io. datainputstream; import Java. io. eofexception; import Java. io. ioexception; import *; 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 *; 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 ("", 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 ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.