代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
Java Socket多线程——服务器端
作者:
mousekitty
/ 发布于
2014/3/11
/
611
这是自己写的一段代码,运行在jdk1.5上,测试成功。jdk1.4不能用,因为不支持java.util.concurrent包。 如果要运行在jdk1.4上,则需要加入backport-util-concurrent.jar的引用,然后将SocketServer.java中的 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; 改成 import edu.emory.mathcs.backport.java.util.concurrent.*; 即可。 SocketServer.java import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.log4j.Logger; import Smart.Java.Com.Constant; import Smart.Java.Com.StringHandler; //c:\jdk1.5\bin\java -classpath c:\jdk1.5 SocketServerThread public class SocketServer { private static Logger logger = Logger.getLogger(SocketServer.class); private ServerSocket serverSocket; private ExecutorService executorService;//线程池 private final int POOL_SIZE=10;//单个CPU线程池大小 public SocketServer(){ try { serverSocket = new ServerSocket(Constant.SocketPort); executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE); System.out.println("监听服务器启动,端口号:" + Constant.SocketPort); } catch(Exception e) { String s = "监听服务器启动 失败,可能是端口" + Constant.SocketPort + "已被占用,请重置端口" + Constant.SocketPort + "后重试"; logger.error(e.getMessage() + "" + s); //System.out.println(); } } public void service() { try { while (true) { Socket clientSocket=null; try { // 接收客户连接,只要客户进行了连接,就会触发accept();从而建立连接 clientSocket = serverSocket.accept(); executorService.execute(new SocketHandler(clientSocket)); } catch (Exception e) { logger.error(StringHandler.GetException(e)); e.printStackTrace(); } } } catch (Exception e1) { //logger.error(StringHandler.GetException(e1)); //e1.printStackTrace(); } finally { if (serverSocket != null) { try { serverSocket.close(); }catch(Exception ex1){} } } } public static void main(String[] args) throws Exception { try { new SocketServer().service(); } catch(Exception ex) { logger.error(StringHandler.GetException(ex)); ex.printStackTrace(); } } } SocketHandler.java import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Calendar; import org.apache.log4j.Logger; import Smart.Java.Com.Constant; import Smart.Java.Com.MonitorHandler; import Smart.Java.Com.StringHandler; //public class SocketHandler implements Runnable { public class SocketHandler implements Runnable{ private static Logger logger = Logger.getLogger(SocketHandler.class); private Socket socket; public SocketHandler(Socket socket) { this.socket = socket; //run(); } private PrintWriter getWriter(Socket socket) throws Exception { OutputStream socketOut = socket.getOutputStream(); return new PrintWriter(socketOut, true); } //private BufferedReader getReader(Socket socket) throws Exception { // //} private String GetInfoByAction(String _args) { try { String returnStr = ""; String[] ssArgs = _args.split(";"); String _productSeries = ssArgs[0]; String _ciKey = ssArgs[1]; if (_productSeries.equals("") || _ciKey.equals("")) { return "0"; } String _ip = ssArgs[2]; String _port = ssArgs[3]; String _username = ""; String _pwd = ""; if (ssArgs.length > 4) { _username = ssArgs[4]; if (ssArgs.length > 5) { _pwd = ssArgs[5]; } } MonitorHandler monitor = new MonitorHandler(_productSeries, _ciKey, _ip, _port, _username, _pwd); returnStr = monitor.GetMonitorInfo(); return returnStr; } catch (Exception e) { e.printStackTrace(); logger.error(StringHandler.GetException(e)); return Constant.ErrorNumber_UnkownException; } } private static String GetCurrentTime() { try { Calendar cal = Calendar.getInstance(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = formatter.format(cal.getTime()); return time; } catch(Exception ex) { ex.printStackTrace(); logger.error(StringHandler.GetException(ex)); return ""; } } public void run() { try { //得到客户端发送的信息 BufferedReader br = null; InputStream socketIn = socket.getInputStream(); br = new BufferedReader(new InputStreamReader(socketIn)); String actionInfo = ""; try { actionInfo = br.readLine(); } catch(Exception ex){} while (actionInfo != null) { System.out.println(GetCurrentTime() + "收到来自【" + socket.getInetAddress() + ":" + socket.getPort() + "】的监听请求:" + actionInfo); if (actionInfo != null) { String str = GetInfoByAction(actionInfo); //返回信息给Client端 PrintWriter pw = getWriter(socket); //System.out.println(str); pw.println(str); } actionInfo = br.readLine(); } } catch (Exception e) { logger.error(StringHandler.GetException(e)); e.printStackTrace(); } finally { try { if (socket != null) socket.close(); } catch (Exception e) {} } } }
试试其它关键字
多线程
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
mousekitty
贡献的其它代码
(
13
)
.
MouseClick事件触发右键菜单
.
通过SetupAPI扫描/禁用/恢复设备
.
利用WebService下载文件
.
遍历文件夹得到所有文件
.
写Windows服务
.
截屏(基于MFC)
.
截取Char[]
.
Java Socket多线程——服务器端
.
List和String互相转换
.
自定义函数Split
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3