Commit 179853741d513367acfa0087f08999db82592688

Authored by 张宗朋
1 parent d256d569

socket

src/main/java/com/cnlive/socket/SocketServer.java
1 1 package com.cnlive.socket;
2 2  
3   -import java.io.BufferedReader;
4 3 import java.io.IOException;
5 4 import java.io.InputStream;
6   -import java.io.InputStreamReader;
7   -import java.io.ObjectInputStream;
8   -import java.io.ObjectOutputStream;
9 5 import java.io.OutputStream;
10 6 import java.net.ServerSocket;
11 7 import java.net.Socket;
... ... @@ -13,141 +9,137 @@ import java.util.concurrent.ConcurrentHashMap;
13 9  
14 10 public class SocketServer {
15 11  
16   - /**
17   - * 要处理客户端发来的对象,并返回一个对象,可实现该接口。
18   - */
19   - public interface ObjectAction{
20   - Object doAction(Object rev);
21   - }
22   -
23   - /*public static final class DefaultObjectAction implements ObjectAction{
24   - public Object doAction(Object rev) {
25   - System.out.println("处理并返回:"+rev);
26   - return rev;
27   - }
28   - } */
29   -
30   - public static void main(String[] args) {
31   - int port = 65432;
32   - SocketServer server = new SocketServer(port);
33   - server.start();
34   - }
35   -
36   - private int port;
37   - private volatile boolean running=false;
38   - private long receiveTimeDelay=7000;
39   - private ConcurrentHashMap<Class, ObjectAction> actionMapping = new ConcurrentHashMap<Class,ObjectAction>();
40   - private Thread connWatchDog;
41   -
42   - public SocketServer(int port) {
43   - this.port = port;
44   - }
45   -
46   - public void start(){
47   - if(running)return;
48   - running=true;
49   - connWatchDog = new Thread(new ConnWatchDog());
50   - connWatchDog.start();
51   - }
52   -
53   - @SuppressWarnings("deprecation")
54   - public void stop(){
55   - if(running)running=false;
56   - if(connWatchDog!=null)connWatchDog.stop();
57   - }
58   -
59   - public void addActionMap(Class<Object> cls,ObjectAction action){
60   - actionMapping.put(cls, action);
61   - }
62   -
63   - class ConnWatchDog implements Runnable{
64   - public void run(){
65   - try {
66   - ServerSocket ss = new ServerSocket(port,5);
67   - while(running){
68   - System.out.println("等待客服端。。。");
69   - Socket s = ss.accept();
70   - new Thread(new SocketAction(s)).start();
71   - }
72   - } catch (IOException e) {
73   - e.printStackTrace();
74   - SocketServer.this.stop();
75   - }
76   -
77   - }
78   - }
79   -
80   - class SocketAction implements Runnable{
81   - Socket s;
82   - boolean run=true;
83   - long lastReceiveTime = System.currentTimeMillis();
84   - public SocketAction(Socket s) {
85   - this.s = s;
86   - }
87   - public void run() {
88   - while(running && run){
89   - try {
  12 + /**
  13 + * 要处理客户端发来的对象,并返回一个对象,可实现该接口。
  14 + */
  15 + public interface ObjectAction {
  16 + Object doAction(Object rev);
  17 + }
  18 +
  19 + /*
  20 + * public static final class DefaultObjectAction implements ObjectAction{
  21 + * public Object doAction(Object rev) { System.out.println("处理并返回:"+rev);
  22 + * return rev; } }
  23 + */
  24 +
  25 + public static void main(String[] args) {
  26 + int port = 65432;
  27 + SocketServer server = new SocketServer(port);
  28 + server.start();
  29 + }
  30 +
  31 + private int port;
  32 + private volatile boolean running = false;
  33 + private long receiveTimeDelay = 30000;
  34 + private ConcurrentHashMap<Class, ObjectAction> actionMapping = new ConcurrentHashMap<Class, ObjectAction>();
  35 + private Thread connWatchDog;
  36 +
  37 + public SocketServer(int port) {
  38 + this.port = port;
  39 + }
  40 +
  41 + public void start() {
  42 + if (running)
  43 + return;
  44 + running = true;
  45 + connWatchDog = new Thread(new ConnWatchDog());
  46 + connWatchDog.start();
  47 + }
  48 +
  49 + @SuppressWarnings("deprecation")
  50 + public void stop() {
  51 + if (running)
  52 + running = false;
  53 + if (connWatchDog != null)
  54 + connWatchDog.stop();
  55 + }
  56 +
  57 + public void addActionMap(Class<Object> cls, ObjectAction action) {
  58 + actionMapping.put(cls, action);
  59 + }
  60 +
  61 + class ConnWatchDog implements Runnable {
  62 + public void run() {
  63 + try {
  64 + ServerSocket ss = new ServerSocket(port, 5);
  65 + while (running) {
  66 + System.out.println("等待客服端 请求。。。");
  67 + Socket s = ss.accept();
  68 + new Thread(new SocketAction(s)).start();
  69 + }
  70 + } catch (IOException e) {
  71 + e.printStackTrace();
  72 + SocketServer.this.stop();
  73 + }
  74 +
  75 + }
  76 + }
  77 +
  78 + class SocketAction implements Runnable {
  79 + Socket s;
  80 + boolean run = true;
  81 + long lastReceiveTime = System.currentTimeMillis();
  82 +
  83 + public SocketAction(Socket s) {
  84 + this.s = s;
  85 + }
  86 +
  87 + public void run() {
  88 + String appId = null;
  89 + String activityId = null;
  90 + while (running && run) {
  91 + try {
90 92 Thread.sleep(3000);
91 93 } catch (InterruptedException e1) {
92   - // TODO Auto-generated catch block
93 94 e1.printStackTrace();
94 95 }
95   - try {
96   - InputStream in = s.getInputStream();
97   - OutputStream out = s.getOutputStream();
98   - byte[] recData = null;
99   - while(true) {
100   - recData = new byte[1024];
101   - int r = in.read(recData);
102   - if(r>-1) {
103   - String data = new String(recData);
104   - /* if(data.trim().equals("over")) {
105   - s.close();
106   - break;
107   - }*/
108   - System.out.println("读取到客户端发送的来数据:"+data);
109   - }
110   - }
111   - /* if(in.available()>0){
112   - BufferedReader br = new BufferedReader(new InputStreamReader(in));
113   - //out = new PrintWriter(socket.getOutputStream(), true);
114   - String line = br.readLine();
115   - System.out.println("you input is : " + line);
116   - //out.println("you input is :" + line);
117   - //out.close();
118   - // in.close();
119   - }*/
120   - /*ObjectInputStream ois = new ObjectInputStream(in);
121   - Object obj = ois.readObject();
122   - lastReceiveTime = System.currentTimeMillis();
123   - System.out.println("接收:\t"+obj);
124   - ObjectAction oa = actionMapping.get(obj.getClass());
125   - oa = oa==null?new DefaultObjectAction():oa;
126   - Object out = oa.doAction(obj);
127   - if(out!=null){
128   - ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
129   - oos.writeObject(out);
130   - oos.flush();
131   - } */
132   - } catch (Exception e) {
133   - System.out.println(System.currentTimeMillis()-lastReceiveTime);
134   - e.printStackTrace();
135   - overThis();
136   - }
137   - }
138   - }
139   -
140   - private void overThis() {
141   - if(run)run=false;
142   - if(s!=null){
143   - try {
144   - s.close();
145   - } catch (IOException e) {
146   - e.printStackTrace();
147   - }
148   - }
149   - System.out.println("关闭:"+s.getRemoteSocketAddress());
150   - }
151   -
152   - }
  96 +
  97 + if (System.currentTimeMillis() - lastReceiveTime > receiveTimeDelay) {
  98 + System.out.println("socket连接超过30s.....");
  99 + } else {
  100 + try {
  101 + InputStream in = s.getInputStream();
  102 + OutputStream out = s.getOutputStream();
  103 + if (in.available() > 0) {
  104 + byte[] recData = null;
  105 + while (true) {
  106 + recData = new byte[1024];
  107 + int r = in.read(recData);
  108 + if (r > -1) {
  109 + lastReceiveTime = System.currentTimeMillis();
  110 + String data = new String(recData);
  111 + System.out.println("读取到客户端发送的来数据:" + data);
  112 + String[] str = data.split("#");
  113 + String app = str[0];
  114 + String activity = str[1];
  115 + appId = app.substring(app.indexOf("=")+1);
  116 + activityId = activity.substring(activity.indexOf("=")+1);
  117 + System.out.println(appId+" : "+activityId);
  118 + }
  119 + }
  120 + }
  121 + } catch (Exception e) {
  122 + System.out.println(System.currentTimeMillis() - lastReceiveTime);
  123 + e.printStackTrace();
  124 + overThis();
  125 + }
  126 + }
  127 + }
  128 +
  129 + }
  130 +
  131 + private void overThis() {
  132 + if (run)
  133 + run = false;
  134 + if (s != null) {
  135 + try {
  136 + s.close();
  137 + } catch (IOException e) {
  138 + e.printStackTrace();
  139 + }
  140 + }
  141 + System.out.println("关闭:" + s.getRemoteSocketAddress());
  142 + }
  143 +
  144 + }
153 145 }
... ...