首页 > 代码库 > JAVA SOCKET网络编程,服务端接收多个客户端连接的实现
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 = http://www.mamicode.com/null;>客户端: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); } }
该实现仍然有很多不足,更好的实现可通过通道来实现,未完待续..........
JAVA SOCKET网络编程,服务端接收多个客户端连接的实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。