Java Socket multi-thread programming, processing millions of data concurrency.
Thanks to the Code shared by the experts on the Internet, I assembled the code into the rest based on the needs of our project. The general framework is this mode. What needs to be modified is the transmission of business data, others do not need to be modified.
In the spirit of learning others' code and sharing others, I spent two days studying the code (evening time ). I hope you will give me a lot of advice on this programming cainiao technology. I am very grateful and disrespectful to this cainiao.
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) {Socke T 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. read Line (); if (msg = null) {break;} System. out. println ("Client:" + msg); // return the 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 ();}}
Client A code:
Import java. io. *; import java.net. *; public class TalkClient {public static void main (String args []) {String rootPath = TalkClient. class. getResource ("/"). getFile (). toString (); String fileName = rootPath + "Initialize.txt"; String file = "my name is YI! Nice to meet you "; // 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 (); // read a string from the system standard input // output the string read from the system standard input to Serverout. println (readline); // refresh the output stream so that the Server immediately receives the string out. flush (); System. out. println ("Client:" + readline); System. out. println ("Servlet:" + in. readLine ();} socket. close (); // close Socket} catch (Exception e) {System. out. println ("Error" + e); // print the Error message when an Error occurs }}}
Client B code:
Import java. io. *; import java.net. *; public class TalkClient {public static void main (String args []) {String rootPath = TalkClient. class. getResource ("/"). getFile (). toString (); String fileName = rootPath + "Initialize.txt"; String file = "my name is YI! Nice to meet you "; // 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 (); // read a string from the system standard input // output the string read from the system standard input to Serverout. println (readline); // refresh the output stream so that the Server immediately receives the string out. flush (); System. out. println ("Client:" + readline); System. out. println ("Servlet:" + in. readLine ();} socket. close (); // close Socket} catch (Exception e) {System. out. println ("Error" + e); // print the Error message when an Error occurs }}}