5000同时连接消息池HCC_Server.7z

  • z1_676551
    了解作者
  • 5.2KB
    文件大小
  • 7z
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-12 01:48
    上传日期
找资源的时候找到的不过是java的,本人用的c++所以没用上,在这里共享给大家看看有需要的兄弟没有
5000同时连接消息池HCC_Server.7z
内容介绍
package com.supcon.hcc.connector; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; public class HccConnector { boolean flag; Socket socket; long lastTime = 0; BufferedReader input; BufferedWriter out; int buffSize; public HccConnector(Socket socket,int buffSize){ this.socket = socket; this.buffSize = buffSize; try { input = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); lastTime = System.currentTimeMillis(); flag = true; } catch (IOException e) { e.printStackTrace(); } } public String getMSG(){ boolean ready = false; char[] msg = new char[1024]; try { ready = input.ready(); if(ready){ input.read(msg); } if(ready){ lastTime = System.currentTimeMillis(); }else{ if((System.currentTimeMillis() - lastTime) > 5000){ flag = false; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return new String(msg); } public void sendMSG(String msg){ try { out.write(msg); out.flush(); } catch (IOException e) { e.printStackTrace(); } } public void close(){ System.out.println("close"); try { input.close(); out.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); }finally{ input = null; out = null; socket = null; } } public boolean getFlag(){ return flag; } }
评论
    相关推荐