A chat room in the problems encountered!

Source: Internet
Author: User
Tags connect readline socket thread client
Chat Room | Problem This chat room is written in Java, part of the source code is such, but debugging can not run, but also please heroes pointing: import Java.awt.event.*;import java.net.*;import java.io.*; Import Java.util.*;import Javax.swing.timer;public class AppServer implements Runnable{serversocket server; Socket fromclient; Thread serverthread;public AppServer () {System.out.println ("Funchat server start ..."); Try{server=new ServerSocket (1001); Serverthread=new Thread (this); Serverthread.start ();} catch (Exception e) {System.out.println ("Cannot Start thread:" +e);} End of appserverpublic static void Main (String args[]) {new AppServer ();} public void Run () {Try{while (true) {fromclient=server.accept (); Connect con=new Connect (fromclient);} catch (Exception e) {System.out.println ("Cannot listen to the client" +e);} End of Catch}//end of Run}//end of Appserverclass Connect{objectoutputstream streamtoclient;int ctr=0; BufferedReader streamfromclient;static vector vector;static vector vctrlist; String Message= ""; static string Str=new string ("Usrlist"); static{vector=new vector (1,1); vctrList=new Vector (1,1); Vctrlist.addelement ((String) str); int verify (String mesg) {Try{randomaccessfield RAS = new Randomaccessfile ("UsrPwd.txt", "R"); int i = 0; Sting str = "" while (Ras.getfilepointer ())!= (Ras.length ()) {str = Ras.readline (); if (Str.equals (MESG)) {ctr=1;break;}} Ras.close ();} catch (Exception e) {System.out.print ("Exception occurred:" +e);} Return CTR;} int checkfile (String mesg) {int chk =1;try{randomaccessfile RS = new Randomaccessfile ("UsrPwd.txt", "R"); int i = 0; String str = ""; String colon = new String (":"); int index = ((String) MESG). LastIndexOf (colon); String userName = (string) mesg.substring (0,index); while ((Rs.getfukeoiubter ())!= (int) (Rs.length ())) {str = Rs.readline (); int index1 = ((String) str). LastIndexOf (colon); String usrname = (string) str.substring (0,INDEX1); if (Usrname.equals (UserName)) {chk = 0;break;}}} catch (Exception e) {System.out.print ("Exception occurred:" +e);} return chk; }public Connect (Socket infromclient) {String msg = ""; String MESG = ""; try{streamfromclient = new BufferedReader (New InputStreamReader (Infromclient.getinputstream ())); streamtoclient = new ObjectOutputStream ( Infromclient.getoutputstream ()); msg = Streamfromclient.rreadline (); if ((Msg.equals ("from Timer")) { StreamToClient.writeObject.writeObject (vector); Streamtoclient.writeobject (vctrlist);} else if (msg.equals ("Logininfo")) {msg = Streamfromclient.readline (); int ver = verify (msg); if (ver==1) {String colon = new String (":"); int index = ((String) msg). LastIndexOf (colon); String userName = (string) msg.substring (0,index); if (!) ( Vctrlist.indexof ((String) userName) >0)) {streamtoclient.writeobject ("Welcome"); Vctrlist.addelement ((String) UserName);} Else{streamtoclient.writeobject ("Ligin denied");} else if (msg.equals ("Registerinfo")) {msg = Streamfromclient.readline (); int ret = Checkfile (msg); if (ret==0) Streamtoclient.writeobject ("User Exists"); if (ret==1) {FileOutputStream out = new FileOutputStream ("UsrPwd.txt", True) ; PrintStream p = new PrintStream (out);p. println ();p. println (msg);p. Close (); StreamtoclieNt.writeobject ("registered");} else if (msg.equals ("User Logout")); {String Remuser = Streamfromclient.readline (); Boolean B = Vctrlist.removeelement ((String) remuser); Else{message = Message+msg;vector.addelement ((String) message); Streamtoclient.writeobject (vector);}} catch (Exception e) {System.out.println ("Cannot get the client stream connect" + e);} Finally{try{infromclient.close ();} catch (IOException e) {System.out.println ("Exception occurred: +e);}}}"

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: info-contact@alibabacloud.com 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.