简体   繁体   English

通过套接字连续数据

[英]continuous data through a socket

lets say I had a socket that needed to send continuous data at random (but small) intervals, lets say about 20 objects a second, for any span of time. 假设我有一个需要以随机(但很小)的间隔发送连续数据的套接字,可以说任何一段时间内每秒约20个对象。

I am for-seeing possible issues that I am not sure how to handle. 我是为了 - 看到可能的问题,我不知道如何处理。

1) If I send one object at a time as in example A, may they still arrive in bunches? 1)如果我像例A中一次发送一个对象,它们是否仍然可以串联到达? So as to make it better to do as in example B? 为了使其更好地像例子B那样做?

2) Would the thread receiving the data possibly try to read the data before a entire object was sent, thus splitting data, and making another issue I will have to look out for? 2)接收数据的线程是否可能尝试在发送整个对象之前读取数据,从而分割数据,并提出另一个我需要注意的问题?

pseudo for sending data might look like this 伪发送数据可能如下所示

EXAMPLE A 例A

void run()
{
    get socket outputstream
    while (isBroadcasting)
    {
        if (myQueue.isEmpty() == false)
            send first object in queue through outputstream

        Thread.sleep(25);
    }
}

EXAMPLE B 例B

void run()
{
    get socket outputstream
    while (isBroadcasting)
    {
        while (myQueue.isEmpty() == false)
            send all object in queue through outputstream

        Thread.sleep(25);
    }
}

and finally read it like this 最后像这样读了

void run()
{
    get socket inputstream
    while (isReceiving)
    {
        get object(s) from inputstream and publish them to main thread

        Thread.sleep(25);
    }
}

3) Would this be a viable solution? 3)这是一个可行的解决方案吗? Is it ok to keep the streams open at both ends, looping and writing/reading data until finished? 是否可以保持两端的流打开,循环和写入/读取数据直到完成?

1) If I send one object at a time as in example A, may they still arrive in bunches? 1)如果我像例A中一次发送一个对象,它们是否仍然可以串联到达? So as to make it better to do as in example B? 为了使其更好地像例子B那样做?

Depending on the type of network you are sending them over, they may definitely still arrive in bunches at the receiving end. 根据您发送的网络类型,它们肯定仍然可以在接收端到达。 In addition, if you are using TCP, the application of Nagle's algorithm may introduce more bunching and apparent delay. 此外,如果您使用的是TCP, Nagle算法的应用可能会引入更多的聚束和明显的延迟。

2) Would the thread receiving the data possibly try to read the data before a entire object was sent, thus splitting data, and making another issue I will have to look out for? 2)接收数据的线程是否可能尝试在发送整个对象之前读取数据,从而分割数据,并提出另一个我需要注意的问题?

This is very dependent on your implementation details, and is impossible to answer with the pseudocode provided. 这非常依赖于您的实现细节,并且无法使用提供的伪代码来回答。

Is it ok to keep the streams open at both ends, looping and writing/reading data until finished? 是否可以保持两端的流打开,循环和写入/读取数据直到完成?

Yes, it is perfectly reasonable to use a TCP connection like this for a long period of time. 是的,长时间使用这样的TCP连接是完全合理的。 However, your application must be willing to reconnect if the connection is lost for some reason. 但是,如果由于某种原因连接丢失,您的应用程序必须愿意重新连接。

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

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