Created
September 10, 2015 05:37
-
-
Save divyanshu013/89fac913aff0245f151d to your computer and use it in GitHub Desktop.
A simple multithreaded server-client application in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import socket | |
TCP_IP = 'localhost' | |
TCP_PORT = 6969 | |
BUFFER_SIZE = 1024 | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((TCP_IP, TCP_PORT)) | |
with open('received_file', 'wb') as f: | |
print 'file opened' | |
while True: | |
#print('receiving data...') | |
data = s.recv(BUFFER_SIZE) | |
print('data=%s', (data)) | |
if not data: | |
f.close() | |
print 'file close()' | |
break | |
# write data to a file | |
f.write(data) | |
print('Successfully get the file') | |
s.close() | |
print('connection closed') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
from threading import Thread | |
from SocketServer import ThreadingMixIn | |
TCP_IP = 'localhost' | |
TCP_PORT = 6969 | |
BUFFER_SIZE = 1024 | |
class ClientThread(Thread): | |
def __init__(self,ip,port,sock): | |
Thread.__init__(self) | |
self.ip = ip | |
self.port = port | |
self.sock = sock | |
print " New thread started for "+ip+":"+str(port) | |
def run(self): | |
filename='mytext.txt' | |
f = open(filename,'rb') | |
while True: | |
l = f.read(BUFFER_SIZE) | |
while (l): | |
self.sock.send(l) | |
#print('Sent ',repr(l)) | |
l = f.read(BUFFER_SIZE) | |
if not l: | |
f.close() | |
self.sock.close() | |
break | |
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
tcpsock.bind((TCP_IP, TCP_PORT)) | |
threads = [] | |
while True: | |
tcpsock.listen(5) | |
print "Waiting for incoming connections..." | |
(conn, (ip,port)) = tcpsock.accept() | |
print 'Got connection from ', (ip,port) | |
newthread = ClientThread(ip,port,conn) | |
newthread.start() | |
threads.append(newthread) | |
for t in threads: | |
t.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment