Skip to content

Instantly share code, notes, and snippets.

@qoelet
Created August 1, 2011 17:37
Show Gist options
  • Save qoelet/1118601 to your computer and use it in GitHub Desktop.
Save qoelet/1118601 to your computer and use it in GitHub Desktop.
# Example of a TCP/IP Server in .NET #
# Tested on Mono/IronPython #
# imports
from System.Net.Sockets import *
from System.Threading import *
from System.IO import *
# basic configuration
SERVER_PORT = 31000
THREAD_LIMIT = 5
class Server(object):
def __init__(self):
self.listener = TcpListener(SERVER_PORT)
self.listener.Start() # wait for incoming conn
def handler(self):
while 1:
self.soc = self.listener.AcceptSocket()
s = NetworkStream(self.soc)
sr = StreamReader(s)
sw = StreamWriter(s)
sw.AutoFlush = True
while True:
data_recv = sr.ReadLine()
if data_recv == "" or data_recv == None:
break
sw.WriteLine(data_recv)
break
# close connections
s.Close()
self.soc.Close()
if __name__ == "__main__":
count = 0
while 1:
while(count < THREAD_LIMIT):
s = Server()
t = Thread(ThreadStart(s.handler))
t.Start()
count +=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment