lin
2025-08-14 dae8bad597b6607a449b32bf76c523423f7720ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# UDP server example
import time, socket, string
 
def main():
 
    port = 9001
    buf = open("random.dat").read()
 
    svrsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    svrsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    svrsocket.bind(('', port))
 
    # hostname = socket.gethostname()
    hostname = "localhost"
    ip = socket.gethostbyname(hostname)
    print 'Server is at IP adress: ', ip
    print 'Listening for requests on port %s ...' % port
 
    data, address = svrsocket.recvfrom(8192)
 
    count = 0
    while count < 500:
        print 'Sending packet', count, 'to', address[0]
        svrsocket.sendto("%3.3s%s" % (count, buf), address)
        time.sleep(0.08)
        count += 1
        
if __name__ == "__main__":
    main()