UDP-server example

This is a simple UDP-server example in Python. Run and connect with nc -u 127.0.0.1 2608.

from socket import *

# Serverparameters
TheServerHost = "localhost"
ListeningPort = 2608
BufferSize = 1024
address = (TheServerHost, ListeningPort)

# Sockets
UDPSock = socket(AF_INET, SOCK_DGRAM)
UDPSock.bind(address)

# Answer calls from client
while 1:
   data,address = UDPSock.recvfrom(BufferSize)

	# If there is data awaiting
   if data:
		# echo
		UDPSock.sendto(data + '\n', address)

TCP? See this post.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s