简体   繁体   中英

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.

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?

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.

---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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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