The implementation is relatively simple, basically with serversocket and socket on the line,
Another thread to listen to, basically there's no problem.
Code:
The thread responsible for listening:
1 PackageNetwork;2 3 Importjava.io.IOException;4 ImportJava.io.PrintStream;5 Importjava.net.*;6 ImportJava.util.Scanner;7 8 Public classListenerthreadextendsThread {9 Ten PrivateSocket C; One PrivateScanner in; A - PublicListenerthread () { -c =NULL; thein =NULL; - } - - PublicListenerthread (Socket c) { + This. C =C; - Try { +in =NewScanner (C.getinputstream ()); A}Catch(IOException e) { atSystem.out.println ("Failed to start"); -System.exit (0); - } - } - - @Override in Public voidrun () { -@SuppressWarnings ("Unused") toPrintStream fs =NULL; + Try { -FS =NewPrintStream (C.getoutputstream ()); the}Catch(IOException e) { * e.printstacktrace (); $System.exit (0);Panax Notoginseng } - while(In.hasnextline ()) { theSystem.out.println (C.getlocaladdress (). toString () + ":" +in.nextline ()); + } A } the + Public voidSetsocket (Socket c) { This. C =C; } - PublicSocket Getsocket () {return This. C; } $ $}
Client:
1 PackageNetwork;2 3 Importjava.net.*;4 ImportJava.util.*;5 ImportJava.io.*;6 7 Public classB {8 9 PublicListenerthread lt;Ten PublicSocket server; One Public Static intPort = 8001; A Public StaticString ip = "localhost"; - - PublicB () { the Try { -Server =NewSocket (IP, port); -}Catch(IOException e) { -System.out.println ("program failed to start \ n"); +System.exit (0); - } + A@SuppressWarnings ("Resource") atScanner in =NewScanner (system.in); -String s =NULL; - Try { - -PrintStream PS =NewPrintStream (Server.getoutputstream ()); -lt =Newlistenerthread (server); in - Lt.start (); to + Do{ - if(s = =NULL)Continue; the Ps.println (s); *} while(! (s = in.nextline ()). Equals ("Exit")); $ Panax Notoginseng}Catch(IOException e) { - e.printstacktrace (); the } + A Try { the server.close (); +}Catch(IOException e) { -SYSTEM.OUT.PRINTLN ("program unexpected \ n"); $ } $ - } - the Public Static voidMain (string[] args) { - Newb ();Wuyi } the -}
Service side:
1 PackageNetwork;2 3 Importjava.net.*;4 ImportJava.util.*;5 ImportJava.io.*;6 7 Public classa {8 9 Listenerthread lt;Ten serversocket server; One Public Static intPort = 8001; A - PublicA () { - Try { theServer =NewServerSocket (port); -}Catch(IOException e) { -System.out.println ("program failed to start \ n"); -System.exit (0); + } - +@SuppressWarnings ("Resource") AScanner in =NewScanner (system.in); atString s =NULL; - Do { - - Try { -Socket C =server.accept (); - inPrintStream PS =NewPrintStream (C.getoutputstream ()); -lt =NewListenerthread (c); to + Lt.start (); - the Do{ * if(s = =NULL)Continue; $ Ps.println (s);Panax Notoginseng} while(! (s = in.nextline ()). Equals ("Exit")); - the}Catch(IOException e) { + e.printstacktrace (); A } the +} while(! (s = in.nextline ()). Equals ("Close")); - $ Try { $ server.close (); -}Catch(IOException e) { -SYSTEM.OUT.PRINTLN ("program unexpected \ n"); the } - Wuyi } the - Public Static voidMain (string[] args) { Wu Newa (); - } About $}
Other than that:
Open the server first, connect it with the client (need to be in the same computer, if you want to cross the computer, you need to change the IP in the B.java)
[Java] Homemade command-line chat program