-
Notifications
You must be signed in to change notification settings - Fork 1
/
p2.py
55 lines (46 loc) · 1.37 KB
/
p2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import socket
import sys
from threading import Thread
from SocketServer import ThreadingMixIn
argv = sys.argv
# Get the server hostname and port as command line arguments
host = argv[1]
TCP_PORT = argv[2]
TCP_PORT = int(TCP_PORT)
TCP_IP = 'localhost'
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 Peer ("+ip+":"+str(port)+")"
def run(self):
filename='chunk2.txt'
f = open(filename,'rb')
while True:
l = f.read(BUFFER_SIZE)
while (l):
str = filename + "_" + l
self.sock.send(str)
print('Chunk 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()