标签:bar sys tostring 重用 tar data info 技术 global
SocketListenerPusher.java代码如下:
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
-
- import org.apache.commons.configuration.ConfigurationException;
- import org.directwebremoting.impl.DaemonThreadFactory;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import com.shihuan.dragonkeeper.common.utils.PropertiesUtil;
- import com.shihuan.dragonkeeper.global.ConfigFile;
-
- public class SocketListenerPusher implements Runnable {
-
- protected static Logger logger = LoggerFactory.getLogger(SocketListenerPusher.class);
-
- public static String socketlistenerserver_CONFIG = ConfigFile.SOCKETLISTENERSERVER__CONFIG + ConfigFile.SUFFIX_NAME;
-
- private ServerSocket serverSocket;
- private ExecutorService pool;
-
-
- public SocketListenerPusher() {
- int port = 0;
- int poolsize = 0;
-
- try {
- port = Integer.parseInt(PropertiesUtil.getPropertiesValue(socketlistenerserver_CONFIG, "serverport"));
- poolsize = Integer.parseInt(PropertiesUtil.getPropertiesValue(socketlistenerserver_CONFIG, "poolsize"));
-
- serverSocket = new ServerSocket();
- serverSocket.setReuseAddress(true);
- serverSocket.bind(new InetSocketAddress(port));
- pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * poolsize);
-
-
- ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory());
- executor.scheduleAtFixedRate(this, 1L, 1L, TimeUnit.MILLISECONDS);
-
- } catch (NumberFormatException e) {
- logger.error(e.getMessage(), e);
- e.printStackTrace();
- } catch (ConfigurationException e) {
- logger.error(e.getMessage(), e);
- e.printStackTrace();
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- e.printStackTrace();
- }
-
- }
-
- public void run() {
- Socket socket = null;
- try {
- socket = serverSocket.accept();
- pool.execute(new SocketListenerHandler(socket));
- } catch (IOException e) {
- System.out.println("线程池被关闭!!!!!!!!!!!");
- pool.shutdown();
- logger.error(e.getMessage(), e);
- e.printStackTrace();
- }
-
- }
SocketListenerHandler.java代码如下:
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.ObjectInputStream;
- import java.net.Socket;
- import java.sql.Connection;
- import java.sql.SQLException;
-
- import org.apache.commons.configuration.ConfigurationException;
- import org.apache.commons.dbutils.DbUtils;
- import org.apache.commons.dbutils.QueryRunner;
- import org.apache.commons.io.IOUtils;
- import org.directwebremoting.Browser;
- import org.directwebremoting.ScriptSessions;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import com.alibaba.fastjson.JSON;
- import com.shihuan.dragonkeeper.common.dto.DataSourceInfo;
- import com.shihuan.dragonkeeper.common.utils.ByteArrayUtil;
- import com.shihuan.dragonkeeper.common.utils.DataSourceMapUtil;
- import com.shihuan.dragonkeeper.common.utils.DateFormatterUtil;
- import com.shihuan.dragonkeeper.common.utils.PropertiesUtil;
- import com.shihuan.dragonkeeper.global.ConfigFile;
- import com.shihuan.dragonkeeper.server.bean.ActivityServiceBean;
-
- public class SocketListenerHandler implements Runnable {
-
- protected static Logger logger = LoggerFactory.getLogger(SocketListenerHandler.class);
-
- private static String jdbc_CONFIG = ConfigFile.JDBC_CONFIG + ConfigFile.SUFFIX_NAME;
-
- public static final int timeOut = 0*1000 ;
- private final String dataRealTimeAction_id = "Agentdata_" + Math.random();
-
- private static final String noData = "{‘nodata‘:‘心跳信息‘}";
- private static final String errorData = "{‘error‘:‘无法解析的请求‘}";
-
-
- private Socket connectedsocket = null;
-
- public SocketListenerHandler(Socket socket){
- this.connectedsocket = socket;
- }
-
- @Override
- public void run() {
- BufferedReader in = null;
- String resultData = "";
-
- try {
- connectedsocket.setSoTimeout(timeOut);
- connectedsocket.setKeepAlive(false);
-
- in = new BufferedReader(new InputStreamReader(connectedsocket.getInputStream()));
- if (in.ready()) {
-
- resultData = getNoHeadData(in.readLine());
- logger.info("#### 结果DATA = "+resultData);
-
- if (resultData==null || "".equals(resultData)) {
- logger.info(dataRealTimeAction_id + " -->>> " + "内容为空!");
- } else if (resultData.charAt(0) != ‘{‘) {
- logger.info(dataRealTimeAction_id + " -->>> " + noData);
- } else {
- ActivityServiceBean asb = JSON.parseObject(resultData, ActivityServiceBean.class);
- System.out.println("打印预处理信息Start......");
- System.out.println(asb.getProxyname() + " -- " + asb.getIp() + " -- " + asb.getCalltime() + " -- " + asb.getAnswertime() + " -- " + asb.getCpu() + " -- " + asb.getThread() + " -- " + asb.getStatus() + " -- " + asb.getAccessaddress() + " -- " + asb.getAccessfilename() + " -- " + asb.getSql() + " -- " + asb.getContent());
- System.out.println("打印预处理信息End......");
-
- logger.info(dataRealTimeAction_id + ": 成功处理了接收到的数据!");
- }
-
- }
- } catch (IOException e) {
- logger.error(e.getMessage() + " " + errorData, e);
- e.printStackTrace();
- } catch (NumberFormatException e) {
- logger.error(e.getMessage(), e);
- e.printStackTrace();
- } finally {
-
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- e.printStackTrace();
- }
- }
- }
-
- }
TestSocketListenerPusher.java请求端代码如下:
- import java.io.BufferedOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.Date;
-
- import org.apache.commons.configuration.ConfigurationException;
-
- import com.alibaba.fastjson.JSON;
- import com.shihuan.dragonkeeper.common.utils.ByteArrayUtil;
- import com.shihuan.dragonkeeper.common.utils.PropertiesUtil;
- import com.shihuan.dragonkeeper.global.ConfigFile;
- import com.shihuan.dragonkeeper.server.bean.ActivityServiceBean;
-
- public class TestSocketListenerPusher implements Runnable {
-
- private static String socketlistenerserver_CONFIG = ConfigFile.SOCKETLISTENERSERVER__CONFIG + ConfigFile.SUFFIX_NAME;
-
- private Socket socketclient = null;
-
- @Override
- public void run() {
-
- String serverip = "";
- int port = 0;
-
- OutputStream os = null;
-
- try {
- serverip = PropertiesUtil.getPropertiesValue(socketlistenerserver_CONFIG, "serverip");
- port = Integer.parseInt(PropertiesUtil.getPropertiesValue(socketlistenerserver_CONFIG, "serverport"));
-
-
- ActivityServiceBean asb = null;
-
- for (int i=0; i<2; i++) {
- asb = new ActivityServiceBean();
- asb.setProxyname("testProxyname"+i);
- asb.setIp("testIp"+i);
- Date curdate = new Date();
- asb.setCalltime(curdate);
- asb.setAnswertime(curdate);
- asb.setCpu("testCpu"+i);
- asb.setThread("testThread"+i);
- asb.setStatus("testStatus"+i);
- asb.setAccessaddress("testAccessaddress"+i);
- asb.setAccessfilename("testAccessfilename"+i);
- asb.setSql("testSql"+i);
- asb.setContent("testContent"+i);
-
- String jsonStr = JSON.toJSONString(asb).trim();
- byte[] information = (new String(ByteArrayUtil.getIntToByte(jsonStr.length()))+jsonStr).getBytes();
-
- System.out.println(information.length);
-
- socketclient = new Socket(serverip, port);
- socketclient.setSoTimeout(0);
- socketclient.setKeepAlive(false);
-
- os = new BufferedOutputStream(socketclient.getOutputStream());
- os.write(information);
- os.flush();
-
- System.out.println("Client" + i + " -->>> " + new String(ByteArrayUtil.getIntToByte(jsonStr.length()))+jsonStr);
-
- os.close();
- Thread.sleep(3000);
- }
-
-
- } catch (ConfigurationException e) {
- e.printStackTrace();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
-
- }
-
-
- }
-
- public static void main(String[] args) {
- Thread t = new Thread(new TestSocketListenerPusher());
- t.start();
- }
-
- }
源代码在笔者shihuan8@163.com邮箱网盘中J2EE代码文件夹里。
----------------------------------------------------------------------------------
如果是按byte[]传输数据的情况,请参考如下代码:
SimpleSocketServer.java代码如下:
- package com.shihuan.socket;
-
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.InetSocketAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
-
- public class SimpleSocketServer {
-
- public static void main(String[] args) {
- try {
- ServerSocket ss = new ServerSocket();
- ss.setReuseAddress(true);
- ss.setReceiveBufferSize(128*1024);
- ss.bind(new InetSocketAddress(19990));
-
- Socket client = ss.accept();
- InputStream in = new BufferedInputStream(client.getInputStream());
-
- byte tmpb = (byte)in.read();
- System.out.println("第一个字节的byte值 --->> " + tmpb);
- System.out.println("接收字节 --->> " + in.available());
- byte[] bc = new byte[in.available()+1];
- bc[0] = tmpb;
- in.read(bc, 1, in.available());
-
- System.out.println(bc.length);
- System.out.println(new String(bc));
- in.close();
- } catch (IOException e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
-
- }
-
- }
SimpleSocketClient.java代码如下:
- package com.shihuan.socket;
-
- import java.io.BufferedOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
-
- public class SimpleSocketClient {
-
- public static void main(String[] args) throws UnknownHostException {
- try {
- Socket s = new Socket("192.168.1.10", 19990);
- OutputStream os = new BufferedOutputStream(s.getOutputStream());
- String info = "abc!";
- info = "大家好!";
- byte[] bi = info.getBytes();
- os.write(bi);
- os.flush();
- os.close();
- } catch (IOException e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
- }
-
- }
稍微复杂一点儿代码示例,处理了粘包问题:
StartListenerTcpThread.java代码:
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.InetSocketAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.SocketAddress;
- import java.util.Vector;
- import java.util.concurrent.ExcutorService;
- import java.util.concurrent.Excutors;
-
- import org.apache.commons.io.IUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import com.shihuan.dragonkeeper.common.utils.ByteArrayUtil;
- import com.shihuan.dragonkeeper.global.ConfigFile;
-
- public class StartListenerTcpThread implements Runnable {
- public static Logger logger = LoggerFactory.getLogger(StartListenerTcpThread.class);
-
- private static ExcutorService Threadpool = Excutors.newCachedThreadPool();
- private static boolean businessflag = true;
- private static final int receiveBufferSize = 128;
- private static Vector<byte[]> tmpbytes = new Vector<byte[]>();
- private ServerSocket serverSocket = null;
-
- public StartListenerTcpThread(String ip, int port){
- try{
- serverSocket = new ServerSocket();
- serverSocket.setReuseAddress(true);
- serverSocket.setReceiveBufferSize(receiveBufferSize*1024);
- serverSocket.setSoTimeout(0);
- SocketAddress sa = new InetSocketAddress(port);
- serverSocket.bind(sa, 20);
- }catch(IOException e){
- logger.error(e.getMessage(), e);
- }
- }
-
- public void run(){
- Socket socket = null;
- while(true){
- if(businessflag){
- try{
- socket = serverSocket.accept();
- System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
- InputStream socketIn = new BufferedInputStream(socket.getInputStream());
- byte tmpb = (byte)socketIn.read();
- byte[] currentbytes = null;
- if(tmpbytes.size() > 0){
- int oldBytesLen = tmpbytes.get(0).length;
- int socketBytesLen = socketIn.available()+1;
- int currentLength = oldByteLen + socketBytesLen;
- currentbytes = new byte[currentLength];
- System.arraycopy(tmpbytes.get(0), 0, currentbytes, oldBytesLen);
- currentbytes[oldBytesLen] = tmpb;
- socketIn.read(currentbytes, oldBytesLen+1, socketBytesLen-1);
- socketIn.close();
- splitInputStreamByte(currentbytes);
- }else{
- int socketBytesLen = socketIn.available()+1;
- currentbytes = new byte[socketBytesLen];
- currentbytes[0] = tmpb;
- socketIn.read(currentbytes, 1, socketBytesLen-1);
- socketIn.close();
- splitInputStreamByte(currentbytes);
- }
- }catch(IOException e){
- logger.error(e.getMessage(), e);
- }
- }
- }
- }
-
-
- private static void splitInputStreamByte(byte[] parambytes) {
- if(parambytes != null){
- if(parambytes.length > 4){
- byte[] head = new byte[4];
- System.arraycopy(parambytes, 0, head, 0, 4);
- int bodyLength = ByteArrayUtil.getint(head);
- if(bodyLength <= parambytes.length-4){
- final byte[] body = new byte[bodyLength];
- System.arraycopy(parambytes, 4, body, 0, bodyLength);
- ThreadPool.execute(new Runnable(){
- public void run(){
- byte[] processDatas = body;
- try{
- System.out.println(IOUtils.toString(processDatas, "UTF-8").trim());
- }catch(IOException e){
- logger.error(e.getMessage(), e);
- }
- }
- });
-
- int resultLen = parambytes.length-4-bodyLength;
- if(resultLen == 0){
- splitInputStreamByte(null);
- }else{
- byte[] resultbytes = new byte[resultLen];
- System.arraycopy(parambytes, 4+bodyLength, resultbytes, 0, resultLen);
- splitInputStreamByte(resultbytes);
- }
- }else{
- tmpbytes.clear();
- tmpbytes.add(parambytes);
- }
- }else{
- tmpbytes.clear();
- tmpbytes.add(parambytes);
- }
- }
- }
-
- public static void openflag(){
- businessflag = true;
- }
-
- public static void closeflag(){
- businessflag = false;
- }
-
- }
TestTcpSocket.java代码:
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
-
- import com.shihuan.dragonkeeper.common.utils.ByteArrayUtil;
- import com.shihuan.dragonkeeper.global.ConfigFile;
-
- public class TestTcpSocket implements Runnable{
- private Socket socketClient = null;
-
- public void run(){
- String serverip = "192.168.1.10";
- int port = 19990;
- try{
- while(true){
- System.out.println("SocketClient start......");
- String mystr = "hello everyone!";
- socketClient = new Socket(serverip, port);
- OutputStream os = socketClient.getOutputStream();
- byte[] head = ByteArrayUtil.int2byte(mystr.length());
- byte[] body = mystr.getBytes();
- byte[] total = ByteArrayUtil.byteMerge(head, body);
-
- os.write(total);
- os.flush();
- os.close();
-
- Thread.sleep(1000);
-
- System.out.println("SocketClient end......");
- }
- }catch(Exception e){
- logger.error(e.getMessage(), e);
- }
- }
-
- public static void main(String[] args){
- Thread t = new Thread(new TestTcpSocket());
- t.start();
- }
- }
下面写ByteArrayUtil.java代码:
- package com.shihuan.dragonkeeper.common.utils;
-
- public class ByteArrayUtil {
-
-
- public static final byte[] int2byte(int paramInt){
- byte[] resultByte = new byte[4];
- resultByte[3] = ((byte)(paramInt & 0xFF));
- resultByte[2] = ((byte)(paramInt >>> 8 & 0xFF));
- resultByte[1] = ((byte)(paramInt >>> 16 & 0xFF));
- resultByte[0] = ((byte)(paramInt >>> 24 & 0xFF));
-
- return resultByte;
- }
-
-
- public static final int getint(byte[] paramArrayOfByte){
- int result = (paramArrayOfByte[0] & 0xFF) << 24 | (paramArrayOfByte[1] & 0xFF) << 16 | (paramArrayOfByte[2] & 0xFF) << 8 | paramArrayOfByte[3] & 0xFF;
- return result;
- }
-
-
- public static byte[] byteMerge(byte[] byte1, byte[] byte2){
- byte[] result = new byte[byte1.length+byte2.length];
- System.arraycopy(byte1, 0, result, 0, byte1.length);
- System.arraycopy(byte2, 0, result, byte1.length, byte2.length);
- return result;
- }
- }
http://blog.csdn.net/defonds/article/details/8782785
Java Socket长连接示例代码
标签:bar sys tostring 重用 tar data info 技术 global
原文地址:http://www.cnblogs.com/yhtboke/p/6230149.html