简体   繁体   English

UDP连接不会收到来自服务器的任何回复-Python(可能还会使用boost的c ++)

[英]UDP connection do not receive any reply from server - Python (potentially also c++ using boost)

I am trying to establish a connection to a server, and send some data to it.. The problem is that, if i try to debug the connection using this MICHAEL SIEGENTHALER | 我正在尝试建立与服务器的连接,并将一些数据发送给它。 TCP/UDP Debugging Tools which clearly shows that there is no issue with the communication, and even some form of random input will result in a data coming out. TCP / UDP调试工具清楚地表明通信没有问题,甚至某种形式的随机输入也将导致数据输出。

but when i try to code it in python, using the same settings, are no response received.. It stalls after it has sent the message, i am not sure whether whether it has send the message, or skipped it? 但是,当我尝试使用相同的设置在python中对其进行编码时,没有收到任何响应。.它在发送消息后停顿了,我不确定是否已发送消息,还是跳过了它?

It seems like my server aren't receiving the message i sent to it, and therefore don't reply.. but what is different? 好像我的服务器没有收到我发送给它的消息,因此没有回复..但是有什么不同?

import socket   #for sockets
import sys  #for exit

# create dgram udp socket
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error:
    print ('Failed to create socket')
    sys.exit()

host = 'localhost';
port = 5634;

while(1) :
    try :
        #Set the whole string
        s.sendto(("-1-117230").encode('utf-8'),('10.2.140.183', 9008))
        print("sent")

        # receive data from client (data, addr)
        d = s.recvfrom(1024)
        reply = d[0]
        addr = d[1]

        print ('Server reply : ' + reply)

    except socket.error as msg:
        print ('Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
        sys.exit()

在此处输入图片说明

what is different from the code, and the way the debugging tool test it? 与代码有什么不同,以及调试工具对其进行测试的方式?

I tried to code it in c++ using boost, but as i had the same issue, i went on to trying in python to see whether that would make a bit more sense. 我试图使用boost在c ++中对其进行编码,但是由于我遇到了同样的问题,我继续尝试使用python来查看这是否更有意义。

---Updated -- - -更新 -

import socket   #for sockets
import sys  #for exit

# create dgram udp socket
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_adress = ('10.2.140.183',5634)
    s.bind(server_adress)
except socket.error:
    print ('Failed to create socket')
    sys.exit()


while(1) :
    try :
        #Set the whole string
        s.sendto(("-1-117230").encode('utf-8'),('10.2.140.183', 9008))
        print("sent")

        # receive data from client (data, addr)
        d = s.recvfrom(1024)
        reply = d[0]
        addr = d[1]

        print ('Server reply : ' + reply)

    except socket.error as msg:
        print ('Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
        sys.exit()

You are missing the binding method. 您缺少绑定方法。

This is kind of an echo server: 这是一种回显服务器:

import socket
import sys

host = ''  
port = 8888
buffersize = 1
server_address = (host, port) 
socket_UDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) 

socket_UDP.bind(server_address)

while True:
    data, from_address = socket_UDP.recvfrom(buffersize)
    if data:
        socket_UDP.sendto(bytes("b"*buffersize, "utf-8"), from_address)
socket_UDP.close()

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

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