JAVA Socket network programming, the server receives the implementation of multiple client connections

Source: Internet
Author: User

This is based on a simple extension of the previous article, and of course this is not the best implementation


Server if you want to receive multiple client connections, you must loop to receive the new client connection request, and a full client server socket connection is maintained through a thread.

Package Com.tree.demo.socket;import Java.io.ioexception;import Java.io.inputstream;import java.io.OutputStream; Import Java.io.printwriter;import java.net.serversocket;import Java.net.socket;import Java.util.Scanner;class Threadserversocket implements Runnable {private socket server;public threadserversocket (Socket i) {//TODO auto-generated constructor stubserver = i;} @Overridepublic void Run () {//TODO auto-generated method Stubtry {InputStream in = Server.getinputstream (); OutputStream o UT = Server.getoutputstream (); Scanner sc = new Scanner (in); PrintWriter pw = new PrintWriter (out,true/*autoflush*/);p w.println ("Hello! Enter BYE to Exit. "); while (Sc.hasnextline ()) {String line = Sc.nextline ();p w.println ("Echo:" +line), if (Line.trim (). Equals ("BYE")) {break;}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally {try {server.close ();} catch (Ioexc Eption e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} public class Mulserversocketdemo {private static final int PORT = 1234;//private static final int buffer_size = 1024;public static void Main (string[] args) throws IOException {//TODO auto-generated method Stubint counter = 1; ServerSocket ss = new ServerSocket (PORT), while (true) {Socket s = ss.accept (); System.out.println ("First" + (counter++) + "Connection"); Thread t = new Thread (new Threadserversocket (s)); T.start ();}}}
After starting the above program, use the command line, telnet command to test the





At the same time, 6 connections are made, and of course it can be implemented via Java:

Service side:

Package Com.tree.demo.socket;import Java.io.ioexception;import Java.io.inputstream;import java.io.OutputStream; Import Java.io.printwriter;import java.net.serversocket;import Java.net.socket;import Java.util.Scanner;class Threadserversocket implements Runnable {private Socket server;private int buffer_size = 1024;public Threadserversocket ( Socket i) {//TODO auto-generated constructor stubserver = i;} @Overridepublic void Run () {//TODO auto-generated method Stubtry {InputStream in = Server.getinputstream (); OutputStream o UT = Server.getoutputstream (); byte[] Recdata = Null;while (true) {recdata = new byte[buffer_size];int r = In.read (RecData); int r = In.read (Recdata), if (r>-1) {string data = new String (recdata), if (Data.trim (). Equals ("over")) {Server.close ( ); break;} SYSTEM.OUT.PRINTLN ("reads the data sent to the client:" +data); Out.write ("This is the data that the server sends to the client:". GetBytes ()); Out.write (recdata);} else {System.out.println ("The data is read! "); Server.close (); Break;//ss.close ();}}} catch (IOException e) {//TODO auto-generated catchBlocke.printstacktrace ();} Finally {try {server.close ()} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} public class Mulserversocketdemo {private static final int PORT = 1234;//private static final int buffer_size = 1024;publi c static void Main (string[] args) throws IOException {//TODO auto-generated method Stubint counter = 1; ServerSocket ss = new ServerSocket (PORT), while (true) {Socket s = ss.accept (); System.out.println ("First" + (counter++) + "Connection"); Thread t = new Thread (new Threadserversocket (s)); T.start ();}}}
Client:

Package Socket;import Java.io.ioexception;import Java.io.outputstream;import java.net.inetsocketaddress;import Java.net.socket;class Threadclientsocket implements Runnable {private static final String HOST = "192.168.8.17";p rivate s tatic Final int PORT = 1234;private socket client;public threadclientsocket () {client = new socket (); try {client.connect (n EW inetsocketaddress (HOST, PORT), 500);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}  @Overridepublic void Run () {//TODO auto-generated method Stubtry {OutputStream out = Client.getoutputstream (); int counter = 0;while (True) {System.out.println ("thread-->" +thread.currentthread (). GetName ()); Out.write ("Heart Beat!". GetBytes ()); SYSTEM.OUT.PRINTLN (client + "================" + (counter++)), try {thread.sleep (+);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} public class MulclientSocketdemo {public static void main (string[] args) throws IOException {//TODO auto-generated method Stubint counter = 4;w Hile ((counter--) >0) {New Thread (new Threadclientsocket ()). Start ();;} Client.settcpnodelay (True);}}





This implementation still has a lot of shortcomings, better implementation can be achieved through the channel, not to be continued ...




JAVA Socket network programming, the server receives the implementation of multiple client connections

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.