1 ImportJava.io.*;2 Importjava.awt.*;3 Importjava.awt.event.*;4 Importjavax.swing.*;5 Importjava.net.*;6 ImportJava.util.*;7 8 classConnextendsthread{9 PrivateJTextArea txt;Ten PrivateSocket St; One PrivateString msg =NULL; A PrivateBufferedReader br =NULL; - PrivatePrintStream PS; - PublicConn (Socket st,jtextarea txt) { the This. St =St; - This. txt =txt; - start (); - } + Public voidrun () { - Try{ +BR =NewBufferedReader (NewInputStreamReader (St.getinputstream ())); APS =NewPrintStream (NewDataOutputStream (St.getoutputstream ())); at}Catch(Exception e) { -SYSTEM.ERR.PRINTLN ("Input Failed"); - } - while(true){ - Try{ -msg = Br.readline ();//Blocking Methods inTxt.append ("received message from client:" +msg+ ' \ n ')); -Txt.append ("Message acceptance time is:" +NewDate () + "\ n"); to //Create a read file thread and send the read content to the server +Filethread ft =NewFilethread (); -Thread T =NewThread (ft); the T.start (); * while(Ft.flag); $ System.out.println (ft.readdata);Panax Notoginseng server.send (ft.readdata); - the}Catch(Exception e) { +SYSTEM.ERR.PRINTLN ("Connection closed"); A Break; the } + } - } $ Public voidSend (String msg) { $ ps.println (msg); - } - the } - Wuyi the classFilethreadImplementsrunnable{ - PublicString readdata = ""; Wu Public Static BooleanFlag =true; - Public voidrun () { AboutReadData = Readfilebylines ("D:\\hello.txt"); $Flag =false; - System.out.println (readdata); - } - A /** + * Read files in behavioral units, often used to read line-oriented format files the */ - Publicstring Readfilebylines (String fileName) { $File File =NewFile (fileName); theBufferedReader reader =NULL; theString tempstring =NULL; theString readdata = ""; the Try { -System.out.println ("read the contents of the file in the behavior unit, read a whole line at a time:"); inReader =NewBufferedReader (Newfilereader (file)); the intline = 1; the About //read one line at a time until NULL is read to the end of the file the while((tempstring = Reader.readline ())! =NULL) { the //Show Line Numbers the //System.out.println ("line" + Line + ":" + tempstring); + //line++; -ReadData = Readdata.concat (tempstring);//string concatenation recommendation function the }Bayi reader.close (); the}Catch(IOException e) { the e.printstacktrace (); -}finally { - if(Reader! =NULL) { the Try { the reader.close (); the}Catch(IOException E1) { the } - } the } the the returnReadData;94 } the the } the 98 About Public classServerextendsjframe{ - PrivateJTextArea txt;101 PrivateServerSocket SS;102 PublicString tempstring =NULL;103 Private StaticJava.util.list<conn> Conns =NewArraylist<conn>();104 PublicServer () { theTXT =NewJTextArea ();106 This. Settitle ("Server");107 This. setlayout (NewBorderLayout ());108 This. Add (NewJScrollPane (TXT), borderlayout.center);109 This. SetSize (500,300); the This. setvisible (true);111 the run ();113 } the Public voidrun () { the Try{ theSS =NewServerSocket (3000);117}Catch(Exception e) {118SYSTEM.ERR.PRINTLN ("Open socket failed");119 } -Txt.append ("The server has started! "+" \ n ");121 while(true){122 Try{123Socket st=ss.accept ();124Conns.add (NewConn (St,txt)); the }126 Catch(IOException ex) {127 System.err.println (ex); - }129 } the }131 Public Static voidSend (String msg) { the for(Conn C:conns)133 c.send (msg);134 }135 136 Public Static voidMain (String args[]) {137 138Server myserver=NewServer (); 139 } $ 141}
Another way to do this:
The Seventh Java Authoring Service program for Android network programming