gpt4 book ai didi

客户端-服务器餐厅模拟

转载 作者:太空宇宙 更新时间:2023-11-04 04:45:37 26 4
gpt4 key购买 nike

我正在使用 C 中的 select() 制作一个模拟快餐或其他任何东西的客户端服务器。

我有客户随机点 1-5 种“食物”。服务器每 30 秒决定一次。所有客户最喜欢的食物是什么?他为那些客户提供服务,他们关闭连接,其余的,向他们发送消息等待。未服务的客户端再尝试2次,否则他们离开。

我的问题是,如何让服务器每 30 秒检查一次。他们的命令是什么?

我尝试制作一个数组,但我不知道如何让服务器每 30 秒连续“检查”一次。然后将数组设置为0。

伪代码如下:

**client**

patience=0;served=0;
do
{send random 1-5
receieve message. if 1->served=1; if 0, patience++;
}while patience !=3 and served!=1;

if served==1 send -1
else send -2
close connection

**Server**
while(1)
{
serves clients in a concurent manner
select
adds client in socket list
serves client


waits message
if 1-5 adds in vector
//here I don't know how to make it wait for 30 sec.
//If I put sleep(30), it's going to sleep for each client every time. I want it to permanently check every 30 sec and send messages to clients.
send respones(0 or 1, depending on if the max order is the same as the client's)

if -1, thanks for the food
if -2, going somewhere else

close client connection
}

最佳答案

您可能想在循环中尝试 sleep() 函数,它将做的是暂停您的程序那么长的时间,然后执行后面的语句。

sleep(30);

我想这就是你想要的。看看here了解更多关于 sleep 的信息。

你的代码应该是这样的:

for(i=0; i<=100; i++)//Checks 100 times
{
sleep(30);
checkserverforupdate();

}

更新代码:

while(1){//runs infinite times
sleep(30);//pauses here for 30 seconds everytime before/after running the following code

*client**

patience=0;served=0;
do
{send random 1-5
receieve message. if 1->served=1; if 0, patience++;
}while patience !=3 and served!=1;

if served==1 send -1
else send -2
close connection

**Server**
while(1)
{
serves clients in a concurent manner
select
adds client in socket list
serves client


waits message
if 1-5 adds in vector
//here I don't know how to make it wait for 30 sec.
//If I put sleep(30), it's going to sleep for each client every time. I want it to permanently check every 30 sec and send messages to clients.
send respones(0 or 1, depending on if the max order is the same as the client's)

if -1, thanks for the food
if -2, going somewhere else

close client connection
}
}

看看 sleep(30) 会发生什么,程序会暂停 30 秒,然后执行其后编写的代码,如果将其放在 while 循环中,它每次都会等待。

更新 2:

C 中获取当前时间的代码:

here

/* localtime example */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );

return 0;
}

更新 3:所以你的代码应该是这样的:

  time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );


All of your code here

and then get the time again,
time_t rawtime1;
.....

and then you may calculate the difference between them and put a sleep statement as you wish. If the time you want it to pause is in x then,

sleep(x);

return 0;

关于客户端-服务器餐厅模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21202564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com