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