The Seventh Java Authoring Service program for Android network programming

Source: Internet
Author: User

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

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.