// Java Web chat server <br/> Import Java. io. *; <br/> Import Java. AWT. *; <br/> Import java.net. *; <br/> Import Java. util. *; <br/> Import Java. AWT. event. *; <br/> public class serverframe extends frame implements actionlistener {<br/> int screenwidth, screenheight; <br/> button closeserver; <br/> textarea serverstatus; <br/> serverthread sthread; <br/> Public serverframe () {<br/> super ("game server"); <br/> serverstatus = New textarea (); <br/> closeserver = new button ("Disable server"); <br/> serverstatus. seteditable (false); <br/> closeserver. addactionlistener (this); <br/> Add (serverstatus, borderlayout. center); <br/> Add (closeserver, borderlayout. south); <br/> screenwidth = gettoolkit (). getscreensize (). width; <br/> screenheight = gettoolkit (). getscreensize (). height; <br/> setsize (screenwidth/2, screenheight/2); <br/> setloca Tion (screenwidth-getwidth ()/2, <br/> (screenheight-getheight ()/2); <br/> setresizable (false ); <br/> setvisible (true); <br/> sthread = new serverthread (this); <br/> sthread. start (); <br/>}< br/> Public static void main (string [] ARGs) {<br/> New serverframe (); <br/>}< br/> Public void actionreceivmed (actionevent e) {<br/> dispose (); <br/> system. exit (0); <br/>}< br/> class serverthre Ad extends thread {<br/> int sport; <br/> serversocket ssocket; <br/> serverframe; <br/> textarea sStatus; <br/> vector clients; <br/> vector messages; <br/> broadcast broadcastwriter; <br/> cleandeadconnect cleaner; <br/> Public serverthread (serverframe F) {<br/> clients = new vector (); <br/> messages = new vector (); <br/> try {<br/> sport = 6633; <br/> ssocket = new serversocket (sport); <br />} Catch (ioexception e) {<br/> system. out. println ("exception:" + E); <br/> system. out. println ("the server interface cannot be created !! "); <Br/> system. exit (1); <br/>}< br/> This. frame = f; <br/> sStatus = frame. serverstatus; <br/> sStatus. append ("the game server has been started .. /n "); <br/> sStatus. append (">>>> wait online </n"); <br/> broadcastwriter = new broadcast (this); <br/> broadcastwriter. start (); <br/> cleaner = new cleandeadconnect (this); <br/> cleaner. start (); <br/>}< br/> Public void run () {<br/> while (true) {<br/> try {<br/> socket csocke T = ssocket. accept (); <br/> clientthread cthread = new clientthread (csocket, this); <br/> cthread. start (); <br/> sStatus. append (cthread. getclientname () + ":"); <br/> sStatus. append (cthread. getclientaddress () + "online"); <br/> sStatus. append ("Current online quantity" + clientthread. connectnumber + "/N"); <br/> synchronized (clients) {<br/> clients. addelement (cthread); <br/>}< br/>} catch (ioexception e) {<br/> system. out. Println ("exception:" + E); <br/> system. Out. println ("An error occurred while establishing the client! "); <Br/> system. exit (2); <br/>}< br/> Public void finalize () {<br/> try {<br/> ssocket. close (); <br/>}catch (ioexception e) {}< br/> ssocket = NULL; <br/>}< br/> class clientthread extends thread {<br/> socket csocket; <br/> printstream write; <br/> bufferedreader read; <br/> serverthread sthread; <br/> textarea sStatus; <br/> string m; <br/> Public static int connectnumber = 0; <br/> Public clientthread (socket csocket, serverthread sthread) {<br/> This. csocket = csocket; <br/> This. sthread = sthread; <br/> sStatus = sthread. frame. serverstatus; <br/> connectnumber ++; <br/> try {<br/> write = new printstream (csocket. getoutputstream (); <br/> Read = new bufferedreader (<br/> New inputstreamreader (csocket. getinputstream (); <br/>}catch (ioexception e) {<br/> system. out. Println ("exception:" + E); <br/> system. Out. println ("I/O channel creation failed! "); <Br/> system. exit (3); <br/>}< br/> M = "new partner <" + getclientname () + "> join !!! "; <Br/> M = m +" chat count: "+ connectnumber +"/n "; <br/> sthread. messages. addelement (m); <br/>}< br/> Public void run () {<br/> while (true) {<br/> try {<br/> string message = read. readline (); <br/> synchronized (sthread. messages) {<br/> If (message! = NULL) {<br/> If (message. equals ("bye") {<br/> connectnumber --; <br/> message = getclientname () + ":" + <br/> getclientaddress () + "Left"; <br/> sStatus. append (Message + ""); <br/> sStatus. append ("current connections:" + <br/> connectnumber + "/N"); <br/> sthread. messages. addelement (Message); <br/> synchronized (sthread. clients) {<br/> sthread. clients. removeelement (this); <br/>}< br/> break; <br/>}else {<br/> Message = "from" + getclientname () + <br/> ":" + getclientaddress () + <br/> "/N =>" + message + "/N "; <br/> sthread. messages. addelement (Message); <br/>}< br/>}catch (ioexception e) {<br/> break; <br/>}< br/> Public String getclientname () {<br/> return csocket. getinetaddress (). gethostname (); <br/>}< br/> Public String getclientaddress () {<br/> return csocket. getineta Ddress (). gethostaddress (); <br/>}< br/> Public void finalize () {<br/> try {<br/> Read. close (); <br/> write. close (); <br/> csocket. close (); <br/>}catch (ioexception e) {<br/>}< br/> csocket = NULL; <br/>}< br/> class broadcast extends thread {<br/> clientthread C; <br/> serverthread sthread; <br/> string m; <br/> Public Broadcast (serverthread sthread) {<br/> This. sthread = sthread; <br/>} <Br/> Public void run () {<br/> while (true) {<br/> try {<br/> thread. sleep (3000); <br/>}catch (interruptedexception e) {}< br/> synchronized (sthread. messages) {<br/> If (sthread. messages. isempty () <br/> continue; <br/> M = (string) sthread. messages. firstelement (); <br/> sthread. messages. removeelement (m); <br/>}< br/> synchronized (sthread. clients) {<br/> for (INT I = 0; I <sthread. clients. size (); I ++) {<br /> C = (clientthread) sthread. clients. elementat (I); <br/> C. write. println (m ); <br/>}< br/> class cleandeadconnect extends thread {<br/> serverthread sthread; <br/> clientthread temp; <br/> string m; <br/> Public cleandeadconnect (serverthread sthread) {<br/> This. sthread = sthread; <br/>}< br/> Public void run () {<br/> while (true) {<br/> try {<br/> thread. sleep (10000 ); <Br/>}catch (interruptedexception e) {}< br/> synchronized (sthread. clients) {<br/> for (INT I = 0; I <sthread. clients. size (); I ++) {<br/> temp = (clientthread) sthread. clients. elementat (I); <br/> If (! Temp. isalive () {<br/> clientthread. connectnumber --; <br/> M = temp. getclientname () + <br/> ":" + temp. getclientaddress (); <br/> M = m + "number of online connections that have left:" + <br/> clientthread. connectnumber + "/N"; <br/> sthread. frame. serverstatus. append (m); <br/> synchronized (sthread. messages) {<br/> sthread. messages. addelement (m); <br/>}< br/> sthread. clients. removeelementat (I); <br/>}< br/>}