简体   繁体   中英

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.

It means I connect and disconnect 25 times every 200 ms.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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