简体   繁体   English

使用C定期从客户端向服务器发送数据

[英]Sending data periodically from client to server in C

I am creating a multithreaded socket program that needs to send 2 sets of data to the server periodically. 我正在创建一个多线程套接字程序,该程序需要定期向服务器发送2组数据。 The client needs to send GPS information every 3 seconds and polarity information every 4 seconds. 客户端需要每3秒发送一次GPS信息,每4秒发送一次极性信息。 My idea on implementing this was to simply create a new thread, make a infinite loop, send() the GPS information, sleep() for 1 second, send() the polarity information, and then sleep() for 2 seconds. 我实现此目标的想法是简单地创建一个新线程,进行无限循环,发送()GPS信息,休眠(1秒钟),发送()极性信息,然后休眠(2秒)。 Would this be a sufficient way of going about this? 这将是解决此问题的足够方法吗? Sorry, I'm new to socket programming so I don't know of any alternative methods. 抱歉,我是套接字编程的新手,所以我不知道任何替代方法。

Your proposed scheme is: 您建议的方案是:

send the GPS information, sleep for 1 second, send the polarity information, and then sleep for 2 seconds 发送GPS信息,睡眠1秒钟,发送极性信息,然后睡眠2秒钟

Over a period of 12 seconds, that leads to: 在12秒内,将导致:

  1. GPS; 全球定位系统; sleep 1 睡觉1
  2. polarity; 极性; sleep 1 睡觉1
  3. sleep 1 睡觉1
  4. GPS; 全球定位系统; sleep 1 睡觉1
  5. polarity; 极性; sleep 1 睡觉1
  6. sleep 1 睡觉1
  7. GPS; 全球定位系统; sleep 1 睡觉1
  8. polarity; 极性; sleep 1 睡觉1
  9. sleep 1 睡觉1
  10. GPS; 全球定位系统; sleep 1 睡觉1
  11. polarity; 极性; sleep 1 睡觉1
  12. sleep 1 睡觉1

This sends the GPS information 4 times (correct) and the polarity 4 times, and every 3 seconds, not 3 times and every 4 seconds as the specification requires. 这会将GPS信息发送4次(正确),并发送4次极性,并且每3秒发送一次,而不是规范要求的3次和每4秒发送一次。 The desired sequence might be: 所需的顺序可能是:

  1. GPS; 全球定位系统; polarity; 极性; sleep 1 睡觉1
  2. sleep 1 睡觉1
  3. sleep 1 睡觉1
  4. GPS; 全球定位系统; sleep 1 睡觉1
  5. polarity; 极性; sleep 1 睡觉1
  6. sleep 1 睡觉1
  7. GPS; 全球定位系统; sleep 1 睡觉1
  8. sleep 1 睡觉1
  9. polarity; 极性; sleep 1 睡觉1
  10. GPS; 全球定位系统; sleep 1 睡觉1
  11. sleep 1 睡觉1
  12. sleep 1 睡觉1

Note that in some second, both GPS and polarity must be sent. 请注意,在一秒钟内,必须同时发送GPS和极性。

You can achieve this in either of two ways. 您可以通过两种方式之一来实现。 Assuming you need to use threads at all, use two threads, one on a three second wait, one on a four second wait. 假设您根本需要使用线程,请使用两个线程,一个线程等待三秒钟,一个线程等待四秒钟。 Coordinate access to the socket with a mutex so that on multiples of 12 seconds, you still get sane behaviour. 使用互斥锁协调对套接字的访问,以便在12秒的倍数上,您仍然可以保持理智的行为。

However, you could do it all with a single-threaded process that waits appropriate intervals: 但是,您可以通过等待适当间隔的单线程进程来完成所有操作:

int i_gps = 3;
int i_pol = 4;
int t_gps = 0;
int t_pol = 0;
int t_cur = 0;

while (1)
{
    if (t_cur == t_gps && t_cur == t_pol)
    {
        t_cur = t_gps = t_pol = 0;  // Avoid integer overflow
    }
    if (t_cur == t_gps)
    {
        send GPS
        t_gps += i_gps;
    }
    if (t_cur == t_pol)
    {
        send polarity
        t_pol += i_pol;
    }
    sleep 1;
    t_cur++;
}

You can also sleep for multiple seconds by altering the code at the end of the loop to determine which send comes next and sleeping for the appropriate amount of time and incrementing t_cur by the appropriate amount of time (rather than just using 1 second all the time). 您还可以通过更改循环末尾的代码来确定接下来发送的内容并休眠适当的时间量,然后将t_cur增加适当的时间量(而不是一直使用1秒)来休眠t_cur 。 )。

If you need to avoid drift (because the code above assumes that the send process is instantaneous and it isn't), then you have to use sub-second sleeps and trap the time at the start of the loop and adjust the sleep interval to allow for the time taken to do the sending. 如果您需要避免漂移(因为上面的代码假定发送过程是瞬时的并且不是瞬时的),那么您必须使用亚秒级睡眠并在循环开始时捕获时间,并将睡眠间隔调整为留出时间进行发送。

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

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