Wednesday-use Java to make easy communication software

Source: Internet
Author: User

Service side:

Package Com.chinasofti.corejava.ch22;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.outputstreamwriter;import Java.io.printwriter;import Java.net.serversocket;import Java.net.socket;import Java.util.scanner;public class Server {private static BufferedReader br=null;private static printwriter pw=null;private static serversocket ss;private static Socket s;static s Canner scanner=new Scanner (system.in);/** * @param args */public static void main (string[] args) {//TODO auto-generated m Ethod stubtry {ss=new serversocket (5500); SYSTEM.OUT.PRINTLN ("server starts normally ....")    ");    S=ss.accept ();//Blocking Method System.out.println ("Connection succeeded" +s.getremotesocketaddress ()); Br=new BufferedReader (New InputStreamReader (S.getinputstream ()));p w=new PrintWriter (New OutputStreamWriter ( S.getoutputstream ())); while (true) {String string=br.readline (); SYSTEM.OUT.PRINTLN ("server read:" +string); SYSTEM.OUT.PRINTLN ("Server Side Please enter:"); String str=scanner.next ();p w.println (str);p W.flush ();if (Str.equals ("Exit")) {break;}}} catch (Exception E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}  try {pw.close (); Br.close ();} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}}}
Port:
Package Com.chinasofti.corejava.ch22;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.outputstreamwriter;import Java.io.printwriter;import Java.net.inetaddress;import Java.net.socket;import Java.net.unknownhostexception;import Java.util.Scanner;public Class Client {private static printwriter pw=null;private static BufferedReader br=null;private static Socket s;static Scan NER scanner=new Scanner (system.in);/** * @param args */public static void main (string[] args) {//TODO auto-generated Meth OD stubtry {socket s=new socket (inetaddress.getlocalhost (), 5500);p w=new printwriter (New OutputStreamWriter ( S.getoutputstream ())); Br=new BufferedReader (New InputStreamReader (S.getinputstream ())); while (true) { SYSTEM.OUT.PRINTLN ("Client-side Please enter:"); String str = scanner.next ();p w.println (str);p W.flush (); String String=br.readline (); SYSTEM.OUT.PRINTLN ("client reads:" +string); if (Str.equals ("Exit")} {break;}} }catch (Exception e) {//TODO auto-generated catch BloCke.printstacktrace ();} try {br.close ();p w.close ();} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}}}

  

Wednesday-use Java to make easy communication software

Related Article

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.