Thanks to the online great god to share the code, and then I according to the needs of our project, assembled into the rest of the code, basically the framework is the model, the need to modify is the delivery of business data, the other is not modified.
In the spirit of learning other people's code, learn to share the spirit, so I spent two days of research on the code to share (the time of the night). I hope the great God to teach me a lot of this programming rookie technology, this rookie grateful disrespect.
ServerSocket:
Import Java.io.bufferedreader;import Java.io.bufferedwriter;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.nio.buffer;public class Talkserver {public static void main ( String[] args) throws Ioexception{serversocket server = new ServerSocket (5203); while (true) {Socket socket = Server.accept (); Exsocketserver (socket);}} public static void Exsocketserver (final socket socket) {New Thread (new Runnable () {@Overridepublic void Run () {//TODO Auto -generated method Stubbufferedreader in = null; PrintWriter out = null; try {in = new BufferedReader (New InputStreamReader (Socket.getinputstream ())); Out=new PrintWriter ( Socket.getoutputstream ()); while (true) {String msg =in.readline (); if (msg = = null) {break;} SYSTEM.OUT.PRINTLN ("Client:" +msg);//Returns a message to the client. OUT.PRINTLN ("OK! "); Out.flush ();}} catch (Exception e) {//e.printstacktrace (); System.out.println ("Error:" +e);} Finally {try {in. Close (); Out.close (); Socket.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}). Start ();}}
Customer A-side code:
Import Java.io.*;import java.net.*;p ublic class Talkclient {public static void main (string args[]) {string ROOTPATH=TALKC Lient.class.getResource ("/"). GetFile (). toString (); String fileName =rootpath+ "Initialize.txt"; String file = "My name is yi! Nice to meet ";//readfromfile.readfilebylinesln (FileName); exsocket (file);} public static void Exsocket (String file) {try{inetaddress addr = inetaddress.getbyname ("localhost"); Socket socket=new socket (addr,5203); BufferedReader in=new BufferedReader (New InputStreamReader (Socket.getinputstream ())); PrintWriter out=new PrintWriter (Socket.getoutputstream ()); for (int i =0; i<1000000;i++) {String readline= file+i+ AAAA ";; Sin.readline (); Reads a string from the system standard input//outputs the string read from the system standard input to SERVEROUT.PRINTLN (readline);//refreshes the output stream so that the server receives the string Out.flush () immediately; System.out.println ("Client:" +readline); System.out.println ("Servlet:" +in.readline ());} Socket.close (); Close Socket}catch (Exception e) {System.out.println ("error" +e);//error, print error message}}}
Customer B-side code:
Import Java.io.*;import java.net.*;p ublic class Talkclient {public static void main (string args[]) {string ROOTPATH=TALKC Lient.class.getResource ("/"). GetFile (). toString (); String fileName =rootpath+ "Initialize.txt"; String file = "My name is yi! Nice to meet ";//readfromfile.readfilebylinesln (FileName); exsocket (file);} public static void Exsocket (String file) {try{inetaddress addr = inetaddress.getbyname ("localhost"); Socket socket=new socket (addr,5203); BufferedReader in=new BufferedReader (New InputStreamReader (Socket.getinputstream ())); PrintWriter out=new PrintWriter (Socket.getoutputstream ()); for (int i =0; i<1000000;i++) {String readline= file+i+ BBBB ";; Sin.readline (); Reads a string from the system standard input//outputs the string read from the system standard input to SERVEROUT.PRINTLN (readline);//refreshes the output stream so that the server receives the string Out.flush () immediately; System.out.println ("Client:" +readline); System.out.println ("Servlet:" +in.readline ());} Socket.close (); Close Socket}catch (Exception e) {System.out.println ("error" +e);//error, print error message}}}
Java Socket multithreaded programming, processing millions data concurrency.