java多线程socket连接(监听连接数)

fkwuyue 贡献于2013-03-05

作者   创建于2012-06-06 01:30:27   修改者  修改于2013-03-06 01:02:22字数3497

文档摘要:java多线程socket连接(监听连接数)
关键词:

 1.服务器端 package com.test.Socket; import java.io.BufferedReader; import java.io.InputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; import java.util.Map; public class JavaSocket { private final String fileName = "/secketsup.Properties"; private BufferedReader in; static boolean b = true; public Socket socket = null; private final int i = 0; Socket socket1; public static final Map map = new HashMap(); public void send() { try { ServerSocket serverScoket = new ServerSocket(9802); System.out.println("等待客户端连接"); while (true) { socket = serverScoket.accept(); System.out.println("这是第" + (i + 1) + "个客户端"); InputStream in = JavaSocket.class.getResourceAsStream(fileName); InputStream is = socket.getInputStream(); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); byte[] b = new byte[1024]; int length = is.read(b); String str3 = new String(b, 0, length); System.out.println(str3); out.println(str3); out.println(str3.length()); // String[] str1 = str3.split(","); // if (str1.length == 2) { // System.out.println(str1[0] + " " + str1[1]); // if (str1[0].equals("admin") && str1[1].equals("123")) { // out.println("登录成功"); // // } else { // out.println(""); // // } // // } else { // out.println("0"); // } System.out.println(socket); out.flush(); socket.close(); /* * if (map.size() > 0 && map.get(str1) != null) { * map.remove(str1); map.put(str1, socket); } else { * System.out.println("12312321"); map.put(str1, socket); * System.out.println(map.size()); } System.out.println("!!!"); */ } } catch (Exception e) { e.printStackTrace(); } } public void servicetoclient(String devicenum, String num, String url, String ip) { final String ip1 = ip; final String devicenum1 = devicenum; final String num1 = num; final String url1 = url; new Thread() { @Override public void run() { try { socket1 = map.get(ip1); System.out.println(socket1); System.out.println("ip1......" + ip1); if (socket1 == null) { } else { while (true) { PrintWriter out = new PrintWriter(socket1 .getOutputStream(), true); out.println(num1 + devicenum1 + ":" + url1); System.out.println(num1 + devicenum1 + ":" + url1); out.flush(); break; } socket1.close(); map.remove(ip1); } } catch (Exception e) { e.printStackTrace(); } }; }.start(); } } 2. 客户端 package com.test.Socket; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.Date; public class TCPClent { public static void main(String[] args) throws UnknownHostException, IOException { //Socket socket = new Socket("192.168.1.93", 9830); Socket socket = new Socket("192.168.1.124", 9802); OutputStream outputStream = socket.getOutputStream(); outputStream.write(("connect to the server at time ," + new Date() .toString()).getBytes()); outputStream.flush(); System.out.println(socket); InputStream is = socket.getInputStream(); byte[] bytes = new byte[1024]; int n = is.read(bytes); System.out.println(new String(bytes, 0, n)); is.close(); socket.close(); } } 3. 初始化线程 package com.test.Socket; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Jsocket extends HttpServlet { public Jsocket() { super(); } @Override public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override public void init() throws ServletException { new Thread() { @Override public void run() { { JavaSocket javaSocket = new JavaSocket(); javaSocket.send(); } }; }.start(); } }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ] 3 人已下载

下载文档