Java Learning Notes _ Network + multithreading

Source: Internet
Author: User

Supports both client and server side of simultaneous transceiver

Client

1 Importjavax.swing.*;2 Importjava.awt.*;3 ImportJava.io.*;4 Importjava.net.*;5 Importjava.awt.event.*;6 7  Public classChatClient {8     PrivateJTextArea incoming;9     PrivateJTextField outgoing;Ten     Privateprintwriter writer; One     PrivateBufferedReader Reader; A          -      Public Static voidMain (string[] args) { -ChatClient client =Newchatclient (); the client.go (); -     } -      -     Private voidGo () { + Build_gui (); - setup_net (); +     } A      at     Private voidBuild_gui () { -JFrame frame =NewJFrame ("Chat Client")); -JPanel Panel =NewJPanel (); -          -incoming =NewJTextArea (15, 50); -Incoming.setlinewrap (true); inIncoming.setwrapstyleword (true); -Incoming.seteditable (false); toJScrollPane Qscroller =NewJScrollPane (incoming); + Qscroller.setverticalscrollbarpolicy (scrollpaneconstants.vertical_scrollbar_always); - Qscroller.sethorizontalscrollbarpolicy (scrollpaneconstants.horizontal_scrollbar_never); the Panel.add (incoming); *          $Outgoing =NewJTextField (20);Panax Notoginseng Panel.add (outgoing); -          theJButton button =NewJButton ("Send"); +Button.addactionlistener (NewSendbuttonlistener ()); A panel.add (button); the          + frame.add (Borderlayout.center, panel); - frame.setdefaultcloseoperation (jframe.exit_on_close); $Frame.setsize (600, 400);  $Frame.setvisible (true); -     } -      the      Public classSendbuttonlistenerImplementsActionListener { -          Public voidactionperformed (ActionEvent a) {WuyiString msg =Outgoing.gettext (); the             Try { - writer.println (msg); Wu Writer.flush (); -}Catch(Exception ex) {ex.printstacktrace ();} AboutOutgoing.settext (""); $ Outgoing.requestfocus (); -         } -     } -      A     classReceivethreadjobImplementsRunnable { +          Public voidrun () { the String msg; -             Try { $                  while(msg = Reader.readline ())! =NULL ) { theIncoming.append (msg + "\ n")); the                 } the}Catch(IOException ex) {System.out.println ("server Disconnected"); }         the         } -     } in      the     Private voidsetup_net () { the         Try { AboutSocket s =NewSocket ("127.0.0.1", 4242); thewriter =NewPrintWriter (S.getoutputstream ()); theInputStreamReader StreamReader =NewInputStreamReader (S.getinputstream ()); theReader =NewBufferedReader (StreamReader);  +  -Runnable Receive_thread_job =Newreceivethreadjob (); theThread Receive_thread =NewThread (receive_thread_job);Bayi Receive_thread.start (); the              the}Catch(IOException ex) { - ex.printstacktrace (); -         } the     } the}

Server-side

1 ImportJava.io.*;2 Importjava.net.*;3 Importjava.util.ArrayList;4 5  Public classChatserver {6 BufferedReader Reader;7Arraylist<printwriter> Writer_array =NewArraylist<printwriter>();8 9      Public Static voidMain (string[] args) {TenChatserver Server =Newchatserver (); One server.go (); A     } -      -     Private voidGo () { the setup_net (); -     } -      -     Private voidsetup_net () { +         Try { -ServerSocket Serversock =NewServerSocket (4242); +              while(true) { ASocket s =serversock.accept (); atSYSTEM.OUT.PRINTLN ("Client Connected"); -InputStreamReader StreamReader =NewInputStreamReader (S.getinputstream ()); -Reader =NewBufferedReader (StreamReader);  -PrintWriter writer =NewPrintWriter (S.getoutputstream ()); - Writer_array.add (writer);  -Runnable Server_proc_thread_job =Newserverprocthreadjob (); inThread Server_proc =NewThread (server_proc_thread_job); - Server_proc.start (); to             } +}Catch(IOException ex) { - ex.printstacktrace (); the         } *     } $     Panax Notoginseng     classServerprocthreadjobImplementsRunnable { -          Public voidrun () { the String msg; +             Try { A                  while(msg = Reader.readline ())! =NULL ) { the System.out.println (msg); +                      for(PrintWriter writer:writer_array) { - writer.println (msg); $ Writer.flush (); $                     } -                 } -}Catch(IOException ex) {System.out.println ("Client Disconnected"); } the         } -     }Wuyi}

Java Learning Notes _ Network + multithreading

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.