UDP server can't send packet to the client

Viewed 26

I have two computer(A and B) in the different VLAN.

the IP of the computer A server is x.x.180.70 and IP of the computer B is x.x.181.52.

the computer A bind 19999 port as the UDP server and B as the client.

A can recv the packet from B, but B can't recv the packet from A.

so i capture the packet with wireshark, the results shown that the server reply packet with another port. In common the server reply packet with the bound port. Why does the server use the different port?

Image A

Then I wrote a TCP Server on computer A, it works well. enter image description here

The udp server works if A and B at the same vlan. Is there any body have idea? thanks!

Code(Python):

#!/usr/bin/python3
#coding=utf-8

import socket
import sys

def server(addr):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(addr)
    
    print("server started, listen on %s" % str(addr))
    
    while True:
        print("wait msg....")
        data, cconn = sock.recvfrom(1024)
        client_addr = "%s:%d" % (cconn[0], cconn[1])
        resp = "hello client"
        print("recv msg from %s <- %s" % (client_addr, data.decode('utf-8')))
        sock.sendto(resp.encode("utf-8"), cconn)
        print("send msg to %s -> %s" % (client_addr, resp))

def client(addr):
    server_addr = "%s:%d" % (addr[0], addr[1])
    print("send msg to %s" % server_addr)
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.connect(addr)
    sock.sendto("hello server".encode("utf-8"), addr)

    data, conn = sock.recvfrom(1024)
    resp = data.decode("utf-8")
    print("recv msg from %s <- %s" % (server_addr, resp))

def usage():
    print("Usage:")
    print("  python3 udp.py server [ip:port]")
    print("  python3 udp.py client [ip:port]")

if __name__ == "__main__":
    if len(sys.argv) < 3:
        usage()
        exit(0)

    role = sys.argv[1]
    array = sys.argv[2].split(":")
    address = (array[0], int(array[1]))

    if role == "server":
        server(address)
    elif role == "client":
        client(address)
    else:
        usage()
0 Answers
Related