小编Dar*_*ius的帖子

如何检测我是否有东西要从套接字读取?(c++)

我试图在一定时间内收到客户的答复。我怎么能那样做呢?我的意思是我有那个代码

 unsigned secondsElapsed = 0;
    while(secondsElapsed <= TIMER){
        char tBuffer[32];

    if (recv(clientSocket, tBuffer, sizeof(tBuffer), MSG_PEEK | MSG_DONTWAIT) == 0){
            myPlayer->dcPlayer();
            \\ More stuff to do if player is dissconected
    \\ But if is not dc, and is typing, how can i check my socket to see
    \\ if i have an answer there to read, else i`ll increment 
    \\ secondsElapsed until is equal to TIMER or until i get an answer    
    \\ from my client.

    usleep(1000000);
    secondsElapsed++;
    }
Run Code Online (Sandbox Code Playgroud)

所以,问题是:我如何检查我的客户是否给我发送了答案?如果我尝试阅读,那么我的程序将被卡住,并且我将无法增加已用秒数。

c++ sockets

3
推荐指数
1
解决办法
5124
查看次数

如何在不阻塞输入的情况下使用getline?

是否有任何方法可以调用getline(),如果没有输入,就不会阻塞并等待?

我有以下代码:

 while(true){
    if(recv(sd, tBuffer, sizeof(tBuffer), MSG_PEEK | MSG_DONTWAIT) > 0) break;
    getline(cin,send);
 }
Run Code Online (Sandbox Code Playgroud)

我想等待输入,但是如果我在sd套接字处接收到一些数据,我想停止等待数据并退出while。我的代码现在停留在的第一次迭代中getline()。我要评估getline(),如果没有可用的输入,请返回if

这可能吗?

PS:我尝试过使用cin.peek(),但是这也阻止了输入。

c++ nonblocking getline

3
推荐指数
1
解决办法
1802
查看次数

在 Haskell 中的列表末尾添加一个元素

我是 Haskell 的初学者,我正在尝试在列表的末尾添加一个元素。

我输入一个列表,如 [1,2,3,4] 和一个数字 10。我想要这样的输出 [1,2,3,4,10]。

我的代码:

func a [] = a
func a (x:xs) = x : func a (x:xs)
Run Code Online (Sandbox Code Playgroud)

但我收到了那个错误:

Non type-variable argument in the constraint: Num [a]
(Use FlexibleContexts to permit this)
When checking that ‘it’ has the inferred type
it :: forall a. (Num a, Num [a]) => [a]
Run Code Online (Sandbox Code Playgroud)

谁能帮忙解释一下?

haskell

3
推荐指数
2
解决办法
3万
查看次数

标签 统计

c++ ×2

getline ×1

haskell ×1

nonblocking ×1

sockets ×1