简体   繁体   English

套接字编程Java

[英]Socket Programming Java

I am trying to send a set of values (~25) every 200 ms but the project is currently set up in a way that I have to connect to a socket(same port) for each value, send the message and disconnect from it. 我正在尝试每200毫秒发送一组值(〜25),但是该项目当前的设置方式是,我必须为每个值连接到一个套接字(相同的端口),发送消息并从中断开连接。

It means I connect and disconnect 25 times every 200 ms. 这意味着我每200毫秒连接和断开25次。

It is possible to have such a high frequency of connections? 可能有这么高的连接频率吗? Is there a limit to this? 这有限制吗?

here is the pseudo code 这是伪代码

func(ByteBuffer packet)
{
-------

if ( packet != null )
    {
        synchronized( tcpClientConnection)
        {
            if ( tcpClientConnection.connect() )
            {
                retval = tcpClientConnection .send( buf );
            }

            tcpClientConnection.disconnect();
        }
    }
-----
}

Try to wrap your connection in what your doing. 尝试将您的连接包装在您的工作中。 Something like 就像是

class...
   establish connection
   do your work/updates/etc
   disconnect connection

Or you can use simply store all the values you want to update in a local variable and then when your program is done you can upload all the data at once. 或者,您可以只将要更新的所有值存储在局部变量中,然后在完成程序后就可以一次上传所有数据。

Those are the two ways I have been approaching similar problems lately. 这就是我最近处理类似问题的两种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM