Posts: 4,653
Threads: 1,496
Joined: Sep 2016
Dec-20-2016, 06:57 AM
(This post was last modified: Dec-20-2016, 06:58 AM by Skaperen.)
my next little project is to make a "what is my IP address" server, but with UDP and my code (a server and a client) on each end. so i need to set up a little server that can get the source IP from the UDP datagram it receives and send a UDP response back with the IP address in the data payload as well as the destination address. i do not need to do IPv6 for this, though i may need to do so some day in the future.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
Dec-22-2016, 03:29 AM
(This post was last modified: Dec-22-2016, 03:29 AM by Skaperen.)
when a process receives a UDP datagram, it needs to knew the address it came from, as translated, so it can send a response UDP datagram back to the sender. if that address is also included in the data payload of the response then the sender can learn its public-visible address.
the API for receiving and sending UDP datagrams needs to enable this. that's why
socket.sendto( string, flags, address ) has that
address argument, for example.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
Dec-24-2016, 04:15 AM
(This post was last modified: Dec-24-2016, 04:15 AM by Skaperen.)
the code i wrote:
import socket
def main(args):
p=int(args[1])
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('0.0.0.0',p))
while True:
d,ap=s.recvfrom(65536)
s.sendto(ap[0],ap)
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.