简体   繁体   English

Python:socket.recvfrom()为地址返回none

[英]Python: socket.recvfrom() returning none for address

I am trying to write a Python version of the example here , but for some reason for every call to socket.recvfrom() that I make in both client and server, the address return value is None . 我想写的例子的Python版本在这里 ,但由于某种原因每次调用socket.recvfrom()我做在客户端和服务器的address返回值是None The only thing I can think of is that maybe it has something to do with the fact that the sockets are STREAM sockets, however when I tried to change the type to socket.SOCK_DGRAM , I get errors on my call to socket.listen() . 我唯一能想到的是它可能与套接字是STREAM套接字这一事实有关,但当我尝试将类型更改为socket.SOCK_DGRAM ,我在调用socket.listen()遇到错误。 How can I fix this problem? 我该如何解决这个问题?

def server(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('127.0.0.1', port))
    sock.listen(5)
    clientsocket, address = sock.accept()

    kkp = KnockKnockProtocol(); #Class which implements knock knock
    outputLine = kkp.processInput();
    clientsocket.sendto(outputLine, address)

    inputLine, address = clientsocket.recvfrom(MAX_BYTES)

    while inputLine:
        print inputLine, address
        outputLine = kkp.processInput(inputLine);
        clientsocket.sendto(outputLine, address)
        if outputLine == "Bye.":
            break
        inputLine, address = clientsocket.recvfrom(MAX_BYTES)


def client(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(("127.0.0.1", port))
    fromServer, address = sock.recvfrom(MAX_BYTES)

    while fromServer:
        print "Server: " + fromServer
        if fromServer == "Bye.":
            break

        fromUser = raw_input()
        if fromUser:
            print "Client: " + fromUser
            sock.sendto(fromUser, ("127.0.0.1", port))
        fromServer, address = sock.recvfrom(MAX_BYTES)


if __name__ == '__main__':
    choices = {'client': client, 'server': server} 
    parser = argparse.ArgumentParser(description='Send and receive UDP locally')
    parser.add_argument('role', choices=choices, help='which role to play')
    parser.add_argument('-p', metavar='PORT', type=int, default=1060, help='UDP port (default 1060)')
    args = parser.parse_args()
    function = choices[args.role]
    function(args.p)

Either use datagram sockets or stream sockets. 使用数据报套接字或流套接字。 You are trying to split the difference. 你正试图分裂差异。 Which do you want to use? 你想用哪个? If you want a connection-based, byte-stream protocol, use stream sockets. 如果需要基于连接的字节流协议,请使用流套接字。 If you want a connectionless, datagram protocol, use datagram sockets. 如果您需要无连接的数据报协议,请使用数据报套接字。

If you want to use stream sockets, don't use recvfrom . 如果要使用流套接字,请不要使用recvfrom It's connection based and the only thing you can receive is bytes from that particular connection. 它是基于连接的,唯一可以接收的是来自该特定连接的字节。

If you want to use datagram sockets, don't use listen or accept . 如果要使用数据报套接字,请不要使用listenaccept Since it's connectionless, you can neither listen for nor accept a connection. 由于它是无连接的,你既不能听也不能接受连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM