首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UDP客户端每秒发送一次ping,并打印发送到它的任何内容?

UDP客户端每秒发送一次ping,并打印发送到它的任何内容?
EN

Stack Overflow用户
提问于 2018-05-28 03:22:37
回答 1查看 1.7K关注 0票数 0

各位读者下午好,我不熟悉套接字编程,也不熟悉异步编程(我觉得异步可能是解决我问题的一部分),所以请原谅我犯下的任何愚蠢的错误。

首先,我有一个UDP Echo服务器作为游戏服务器。每当它收到ping请求时,它就会将源ip和端口添加到“已连接客户端”列表中,并将该确切的ping发送给列表中除发送者之外的所有人。这工作得相当好,因为它在收到消息时会做出反应,所以它总是可以只监听。然而,客户端的问题是,我需要不断地发送ping,同时也在监听。

这是我的客户端当前的样子:

代码语言:javascript
复制
import socket
from time import sleep
from contextlib import contextmanager

UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT_NO = 14004
Message = b"Hello World, From Client B"


@contextmanager
def socket_ctx():
    """ Context Manager for the socket. Makes sure the socket will close regardless of why it exited."""
    my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # Assign IP address and a RANDOM available port number to socket
    my_socket.bind(('127.0.0.1', 0))
    try:
        # Let the rest of the app use the socket and wait for it to finish
        yield my_socket
    finally:
        my_socket.close()


def send_data(client_sock):
    client_sock.sendto(Message, (UDP_IP_ADDRESS, UDP_PORT_NO))


def listen(client_sock):
    print(client_sock.recvfrom(100))



with socket_ctx() as sock:
    while True:
        send_data(sock)
        listen(sock)
        sleep(2)

目前,它只发送一次ping,然后空闲,因为它可能正在侦听。如果碰巧收到了ping,比如说,另一个客户端向服务器发送了一个ping,而服务器将ping发送给了这个客户端,它就会听到它,打印它,然后再次开始循环。问题是,如果没有另一个客户端发送一些东西来使这个客户端脱离侦听,它就不会发送ping。

我认为异步可能是我的解决方案,但我不知道如何去做。有谁有解决这个问题的办法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-28 09:52:27

下面是我如何实现一个具有“接收和处理传入的UDP套接字,以及每秒发送一次数据包”行为的服务器。注意,这使用了select()函数来多路复用两个任务,而不是异步I/O;希望这是可以的。

代码语言:javascript
复制
import socket
import select
import time

UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT_NO = 14004
Message = b"Hello World, From Client B"

udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.bind(('127.0.0.1', 0))
print "UDP socket is listening for incoming packets on port", udp_socket.getsockname()[1]

# When we want to send the next periodic-ping-message out
nextPingTime = time.time()

while True:
   secondsUntilNextPing = nextPingTime - time.time();
   if (secondsUntilNextPing < 0):
      secondsUntilNextPing = 0

   # select() won't return until udp_socket has some data
   # ready-for-read, OR until secondsUntilNextPing seconds 
   # have passed, whichever comes first
   inReady, outReady, exReady = select.select([udp_socket], [], [], secondsUntilNextPing)

   if (udp_socket in inReady):
      # There's an incoming UDP packet ready to receive!
      print(udp_socket.recvfrom(100))

   now = time.time()
   if (now >= nextPingTime):
      # Time to send out the next ping!
      print "Sending out scheduled ping at time ", now
      udp_socket.sendto(Message, (UDP_IP_ADDRESS, UDP_PORT_NO))
      nextPingTime = now + 1.0   # we'll do it again in another second
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50555713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档