# ---------------------------------------------------------------------------------------------------------------------- # Phase 1 Project, UDPServer.py # Written by Richard Douglas and Mounika Gannu # for 16.483/583.201 - Network Design: Principles, Protocols, and Applications at UMass Lowell # February 6, 2015 # # Credits to "Computer Networking, A Top Down Approach, 6th Ed." by Kurose and Ross for Python 2 code in Chapter 2, # as well as to Jeremy Plante for finding compatibility issues between Python 2 and Python 3 code. # ---------------------------------------------------------------------------------------------------------------------- from socket import * # this line allows the creation of sockets serverPort = 12000 # communication will be on port 12000 serverSocket = socket(AF_INET, SOCK_DGRAM) # creates a server socket. AF_INET indicates the network is using IPv4, # SOCK_DGRAM indicates that the socket will be a UDP socket serverSocket.bind(('', serverPort)) # assigns port 12000 to the server socket. All packets sent to port 12000 of the # server's IP address will be sent to the server's socket. filename = "ImageStorage.png" fileIn = open(filename, 'w+b') print "The server is ready to receive" # prints the statement to screen while 1: # begins an infinite loop data, clientAddress = serverSocket.recvfrom(2048) # the server waits to receive data from the client. when data arrives, the method 'recvfrom' takes the 2048 bit # buffer input and puts the incoming message into the variable 'message' and combines the client's IP address and # client's port number into the variable 'clientAddress'. if not data: print "Finished receiving file.\n" fileIn = open(filename, 'rb') break fileIn = data(2048) while 1: data = fileIn(2048) if not data: print "Finished sending file.\n" break serverSocket.sendto(data, clientAddress) # the method 'sendto' attaches the clientAddress to the modifiedMessage and sends it to the process's socket, # 'serverSocket'
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question