Winsock looping until host comes online

60 Views Asked by At

I have a client that basically wants to (from time-to-time) connect to a remote host send some messages and disconnect. But the host might not always be available. How do I set up a loop that does something like (preferably avoiding some constant cpu-time-consuming loops):

Attempt_Send_Data(DATA data)


Is the host online? No: Check again Yes: Connect, Transmit this data , Disconnect, return

(I am using C++ , WinSock2 and the TCP protocol)

1

There are 1 best solutions below

2
On

On a totally unrelated note, but since I was working on it when you deleted your other question, I will post it here, and deleted as soon as you have had the time to take a look at it.

For other readers, yes, this is off-topic for this question, but maybe we need a messaging system of some sort.

for starters, refactor your 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

Use like so:

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)