10K终端的服务程序问题

qq_35597957 2017-12-02 04:06:51
开发一个通信服务器,要求保持10K终端的长连接,终端每10s向服务器发送1次数据,数据长度在100个字节左右;服务运行在linux上的Java程序。现在遇到的问题是,服务端接收这10k终端的数据花费的时间会慢慢增长。是服务端有什么需要配置的地方还是程序里有什么问题?
服务端代码:

private void run()
{
while (mbIsRunning)
{
try
{
if (mbSelector.select(2000) == 0) continue;
}
catch (Exception e)
{

}
Iterator<SelectionKey> iterator = mbSelector.selectedKeys().iterator();
SelectionKey key;
while (iterator.hasNext())
{
key = iterator.next();
iterator.remove();
if (key.isAcceptable()) handleAccept(key);
else if (key.isReadable()) handleReceive(key);
}
}
depose();
}


private void handleReceive(SelectionKey paKey)
{
if (tpCount % 10000 == 0) tpSTime = System.currentTimeMillis();
SocketChannel channel = (SocketChannel)paKey.channel();
int readBytes;
try
{
if (!channel.isConnected()) return;
mbReadBuffer.clear();
readBytes = channel.read(mbReadBuffer);
if (readBytes > 0)
{
mbReadBuffer.flip();
byte[] bytes = new byte[mbReadBuffer.remaining()];
mbReadBuffer.get(bytes);
String data = new String(bytes, "UTF-8");
++tpCount;
if (tpCount % 10000 == 0)
{
System.out.println(String.format("Received 10000 data : %d",
System.currentTimeMillis() - tpSTime));
}
}
}
catch (Exception e)
{
}
}


目前只是读取数据,并不做处理。
...全文
277 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_35597957 2017-12-02
  • 打赏
  • 举报
回复

4,357

社区成员

发帖
与我相关
我的任务
社区描述
通信技术相关讨论
社区管理员
  • 网络通信
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧