精华内容
下载资源
问答
  • haproxy 服务端超时时间 timeout server 17000 --后台程序17秒没有响应 返回超时

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

    也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                   
    haproxy 服务端超时时间:haproxy 配置:timeout server 17000    --后台程序17秒没有响应,返回超时Jun 27 09:29:56 localhost haproxy[13593]: 192.168.32.101:43552 [27/Jun/2016:09:29:39.002] www appserver_3000/webhost01_8001 64/0/6/-1/17072 504 194 - - sH-- 0/0/0/0/0 0/0 "GET /admin/api/menu HTTP/1.1"Jun 27 09:30:13 localhost haproxy[13593]: 192.168.32.101:43558 [27/Jun/2016:09:29:56.300] www appserver_3000/webhost01_8001 59/0/7/-1/17067 504 194 - - sH-- 0/0/0/0/0 0/0 "GET /admin/api/menu HTTP/1.1"[root@dr-mysql01 test]# cat get.pl use  LWP::UserAgent; my $ua = LWP::UserAgent->new;#$ua->env_proxy('http','http://$ARGV[0]');$ua->agent("Mozilla/8.0");my $response = $ua->get('http://www.zjtest6.com/admin/api/menu');if ($response->is_success) { print $response->decoded_content;  # or whatever}else { die $response->status_line;}[root@dr-mysql01 test]# sh ./a1.sh504 Gateway Time-out at ./get.pl line 14.Mon Jun 27 09:31:16 CST 2016504 Gateway Time-out at ./get.pl line 14.Mon Jun 27 09:31:33 CST 2016

               

    给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

    这里写图片描述
    展开全文
  • Socket通信完整实例(心跳包,客户端断线重连,服务端超时断开客户端连接) 通用工具类 public class SocketUtil { public static String ADDRESS = "192.168.1.123"; public static...

    Socket通信完整实例(心跳包,客户端断线重连,服务端超时断开客户端连接)

    通用工具类

    public class SocketUtil {
    
        public static String ADDRESS = "192.168.1.123";
        public static int PORT = 10086;
    
        /**
         * 读数据
         *
         * @param bufferedReader
         */
        public static String readFromStream(BufferedReader bufferedReader) {
            try {
                String s;
                if ((s = bufferedReader.readLine()) != null) {
                    return s;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 写数据
         *
         * @param data
         * @param printWriter
         */
        public static void write2Stream(String data, PrintWriter printWriter) {
            if (data == null) {
                return;
            }
            if (printWriter != null) {
                printWriter.println(data);
            }
        }
    
    
        /**
         * 关闭输入流
         *
         * @param socket
         */
        public static void inputStreamShutdown(Socket socket) {
            try {
                if (!socket.isClosed() && !socket.isInputShutdown()) {
                    socket.shutdownInput();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 关闭BufferedReader
         *
         * @param br
         */
        public static void closeBufferedReader(BufferedReader br) {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 关闭输出流
         *
         * @param socket
         */
        public static void outputStreamShutdown(Socket socket) {
            try {
                if (!socket.isClosed() && !socket.isOutputShutdown()) {
                    socket.shutdownOutput();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 关闭PrintWriter
         *
         * @param pw
         */
        public static void closePrintWriter(PrintWriter pw) {
            if (pw != null) {
                pw.close();
            }
        }
    
        /**
         * 获取本机IP地址
         */
        public static String getIP() {
            String hostIP = null;
            try {
                Enumeration nis = NetworkInterface.getNetworkInterfaces();
                InetAddress ia = null;
                while (nis.hasMoreElements()) {
                    NetworkInterface ni = (NetworkInterface) nis.nextElement();
                    Enumeration<InetAddress> ias = ni.getInetAddresses();
                    while (ias.hasMoreElements()) {
                        ia = ias.nextElement();
                        if (ia instanceof Inet6Address) {
                            continue;// skip ipv6
                        }
                        String ip = ia.getHostAddress();
                        if (!"127.0.0.1".equals(ip)) {
                            hostIP = ia.getHostAddress();
                            break;
                        }
                    }
                }
            } catch (SocketException e) {
                e.printStackTrace();
            }
            return hostIP;
    
        }
    
    }
    

    客户端

    状态回调接口

    public interface SocketClientResponseInterface<T> {
    
        /**
         * 客户端连接回调
         */
        void onSocketConnect();
    
        /**
         * 客户端收到服务端消息回调
         *
         * @param socketResult
         * @param code
         */
        void onSocketReceive(T socketResult, int code);
    
        /**
         * 客户端关闭回调
         *
         * @param msg
         * @param code
         */
        void onSocketDisable(String msg, int code);
    }
    

    客户端Socket实例代码

    SocketClientThread连接线程

    /**
     * 写数据采用死循环,没有数据时wait,有新消息时notify
     * Created by gavinandre on 18-1-8.
     */
    public class SocketClientThread extends Thread implements SocketCloseInterface {
    
        private static final String TAG = SocketClientThread.class.getSimpleName();
    
        private volatile String name;
    
        private boolean isLongConnection = true;
        private boolean isReConnect = true;
        private SocketSendThread mSocketSendThread;
        private SocketReceiveThread mSocketReceiveThread;
        private SocketHeartBeatThread mSocketHeartBeatThread;
        private Socket mSocket;
    
        private boolean isSocketAvailable;
    
        private SocketClientResponseInterface socketClientResponseInterface;
    
        public SocketClientThread(String name, SocketClientResponseInterface socketClientResponseInterface) {
            this.name = name;
            this.socketClientResponseInterface = socketClientResponseInterface;
        }
    
        @Override
        public void run() {
            final Thread currentThread = Thread.currentThread();
            final String oldName = currentThread.getName();
            currentThread.setName("Processing-" + name);
            try {
                initSocket();
                Log.i(TAG, "run: SocketClientThread end");
            } finally {
                currentThread.setName(oldName);
            }
        }
    
        /**
         * 初始化socket客户端
         */
        private void initSocket() {
            try {
                mSocket = SocketFactory.getDefault().createSocket();
                SocketAddress socketAddress = new InetSocketAddress(SocketUtil.ADDRESS, SocketUtil.PORT);
                mSocket.connect(socketAddress, 10000);
    
                isSocketAvailable = true;
    
                //开启接收线程
                mSocketReceiveThread = new SocketReceiveThread("SocketReceiveThread",
                        new BufferedReader(new InputStreamReader(mSocket.getInputStream(), "UTF-8")),
                        socketClientResponseInterface, this);
                mSocketReceiveThread.start();
    
                //开启发送线程
                PrintWriter printWriter = new PrintWriter(mSocket.getOutputStream(), true);
                Log.i(TAG, "initSocket: " + printWriter);
                mSocketSendThread = new SocketSendThread("SocketSendThread", printWriter);
                mSocketSendThread.setCloseSendTask(false);
                mSocketSendThread.start();
    
                //开启心跳线程
                if (isLongConnection) {
                    mSocketHeartBeatThread = new SocketHeartBeatThread("SocketHeartBeatThread",
                            printWriter, mSocket, this);
                    mSocketHeartBeatThread.start();
                }
    
                if (socketClientResponseInterface != null) {
                    socketClientResponseInterface.onSocketConnect();
                }
            } catch (ConnectException e) {
                failedMessage("服务器连接异常,请检查网络", SocketUtil.FAILED);
                e.printStackTrace();
                stopThread();
            } catch (IOException e) {
                failedMessage("网络发生异常,请稍后重试", SocketUtil.FAILED);
                e.printStackTrace();
                stopThread();
            }
        }
    
        /**
         * 发送消息
         */
        public void sendMsg(String data) {
            if (mSocketSendThread != null) {
                mSocketSendThread.sendMsg(data);
            }
        }
    
        /**
         * 关闭socket客户端
         */
        public synchronized void stopThread() {
            //关闭接收线程
            closeReceiveTask();
            //唤醒发送线程并关闭
            wakeSendTask();
            //关闭心跳线程
            closeHeartBeatTask();
            //关闭socket
            closeSocket();
            //清除数据
            clearData();
            failedMessage("断开连接", SocketUtil.FAILED);
            if (isReConnect) {
                SocketUtil.toWait(this, 15000);
                initSocket();
                Log.i(TAG, "stopThread: " + Thread.currentThread().getName());
            }
        }
    
        /**
         * 唤醒后关闭发送线程
         */
        private void wakeSendTask() {
            if (mSocketSendThread != null) {
                mSocketSendThread.wakeSendTask();
            }
        }
    
        /**
         * 关闭接收线程
         */
        private void closeReceiveTask() {
            if (mSocketReceiveThread != null) {
                mSocketReceiveThread.close();
                mSocketReceiveThread = null;
            }
        }
    
        /**
         * 关闭心跳线程
         */
        private void closeHeartBeatTask() {
            if (mSocketHeartBeatThread != null) {
                mSocketHeartBeatThread.close();
            }
        }
    
        /**
         * 关闭socket
         */
        private void closeSocket() {
            if (mSocket != null) {
                if (!mSocket.isClosed() && mSocket.isConnected()) {
                    try {
                        mSocket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                isSocketAvailable = false;
                mSocket = null;
            }
        }
    
        /**
         * 清除数据
         */
        private void clearData() {
            if (mSocketSendThread != null) {
                mSocketSendThread.clearData();
            }
        }
    
        /**
         * 连接失败回调
         */
        private void failedMessage(String msg, int code) {
            if (socketClientResponseInterface != null) {
                socketClientResponseInterface.onSocketDisable(msg, code);
            }
        }
    
        @Override
        public void onSocketShutdownInput() {
            if (isSocketAvailable) {
                SocketUtil.inputStreamShutdown(mSocket);
            }
        }
    
        @Override
        public void onSocketDisconnection() {
            isSocketAvailable = false;
            stopThread();
        }
    
        /**
         * 设置是否断线重连
         */
        public void setReConnect(boolean reConnect) {
            isReConnect = reConnect;
        }
    
    }
    

    SocketReceiveThread 接收线程

    /**
     * Created by gavinandre on 18-3-13.
     * 数据接收线程
     */
    public class SocketReceiveThread extends Thread {
    
        private static final String TAG = SocketReceiveThread.class.getSimpleName();
    
        private volatile String name;
    
        private volatile boolean isCancel = false;
    
        private BufferedReader bufferedReader;
    
        private SocketCloseInterface socketCloseInterface;
    
        private SocketClientResponseInterface socketClientResponseInterface;
    
        public SocketReceiveThread(String name, BufferedReader bufferedReader,
                                   SocketClientResponseInterface socketClientResponseInterface,
                                   SocketCloseInterface socketCloseInterface) {
            this.name = name;
            this.bufferedReader = bufferedReader;
            this.socketClientResponseInterface = socketClientResponseInterface;
            this.socketCloseInterface = socketCloseInterface;
        }
    
        @Override
        public void run() {
            final Thread currentThread = Thread.currentThread();
            final String oldName = currentThread.getName();
            currentThread.setName("Processing-" + name);
            try {
                while (!isCancel) {
                    //if (!isConnected()) {
                    //    break;
                    //}
    
                    if (bufferedReader != null) {
                        String receiverData = SocketUtil.readFromStream(bufferedReader);
                        if (receiverData != null) {
                            successMessage(receiverData);
                        } else {
                            Log.i(TAG, "run: receiverData==null");
                            break;
                        }
                    }
                }
            } finally {
                //循环结束则退出输入流
                SocketUtil.closeBufferedReader(bufferedReader);
                currentThread.setName(oldName);
                Log.i(TAG, "SocketReceiveThread finish");
            }
        }
    
        /**
         * 接收消息回调
         */
        private void successMessage(String data) {
            if (socketClientResponseInterface != null) {
                socketClientResponseInterface.onSocketReceive(data, SocketUtil.SUCCESS);
            }
        }
    
        public void close() {
            isCancel = true;
            this.interrupt();
            if (bufferedReader != null) {
                if (socketCloseInterface != null) {
                    socketCloseInterface.onSocketShutdownInput();
                }
                SocketUtil.closeBufferedReader(bufferedReader);
                bufferedReader = null;
            }
        }
    
    }
    

    SocketSendThread 发送线程

    /**
     * Created by gavinandre on 18-3-13.
     * 数据发送线程,当没有发送数据时让线程等待
     */
    public class SocketSendThread extends Thread {
    
        private static final String TAG = SocketSendThread.class.getSimpleName();
    
        private volatile String name;
    
        private volatile boolean isCancel = false;
        private boolean closeSendTask;
        private final PrintWriter printWriter;
    
        protected volatile ConcurrentLinkedQueue<String> dataQueue = new ConcurrentLinkedQueue<>();
    
        public SocketSendThread(String name, PrintWriter printWriter) {
            this.name = name;
            this.printWriter = printWriter;
        }
    
        @Override
        public void run() {
            final Thread currentThread = Thread.currentThread();
            final String oldName = currentThread.getName();
            currentThread.setName("Processing-" + name);
            try {
                while (!isCancel) {
    
                    String dataContent = dataQueue.poll();
                    if (dataContent == null) {
                        //没有发送数据则等待
                        SocketUtil.toWait(dataQueue, 0);
                        if (closeSendTask) {
                            //notify()调用后,并不是马上就释放对象锁的,所以在此处中断发送线程
                            close();
                        }
                    } else if (printWriter != null) {
                        synchronized (printWriter) {
                            SocketUtil.write2Stream(dataContent, printWriter);
                        }
                    }
                }
            } finally {
                //循环结束则退出输出流
                if (printWriter != null) {
                    synchronized (printWriter) {
                        SocketUtil.closePrintWriter(printWriter);
                    }
                }
                currentThread.setName(oldName);
                Log.i(TAG, "SocketSendThread finish");
            }
        }
    
        /**
         * 发送消息
         */
        public void sendMsg(String data) {
            dataQueue.add(data);
            //有新增待发送数据,则唤醒发送线程
            SocketUtil.toNotifyAll(dataQueue);
        }
    
        /**
         * 清除数据
         */
        public void clearData() {
            dataQueue.clear();
        }
    
        public void close() {
            isCancel = true;
            this.interrupt();
            if (printWriter != null) {
                //防止写数据时停止,写完再停
                synchronized (printWriter) {
                    SocketUtil.closePrintWriter(printWriter);
                }
            }
        }
    
        public void wakeSendTask() {
            closeSendTask = true;
            SocketUtil.toNotifyAll(dataQueue);
        }
    
        public void setCloseSendTask(boolean closeSendTask) {
            this.closeSendTask = closeSendTask;
        }
    }
    

    SocketHeartBeatThread 心跳线程

    /**
     * 心跳实现,频率5秒
     * Created by gavinandre on 18-3-13.
     */
    public class SocketHeartBeatThread extends Thread {
    
        private static final String TAG = SocketHeartBeatThread.class.getSimpleName();
    
        private volatile String name;
    
        private static final int REPEAT_TIME = 5000;
        private boolean isCancel = false;
        private final PrintWriter printWriter;
        private Socket mSocket;
    
        private SocketCloseInterface socketCloseInterface;
    
        public SocketHeartBeatThread(String name, PrintWriter printWriter,
                                     Socket mSocket, SocketCloseInterface socketCloseInterface) {
            this.name = name;
            this.printWriter = printWriter;
            this.mSocket = mSocket;
            this.socketCloseInterface = socketCloseInterface;
        }
    
        @Override
        public void run() {
            final Thread currentThread = Thread.currentThread();
            final String oldName = currentThread.getName();
            currentThread.setName("Processing-" + name);
            try {
                while (!isCancel) {
                    if (!isConnected()) {
                        break;
                    }
    
                    //去除sendUrgentData,防止windows系统下发送多次后断开的问题
                    //try {
                    //    mSocket.sendUrgentData(0xFF);
                    //} catch (IOException e) {
                    //    if (socketCloseInterface != null) {
                    //        socketCloseInterface.onSocketDisconnection();
                    //    }
                    //    break;
                    //}
                    if (printWriter != null) {
                        synchronized (printWriter) {
                            SocketUtil.write2Stream("ping", printWriter);
                        }
                    }
                    //Log.i(TAG, "run: SocketHeartBeatThread");
                    try {
                        Thread.sleep(REPEAT_TIME);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } finally {
                //循环结束则退出输入流
                if (printWriter != null) {
                    synchronized (printWriter) {
                        SocketUtil.closePrintWriter(printWriter);
                    }
                }
                currentThread.setName(oldName);
                Log.i(TAG, "SocketHeartBeatThread finish");
            }
        }
    
        /**
         * 判断本地socket连接状态
         */
        private boolean isConnected() {
            if (mSocket.isClosed() || !mSocket.isConnected() ||
                    mSocket.isInputShutdown() || mSocket.isOutputShutdown()) {
                if (socketCloseInterface != null) {
                    socketCloseInterface.onSocketDisconnection();
                }
                return false;
            }
            return true;
        }
    
        public void close() {
            isCancel = true;
            if (printWriter != null) {
                synchronized (printWriter) {
                    SocketUtil.closePrintWriter(printWriter);
                }
            }
        }
    
    }
    
    

    客户端开启三个线程:接收线程,发送线程和心跳包线程,接收线程无消息时会堵塞在bufferedReader.readLine()处,发送线程无发送消息时阻塞,有发送消息时唤醒,使用ConcurrentLinkedQueue来作为消息队列并将发送线程和心跳包线程的输出流加锁来防止粘包,心跳包线程每隔五秒发送一次,并且进行sendUrgentData(0xFF)操作,判断连接是否断开,去除sendUrgentData,防止windows系统下发送多次后断开的问题(感谢小伙伴发现问题)。
    在initSocket()的catch块内调用initSocket(),这样就能实现第一次连接超过10秒后重连,在stopThread()内调用initSocket()来实现通讯过程中由于网络环境较差导致的连接断开后的重连操作。

    public class SocketClient {
    
        private SocketClientThread socketClientThread;
    
        public SocketClient() {
            socketClientThread = new SocketClientThread("socketClientThread", this);
            new Thread(socketClientThread).start();
        }
    
        @Override
        public void onSocketConnect() {
            Log.i(TAG, "onSocketConnect: 连接成功");
        }
    
        @Override
        public void onSocketReceive(Object socketResult, int code) {
            Log.i(TAG, "onSocketReceive: 收到消息 ,  data: " + socketResult + " , code: " + code);
        }
    
        @Override
        public void onSocketDisable(String msg, int code) {
            Log.i(TAG, "onSocketDisable: 连接断开 , msg: " + msg + " , code: " + code);
        }
    
        public <T> void sendData(T data) {
            //convert to string or serialize object
            String s = (String) data;
            if (TextUtils.isEmpty(s)) {
                Log.i(TAG, "sendData: 消息不能为空");
                return;
            }
            if (socketClientThread != null) {
                socketClientThread.addRequest(s);
            }
        }
    
        public void stopSocket() {
            //一定要在子线程内执行关闭socket等IO操作
            new Thread(() -> {
                socketClientThread.setReConnect(false);
                socketClientThread.stopThread();
            }).start();
        }
    }
    

    服务端

    状态回调接口

    public interface SocketServerResponseInterface {
    
        /**
         * 客户端断线回调
         */
        void clientOffline();
    
        /**
         * 客户端上线回调
         *
         * @param clientIp
         */
        void clientOnline(String clientIp);
    }
    

    服务端代码

    /**
     * Created by gavinandre on 18-2-24.
     */
    public class ServerResponseThread implements Runnable {
    
        private ReceiveThread receiveThread;
        private SendThread sendThread;
        private Socket socket;
        private SocketServerResponseInterface socketServerResponseInterface;
    
        private volatile ConcurrentLinkedQueue<String> dataQueue = new ConcurrentLinkedQueue<>();
        private static ConcurrentHashMap<String, Socket> onLineClient = new ConcurrentHashMap<>();
    
        private long lastReceiveTime = System.currentTimeMillis();
    
        private String userIP;
    
        public String getUserIP() {
            return userIP;
        }
    
        public ServerResponseThread(Socket socket, SocketServerResponseInterface socketServerResponseInterface) {
            this.socket = socket;
            this.socketServerResponseInterface = socketServerResponseInterface;
            this.userIP = socket.getInetAddress().getHostAddress();
            onLineClient.put(userIP, socket);
            System.out.println("用户:" + userIP
                    + " 加入了聊天室,当前在线人数:" + onLineClient.size());
        }
    
        @Override
        public void run() {
            try {
                //开启接收线程
                receiveThread = new ReceiveThread();
                receiveThread.bufferedReader = new BufferedReader(
                        new InputStreamReader(socket.getInputStream(), "UTF-8"));
                receiveThread.start();
    
                //开启发送线程
                sendThread = new SendThread();
                sendThread.printWriter = new PrintWriter(socket.getOutputStream(), true);
                sendThread.start();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 断开socket连接
         */
        public void stop() {
            try {
                System.out.println("stop");
                if (receiveThread != null) {
                    receiveThread.isCancel = true;
                    receiveThread.interrupt();
                    if (receiveThread.bufferedReader != null) {
                        SocketUtil.inputStreamShutdown(socket);
                        System.out.println("before closeBufferedReader");
                        SocketUtil.closeBufferedReader(receiveThread.bufferedReader);
                        System.out.println("after closeBufferedReader");
                        receiveThread.bufferedReader = null;
                    }
                    receiveThread = null;
                    System.out.println("stop receiveThread");
                }
    
                if (sendThread != null) {
                    sendThread.isCancel = true;
                    toNotifyAll(sendThread);
                    sendThread.interrupt();
                    if (sendThread.printWriter != null) {
                        //防止写数据时停止,写完再停
                        synchronized (sendThread.printWriter) {
                            SocketUtil.closePrintWriter(sendThread.printWriter);
                            sendThread.printWriter = null;
                        }
                    }
                    sendThread = null;
                    System.out.println("stop sendThread");
                }
                onLineClient.remove(userIP);
                System.out.println("用户:" + userIP
                        + " 退出,当前在线人数:" + onLineClient.size());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 发送消息
         */
        public void addMessage(String data) {
            if (!isConnected()) {
                return;
            }
    
            dataQueue.offer(data);
            //有新增待发送数据,则唤醒发送线程
            toNotifyAll(dataQueue);
        }
    
        /**
         * 获取已接连的客户端
         */
        public Socket getConnectdClient(String clientID) {
            return onLineClient.get(clientID);
        }
    
        /**
         * 打印已经连接的客户端
         */
        public static void printAllClient() {
            if (onLineClient == null) {
                return;
            }
            Iterator<String> inter = onLineClient.keySet().iterator();
            while (inter.hasNext()) {
                System.out.println("client:" + inter.next());
            }
        }
    
        /**
         * 阻塞线程,millis为0则永久阻塞,知道调用notify()
         */
        public void toWaitAll(Object o) {
            synchronized (o) {
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * notify()调用后,并不是马上就释放对象锁的,而是在相应的synchronized(){}语句块执行结束,自动释放锁后
         */
        public void toNotifyAll(Object obj) {
            synchronized (obj) {
                obj.notifyAll();
            }
        }
    
        /**
         * 判断本地socket连接状态
         */
        private boolean isConnected() {
            if (socket.isClosed() || !socket.isConnected()) {
                onLineClient.remove(userIP);
                ServerResponseThread.this.stop();
                System.out.println("socket closed...");
                return false;
            }
            return true;
        }
    
        /**
         * 数据接收线程
         */
        public class ReceiveThread extends Thread {
    
            private BufferedReader bufferedReader;
            private boolean isCancel;
    
            @Override
            public void run() {
                try {
                    while (!isCancel) {
                        if (!isConnected()) {
                            isCancel = true;
                            break;
                        }
    
                        String msg = SocketUtil.readFromStream(bufferedReader);
                        if (msg != null) {
                            if ("ping".equals(msg)) {
                                System.out.println("收到心跳包");
                                lastReceiveTime = System.currentTimeMillis();
                                socketServerResponseInterface.clientOnline(userIP);
                            } else {
                                msg = "用户" + userIP + " : " + msg;
                                System.out.println(msg);
                                addMessage(msg);
                                socketServerResponseInterface.clientOnline(userIP);
                            }
                        } else {
                            System.out.println("client is offline...");
                            ServerResponseThread.this.stop();
                            socketServerResponseInterface.clientOffline();
                            break;
                        }
                        System.out.println("ReceiveThread");
                    }
    
                    SocketUtil.inputStreamShutdown(socket);
                    SocketUtil.closeBufferedReader(bufferedReader);
                    System.out.println("ReceiveThread is finish");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 数据发送线程,当没有发送数据时让线程等待
         */
        public class SendThread extends Thread {
    
            private PrintWriter printWriter;
            private boolean isCancel;
    
            @Override
            public void run() {
                try {
                    while (!isCancel) {
                        if (!isConnected()) {
                            isCancel = true;
                            break;
                        }
    
                        String msg = dataQueue.poll();
                        if (msg == null) {
                            toWaitAll(dataQueue);
                        } else if (printWriter != null) {
                            synchronized (printWriter) {
                                SocketUtil.write2Stream(msg, printWriter);
                            }
                        }
                        System.out.println("SendThread");
                    }
    
                    SocketUtil.outputStreamShutdown(socket);
                    SocketUtil.closePrintWriter(printWriter);
                    System.out.println("SendThread is finish");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    服务端同样开启个线程:接收线程,发送线程和状态监控线程,接收和发送线程和客户端同理,这里说下状态监控线程,客户端如果在连上服务端的情况下突然断网服务端是不会有任何反应的,因此就要通过客户端的心跳包机制来做超时处理,客户端超过一定的时间后不发心跳包的话就关闭输入流,这样接收线程的bufferedReader.readLine()就会返回null,就会进行断开socket连接操作了(经小伙伴提醒,不需要状态监控线程,只需要使用setSoTimeout设定超时时间即可)。

    public class Main {
    
        private static boolean isStart = true;
        private static ServerResponseThread serverResponseThread;
    
        public static void main(String[] args) {
            ServerSocket serverSocket = null;
            ExecutorService executorService = Executors.newCachedThreadPool();
            System.out.println("服务端 " + SocketUtil.getIP() + " 运行中...\n");
            try {
                serverSocket = new ServerSocket(SocketUtil.PORT);
                while (isStart) {
                    Socket socket = serverSocket.accept();
                    //设定输入流读取阻塞超时时间(10秒收不到客户端消息判定断线)
                    socket.setSoTimeout(10000);
                    serverResponseThread = new ServerResponseThread(socket,
                            new SocketServerResponseInterface() {
    
                                @Override
                                public void clientOffline() {// 对方不在线
                                    System.out.println("offline");
                                }
    
                                @Override
                                public void clientOnline(String clientIp) {
                                    System.out.println(clientIp + " is online");
                                    System.out.println("-----------------------------------------");
                                }
                            });
    
                    if (socket.isConnected()) {
                        executorService.execute(serverResponseThread);
                    }
                }
    
                serverSocket.close();
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (serverSocket != null) {
                    try {
                        isStart = false;
                        serverSocket.close();
                        if (serverSocket != null)
                            serverResponseThread.stop();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    android 基于MVP构架的Socket项目(完整demo)
    https://blog.csdn.net/lj402159806/article/details/79517244

    参考:
    http://blog.csdn.net/u010818425/article/details/53448817

    展开全文
  • dubbo服务端超时情况

    2019-10-29 15:51:04
    https://www.cnblogs.com/xuwc/p/8974709.html
    展开全文
  • haproxy 服务端超时时间: haproxy 配置: timeout server 17000 --后台程序17秒没有响应,返回超时 Jun 27 09:29:56 localhost haproxy[13593]: 192.168.32.101:43552 [27/Jun/2016:09:29:39.002] ...
    haproxy 服务端超时时间:
    
    haproxy 配置:
    timeout server 17000    --后台程序17秒没有响应,返回超时
    
    
    
    Jun 27 09:29:56 localhost haproxy[13593]: 192.168.32.101:43552 [27/Jun/2016:09:29:39.002] www appserver_3000/webhost01_8001 64/0/6/-1/17072 504 194 - - sH-- 0/0/0/0/0 0/0 "GET /admin/api/menu HTTP/1.1"
    Jun 27 09:30:13 localhost haproxy[13593]: 192.168.32.101:43558 [27/Jun/2016:09:29:56.300] www appserver_3000/webhost01_8001 59/0/7/-1/17067 504 194 - - sH-- 0/0/0/0/0 0/0 "GET /admin/api/menu HTTP/1.1"
    
    
    
    
    
    [root@dr-mysql01 test]# cat get.pl 
    use  LWP::UserAgent;
     
    my $ua = LWP::UserAgent->new;
    #$ua->env_proxy('http','http://$ARGV[0]');
    $ua->agent("Mozilla/8.0");
    
    my $response = $ua->get('http://www.zjtest6.com/admin/api/menu');
    
    
    if ($response->is_success) {
     print $response->decoded_content;  # or whatever
    }
    else {
     die $response->status_line;
    }
    
    
    
    [root@dr-mysql01 test]# sh ./a1.sh
    504 Gateway Time-out at ./get.pl line 14.
    Mon Jun 27 09:31:16 CST 2016
    504 Gateway Time-out at ./get.pl line 14.
    Mon Jun 27 09:31:33 CST 2016

    展开全文
  • golang 官方博文对context的主要设计目的和实践作阐述, 本文结合该博文对服务器端实现超时的方法再作汇总。 1 通过设置nginx或后端web server配置实现请求超时 router.HandleFunc("/lazy", lazyHandler) router....
  • Socket中的异常和参数设置 ... 来连接服务器,但是如果服务器无响应的话,客户端就会长时间的无响应,解决方法是设置Socket超时参数,如下: try { Socket mSocket = new Socket(); //实例化socket So...
  •  显然,如题,可以通过统计 nginx 日志中的 499 数量来判断有多少请求超出了客户端设置的超时时间,当然,如果服务端记录了请求的耗时,还可以由此判断客户端的超时时间是多少。  如果要统计超时的数量,在 ...
  • 有哪位大神知道怎么设置weblogic请求超时重发。。看日志发现客户端一个请求过来,这边要处理很久,超过5分钟没响应的话,请求有重新发起了一次,在tomcat本机测试就算设置Thread.sleep(十分钟)也没问题,十分钟过后...
  • mongodb pymongo.errors.CursorNotFound: ... 延长超时时间 需显示的关闭cursor  db.getCollection("unicom_jd").find({}).noCursorTimeout() 转载于:https://www.cnblogs.com/CHWLearningNotes/p/10333479.html
  • 服务端常见超时问题

    2020-10-26 09:13:52
    kafka listener 超时 max.poll.interval.ms 默认5分钟 listener 拉取消息间隔,超过这个时间会引起rebalance,重试,要求我们必须在5分钟内完成,太长时间的不可以用listener 处理,要改用异步job mysql 锁表超时 ...
  • 1.服务器本身有问题,比如接口不通服务挂了等等 2.前段请求是响应超时,自己模拟服务端请求成功,说明设置了超时时间,自己如果模拟比如查询很慢可能就是少索引的问题,可以对比其他环境的索引尝试添加索引就可以了
  • 项目上想通过websocket做好友的上线下线通知功能,用户上线时客户端websocket连接服务端,调用服务端onOpen()方法,服务端通知所有好友当前用户上线;用户退出时客户端websocket断开连接,调用服务端onClose()方法,...
  • <div><p>针对客户端连接,服务端超时限制关闭这些连接,请问这个有样例吗?</p><p>该提问来源于开源项目:smallnest/rpcx</p></div>
  • 我们可以利用jquery.ajax的datafilter ,请保证你的所有...请在公共的header头里面写上这样的js代码 //做一个过滤器处理服务端超时后302的情况 $.ajaxSetup({ dataFilter:function(response){ if(response.
  • 基于JetSpeed2中的Portlet调用平台暴漏的dubbo服务,以前正常运行,突然无法调用,后台报响应超时。 经确认:所有服务、配置、授权均正常; 但是在消费者调用时随机出现部分服务响应超时的情况,重启服务后报错...
  • 那么对于连接成功后,但不发送数据的客户端,应该要踢掉的,但服务端如何检测这种超时客户端呢?单开一个线程扫描? 如果单开线程扫描,发现客户端超时了,准备closesocket,但正在要关闭客户端socket以及释放...
  • 我们经常会遇到远程调用超时的情况,一般这种情况客户端会有一个超时时间,超过该时间,那客户端抛异常,客户端可以进行重试等操作,那此时此刻,服务端呢? 最近遇到了一个线上问题,我的一个服务是文件处理,所有...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,243
精华内容 897
关键字:

服务端超时