Winsock循环直到主机联机

Winsock looping until host comes online

本文关键字:主机 联机 循环 Winsock      更新时间:2023-10-16

我有一个客户端,它基本上想(不时)连接到远程主机,发送一些消息并断开连接。但主机可能并不总是可用的。我如何设置一个执行类似操作的循环(最好避免一些持续的cpu耗时循环):

尝试发送数据(数据数据)


主机在线吗:再次检查是:连接,传输此数据,断开连接,返回

(我使用的是C++、WinSock2和TCP协议)

在一个完全无关的注释上,但由于你删除另一个问题时我正在处理它,我将把它发布在这里,并在你有时间看一看后立即删除。

对于其他读者来说,是的,这不是这个问题的主题,但也许我们需要某种消息传递系统

首先,重构getInterSection:

# This return 2, 3 or 4 items, depending on the position
def getInterSection(x,y,grid):
    intersections = []
    if y > 0:
        intersections.append((x, y - 1))
    if y < grid[1] - 1:
        intersections.append((x, y + 1))
    if x > 0:
        intersections.append((x - 1, y))
    if x < grid[0] - 1:
        intersections.append((x + 1, y))
    return intersections

像这样使用:

intersect = getInterSection(x,y,grid)
# This is a uniform choice, therefore probability is as expected 1/2, 1/3 or 1/4
x,y=random.choice(intersect)