简体   繁体   English

UDP服务器和客户端无法发送和接收消息

[英]UDP Server and Client Failing to Send And Recieve Messages

I am building a simple network chat in Python using UDP, however, when I run the server code on one machine and the client on another, no message is received by the server and no message is sent back to the client by the server script. 我正在使用UDP在Python中建立一个简单的网络聊天,但是,当我在一台计算机上运行服务器代码而在另一台计算机上运行客户端时,服务器未收到任何消息,服务器脚本也未将任何消息发送回客户端。 Here is my code: 这是我的代码:

Server: 服务器:

import socket, sys

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sock.bind(('', 9997)) #need higher port

while True:

    x = raw_input("Enter your message: ")
    sent = sock.sendto(x, ('', 9997))
    data, address = sock.recvfrom(4096)
    print data, " ", address

sock.close()

Client: 客户:

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

while True:
    print "Waiting to receive"
    data, server = sock.recvfrom(4096)
    print data
    x = raw_input("Enter message: ")
    sent = sock.sendto(x, server)

sock.close()

Does anyone know what I am doing wrong here? 有人知道我在这里做错了吗? Is is possible that code is fine, but the UDP is not reliable enough and is dropping the message? 代码是否可能很好,但是UDP不够可靠并且正在丢弃消息?

As I said, since your code seems a little unclear (to me, at least), I'm posting you a very similar working example. 正如我所说,由于您的代码似乎还不清楚(至少对我而言),所以我向您发布了一个非常相似的工作示例。
Here's the Server : 这是服务器

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('localhost', 1932)
sock.bind(server_address)

BUFFER_SIZE = 4096
try:
    while True:
        data, address = sock.recvfrom(BUFFER_SIZE)
        print "Client sends: ", data
        reply = raw_input("Your response:\n")
        sock.sendto(reply,address)
except KeyboardInterrupt:
    sock.close()

The server creates a socket and binds it to its address and the port it's listening to, 1932 in our case. 服务器创建一个套接字并将其绑定到它的地址和它正在侦听的端口,在本例中为1932。 He waits for an incoming message, asks for a reply, then sends it back to the sender. 他等待传入的消息,要求答复,然后将其发送回发件人。
Here's the Client : 这是客户

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_address = ('localhost', 1931)

server_address = ('localhost', 1932)

sock.bind(client_address)

BUFFER_SIZE = 4096
try:
    first_msg = raw_input("Your first message:\n")
    sock.sendto(first_msg,server_address)
    while True:
        data, address = sock.recvfrom(BUFFER_SIZE)
        print "Client sends: ", data
        reply = raw_input("Your response:\n")
        sock.sendto(reply,address)
except KeyboardInterrupt:
    sock.close()

It's very similar to the server, the only difference is that it sends a message before the while loop, in order to start the conversation. 它与服务器非常相似,唯一的区别是,它在while循环之前发送了一条消息,以启动对话。 Then it just enters the receive/reply loop, just as the server does. 然后,它就像服务器一样进入receive/reply循环。 It has the server address too, that is different (different port, since I'm on localhost) 它也具有服务器地址,这是不同的(端口不同,因为我在本地主机上)
The try/catch block is here just to close gracefully the whole process. 这里的try/catch块只是用来优雅地关闭整个过程。
I used localhost and different ports on my computer and tested it, and it works. 我在计算机上使用了localhost和其他端口并对其进行了测试,并且可以正常工作。 You should just change the addresses to get it working over LAN, and you could keep the same port if the addresses are different, it should work. 您只需更改地址即可使其通过LAN工作,并且如果地址不同,则可以保留相同的端口,它应该可以工作。

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

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