JAVA SOCKET网络编程,服务端接收多个客户端连接的实现
这是基于前面一篇文章的一个简单扩展,当然这不是最好的实现
服务端如果要想接收多个客户端连接,必须死循环去接收新的客户端的连接请求,一个完整的客户端服务端socket连接通过一个线程来维护
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 stub server = i; } @Override public void run() { // TODO Auto-generated method stub try { InputStream in = server.getInputStream(); OutputStream out = server.getOutputStream(); Scanner sc = new Scanner(in); PrintWriter pw = new PrintWriter(out,true/*autoFlush*/); pw.println("Hello ! Enter BYE to Exit."); while(sc.hasNextLine()) { String line = sc.nextLine(); pw.println("Echo:"+line); if(line.trim().equals("BYE")) { break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { server.close(); } catch (IOException e) { // TODO Auto-generated catch block e.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 stub int counter = 1; ServerSocket ss = new ServerSocket(PORT); while(true) { Socket s = ss.accept(); System.out.println("第 "+(counter++)+" 个连接"); Thread t = new Thread(new ThreadServerSocket(s)); t.start(); } } }启动以上程序之后,通过命令行,telnet命令来试验下
同时开了6个连接,当然也可以通过JAVA来是实现:
服务端:
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 stub server = i; } @Override public void run() { // TODO Auto-generated method stub try { InputStream in = server.getInputStream(); OutputStream out = 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("读取到客户端发送的来数据:"+data); out.write("这是服务端发给客户端的数据:".getBytes()); out.write(recData); }else { System.out.println("数据读取完毕!"); server.close(); break; //ss.close(); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { server.close(); } catch (IOException e) { // TODO Auto-generated catch block e.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 stub int counter = 1; ServerSocket ss = new ServerSocket(PORT); while(true) { Socket s = ss.accept(); System.out.println("第 "+(counter++)+" 个连接"); Thread t = new Thread(new ThreadServerSocket(s)); t.start(); } } }客户端:
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"; private static final int PORT = 1234; private Socket client; public ThreadClientSocket() { client = new Socket(); try { client.connect(new InetSocketAddress(HOST, PORT), 500); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { // TODO Auto-generated method stub try { 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(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public class MulClientSocketDemo { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub int counter = 4; while((counter--)>0) { new Thread(new ThreadClientSocket()).start();; } // client.setTcpNoDelay(true); } }
该实现仍然有很多不足,更好的实现可通过通道来实现,未完待续..........
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。