Java socket programming classic instance

Source: Internet
Author: User

The server listens to and receives information from each client and then sends it to each client server [java] package com. java. xiong. net17; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; import java. io. printStream; import java.net. socket; public class RunableSocket implements Runnable {// define Socket private Socket socket = null for processing by the current thread; // private BufferedReader read = null for the input stream used by this thread; public RunableSocket (Socket s) throws IOExc Eption {this. socket = s; read = new BufferedReader (new InputStreamReader (socket. getInputStream ();} @ Override public void run () {String line = null; try {while (line = getClentData ())! = Null) {for (Socket s: MyServer. list) {// output the information PrintStream print = new PrintStream (s. getOutputStream (); print. println (line) ;}} catch (IOException io) {io. printStackTrace () ;}// Method for reading client data public String getClentData () {String line = null; try {line = read. readLine ();} catch (IOException io) {MyServer. list. remove (socket) ;}return line ;}[ java] package com. java. xiong. net17; import java. io. IOExcep Tion; import java.net. serverSocket; import java.net. socket; import java. util. arrayList; import java. util. list; public class MyServer {public static List <Socket> list = new ArrayList <Socket> (); public static void main (String [] args) throws Exception {ServerSocket server = new ServerSocket (30001); while (true) {Socket scoket = server. accept (); list. add (scoket); new Thread (new RunableSocket (scoket )). start () ;}} Client [java] package com. java. xiong. net17; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; import java.net. socket; // print the data returned by the server out public class RubableClient implements Runnable {private Socket socket; private BufferedReader read; public RubableClient (Socket socket) throws IOException {this. socket = socket; this. read = new BufferedReader (new In PutStreamReader (this. socket. getInputStream () ;}@ Override public void run () {try {String line = null; while (line = read. readLine ())! = Null) {System. out. print (line) ;}} catch (IOException io) {io. printStackTrace () ;}} [java] package com. java. xiong. net17; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; import java. io. printStream; import java.net. socket; import java.net. unknownHostException; public class MyClient {public static void main (String [] args) throws Exception, IOException {// Send the request Socket socket = new Socket ("127.0.0.1", 30001) to the server; new Thread (new RubableClient (socket )). start (); // obtain the output stream PrintStream print = new PrintStream (socket. getOutputStream (); String line = ""; BufferedReader read = new BufferedReader (new InputStreamReader (System. in); while (line = read. readLine ())! = Null) {// print. println (line );}}}

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.