├── README.md
├── client
├── eye.png
├── files
│ ├── eye.png
│ ├── ico.png
│ ├── jpg.png
│ ├── png.png
│ └── wmv.png
├── jpg.png
├── png.png
├── server.txt
├── temp.py
├── temp2.py
├── temp3.py
└── zero.png
└── server
├── files
├── question.jpeg
├── server.txt
└── zero.png
├── multiserver.py
├── paris.png
├── wmv.png
└── zero.png
/README.md:
--------------------------------------------------------------------------------
1 | # Socket-Programming-TCP-Multithreading
2 | ### Problem Statement for the above Code
3 | Implement a Multithreaded File Server Using Tcp Sockets.
4 |
5 |
6 |
7 |
8 | ## File Server (Upload/Download) Using TCP Sockets using python language
9 | - Multiple Clients Can Connect to the Server.
10 | - There are 3 files named *temp.py*, *temp2.py* and *temp3.py*
11 | - First run the *multiserver.py*(in server folder), then run all the above(files in client folder)
12 | - With the help of Multithreading, You can download from a server and upload on the server through different clients.
13 | - Server create one thread for each client.
14 | - The line below will create thread for every client connection
15 | - `threading.Thread(target = self.listenToClient,args = (c,addr)).start()`
16 | - File is divided into 1KB blocks and File is transferred block by block.
17 | - code ensures that file is recieved at the other end in 1024bytes only at a time, but you can make changes as you want.
18 | - **SOCK_STREAM** keyword in socket definition implies that the sockets are ***TCP***, in case of **SOCK_DGRAM** it would ***UDP*** sockets.
19 | - `self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)`
20 |
--------------------------------------------------------------------------------
/client/eye.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/client/eye.png
--------------------------------------------------------------------------------
/client/files/eye.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/client/files/eye.png
--------------------------------------------------------------------------------
/client/files/ico.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/client/files/ico.png
--------------------------------------------------------------------------------
/client/files/jpg.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/client/files/jpg.png
--------------------------------------------------------------------------------
/client/files/png.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/client/files/png.png
--------------------------------------------------------------------------------
/client/files/wmv.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/client/files/wmv.png
--------------------------------------------------------------------------------
/client/jpg.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/client/jpg.png
--------------------------------------------------------------------------------
/client/png.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/client/png.png
--------------------------------------------------------------------------------
/client/server.txt:
--------------------------------------------------------------------------------
1 | dsahdas
2 | as
3 | sa
4 | dsa
5 | dassd
6 | s
7 | adas
8 | dsa
9 | da
10 |
11 |
12 | gdg
13 | dg
14 | sda
15 | g'
16 | ad'g
17 | sg'g
18 | s'g
19 | gs'
20 | #3
21 | 33
22 | 3
23 | 3#
24 | ###
25 |
26 | ###
27 | #
28 | #
29 | #
30 | #
31 | #
32 | #
33 | #
34 | #
35 | #
36 | #
37 | #
38 | #
39 | #
40 | #
41 | #
42 | #
43 | ##
44 | #5F#@
45 | 4
46 |
47 |
48 | @#
49 | F5
50 | 6
51 | C"
52 |
53 | XC$@
54 | ^&C&
55 | 4
56 | ^QC
57 | ^#
58 | ^C
59 |
60 | "
61 | $^&"@
62 | V
63 | "C#
64 | C
65 |
66 | f
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | ffffffff
85 | f
86 | f
87 | f
88 | f
89 | f
90 | ff
91 | f
92 | f
93 | f
94 | f
95 | f
96 | f
97 | f
98 | f
99 | f
100 | f
101 | f
102 | f
103 | f
104 | f
105 | f
106 | f
107 | f
108 | f
109 | f
110 | f
111 | f
112 | f
113 | f
114 | f
115 | f
116 | f
117 | f
118 | f
119 | f
120 | f
121 |
122 | s'gd
123 | d
124 | s rhjgw 8778t uqgiudajbcas
125 | fafs
126 |
127 | G
128 | t
129 | s
130 |
131 |
132 | S
133 | gg
134 | gs
135 | t
136 | H
137 | "
138 |
139 | rg
140 |
141 | EH"
142 | eh
143 |
--------------------------------------------------------------------------------
/client/temp.py:
--------------------------------------------------------------------------------
1 | import socket # Import socket module
2 | import sys
3 | import os
4 |
5 | #49152-65535
6 | s = socket.socket() # Create a socket object
7 | host = socket.gethostname() # Get local machine name
8 | port = 49157 # Reserve a port for your service.
9 |
10 | s.bind((host, port)) # Bind to the port
11 |
12 | try:
13 | s.connect((host, 49159))
14 | print("Connected Successfully!")
15 | except Exception as e:
16 | print("something's wrong with %s:%d. Exception is %s" % (host, 49159, e))
17 |
18 | Answer = input("download/upload?")
19 | if(Answer == "download"):
20 | mssg = "download"
21 | s.send(mssg.encode())
22 | FileName = input("Enter Filename to Download from server : ")
23 | Data = "Temp"
24 |
25 | while True:
26 | s.send(FileName.encode())
27 | Data = s.recv(1024)
28 | DownloadFile = open(FileName,"wb")
29 | i = 1
30 | while Data:
31 | print('Recieving...%d' %(i))
32 | DownloadFile.write(Data)
33 | Data = s.recv(1024)
34 | i = i + 1
35 | print("Done Recieving")
36 | DownloadFile.close()
37 | break
38 |
39 | elif(Answer == "upload"):
40 | mssg = "upload"
41 | s.send(mssg.encode())
42 | print(os.listdir("/Ambition/client/"))
43 | FileName = input("Enter Filename to Upload On server : ")
44 | s.send(FileName.encode())
45 |
46 | UploadFile = open("/Ambition/client//"+FileName,"rb")
47 | Read = UploadFile.read(1024)
48 | i = 1
49 | while Read:
50 | print("Sending...%d" %(i))
51 | s.send(Read) #sends 1KB
52 | Read = UploadFile.read(1024)
53 | print("Done Sending")
54 | UploadFile.close()
55 |
56 |
57 | s.close()
58 |
59 |
60 | '''
61 | else:
62 | print("Enter the option carefully Next time!")
63 | break
64 |
65 | ##### Code To Recieve File ####
66 |
67 | i = 1
68 | l = s.recv(block_size)
69 | while (l):
70 | print("Receiving...%d" % (i))
71 | l = s.recv(block_size)
72 | i = i + 1
73 | print("Done Receiving")
74 | print(s.recv(1024))
75 | s.shutdown(socket.SHUT_WR)
76 | s.close
77 |
78 | res = input('do you want to continue??(yes/no)')
79 | if(res == 'yes'):
80 | continue
81 | else:
82 | break
83 | ################################
84 | '''
85 |
--------------------------------------------------------------------------------
/client/temp2.py:
--------------------------------------------------------------------------------
1 | import socket # Import socket module
2 | import sys
3 | import os
4 |
5 | #49152-65535
6 | s = socket.socket() # Create a socket object
7 | host = socket.gethostname() # Get local machine name
8 | port = 49158 # Reserve a port for your service.
9 |
10 | s.bind((host, port)) # Bind to the port
11 |
12 | try:
13 | s.connect((host, 49159))
14 | print("Connected Successfully!")
15 | except Exception as e:
16 | print("something's wrong with %s:%d. Exception is %s" % (host, 49159, e))
17 |
18 | Answer = input("download/upload?")
19 | if(Answer == "download"):
20 | mssg = "download"
21 | s.send(mssg.encode())
22 | FileName = input("Enter Filename to Download from server : ")
23 | Data = "Temp"
24 |
25 | while True:
26 | s.send(FileName.encode())
27 | Data = s.recv(1024)
28 | DownloadFile = open(FileName,"wb")
29 | i = 1
30 | while Data:
31 | print('Recieving...%d' %(i))
32 | DownloadFile.write(Data)
33 | Data = s.recv(1024)
34 | i = i + 1
35 | print("Done Recieving")
36 | DownloadFile.close()
37 | break
38 |
39 | elif(Answer == "upload"):
40 | mssg = "upload"
41 | s.send(mssg.encode())
42 | print(os.listdir("/Ambition/client/"))
43 | FileName = input("Enter Filename to Upload On server : ")
44 | s.send(FileName.encode())
45 |
46 | UploadFile = open("/Ambition/client/"+FileName,"rb")
47 | Read = UploadFile.read(1024)
48 | i = 1
49 | while Read:
50 | print("Sending...%d" %(i))
51 | s.send(Read) #sends 1KB
52 | Read = UploadFile.read(1024)
53 | print("Done Sending")
54 | UploadFile.close()
55 |
56 |
57 | s.close()
58 |
59 |
60 | '''
61 | else:
62 | print("Enter the option carefully Next time!")
63 | break
64 |
65 | ##### Code To Recieve File ####
66 |
67 | i = 1
68 | l = s.recv(block_size)
69 | while (l):
70 | print("Receiving...%d" % (i))
71 | l = s.recv(block_size)
72 | i = i + 1
73 | print("Done Receiving")
74 | print(s.recv(1024))
75 | s.shutdown(socket.SHUT_WR)
76 | s.close
77 |
78 | res = input('do you want to continue??(yes/no)')
79 | if(res == 'yes'):
80 | continue
81 | else:
82 | break
83 | ################################
84 | '''
85 |
--------------------------------------------------------------------------------
/client/temp3.py:
--------------------------------------------------------------------------------
1 | import socket # Import socket module
2 | import sys
3 | import os
4 |
5 | #49152-65535
6 | s = socket.socket() # Create a socket object
7 | host = socket.gethostname() # Get local machine name
8 | port = 49156 # Reserve a port for your service.
9 |
10 | s.bind((host, port)) # Bind to the port
11 |
12 | try:
13 | s.connect((host, 49159))
14 | print("Connected Successfully!")
15 | except Exception as e:
16 | print("something's wrong with %s:%d. Exception is %s" % (host, 49159, e))
17 |
18 | Answer = input("download/upload?")
19 | if(Answer == "download"):
20 | mssg = "download"
21 | s.send(mssg.encode())
22 | FileName = input("Enter Filename to Download from server : ")
23 | Data = "Temp"
24 |
25 | while True:
26 | s.send(FileName.encode())
27 | Data = s.recv(1024)
28 | DownloadFile = open(FileName,"wb")
29 | i = 1
30 | while Data:
31 | print('Recieving...%d' %(i))
32 | DownloadFile.write(Data)
33 | Data = s.recv(1024)
34 | i = i + 1
35 | print("Done Recieving")
36 | DownloadFile.close()
37 | break
38 |
39 | elif(Answer == "upload"):
40 | mssg = "upload"
41 | s.send(mssg.encode())
42 | print(os.listdir("/Ambition/client/"))
43 | FileName = input("Enter Filename to Upload On server : ")
44 | s.send(FileName.encode())
45 |
46 | UploadFile = open("/Ambition/client/"+FileName,"rb")
47 | Read = UploadFile.read(1024)
48 | i = 1
49 | while Read:
50 | print("Sending...%d" %(i))
51 | s.send(Read) #sends 1KB
52 | Read = UploadFile.read(1024)
53 | print("Done Sending")
54 | UploadFile.close()
55 |
56 |
57 | s.close()
58 |
59 |
60 | '''
61 | else:
62 | print("Enter the option carefully Next time!")
63 | break
64 |
65 | ##### Code To Recieve File ####
66 |
67 | i = 1
68 | l = s.recv(block_size)
69 | while (l):
70 | print("Receiving...%d" % (i))
71 | l = s.recv(block_size)
72 | i = i + 1
73 | print("Done Receiving")
74 | print(s.recv(1024))
75 | s.shutdown(socket.SHUT_WR)
76 | s.close
77 |
78 | res = input('do you want to continue??(yes/no)')
79 | if(res == 'yes'):
80 | continue
81 | else:
82 | break
83 | ################################
84 | '''
85 |
--------------------------------------------------------------------------------
/client/zero.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/client/zero.png
--------------------------------------------------------------------------------
/server/files/question.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/server/files/question.jpeg
--------------------------------------------------------------------------------
/server/files/server.txt:
--------------------------------------------------------------------------------
1 | dsahdas
2 | as
3 | sa
4 | dsa
5 | dassd
6 | s
7 | adas
8 | dsa
9 | da
10 |
11 |
12 | gdg
13 | dg
14 | sda
15 | g'
16 | ad'g
17 | sg'g
18 | s'g
19 | gs'
20 | #3
21 | 33
22 | 3
23 | 3#
24 | ###
25 |
26 | ###
27 | #
28 | #
29 | #
30 | #
31 | #
32 | #
33 | #
34 | #
35 | #
36 | #
37 | #
38 | #
39 | #
40 | #
41 | #
42 | #
43 | ##
44 | #5F#@
45 | 4
46 |
47 |
48 | @#
49 | F5
50 | 6
51 | C"
52 |
53 | XC$@
54 | ^&C&
55 | 4
56 | ^QC
57 | ^#
58 | ^C
59 |
60 | "
61 | $^&"@
62 | V
63 | "C#
64 | C
65 |
66 | f
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | ffffffff
85 | f
86 | f
87 | f
88 | f
89 | f
90 | ff
91 | f
92 | f
93 | f
94 | f
95 | f
96 | f
97 | f
98 | f
99 | f
100 | f
101 | f
102 | f
103 | f
104 | f
105 | f
106 | f
107 | f
108 | f
109 | f
110 | f
111 | f
112 | f
113 | f
114 | f
115 | f
116 | f
117 | f
118 | f
119 | f
120 | f
121 |
122 | s'gd
123 | d
124 | s rhjgw 8778t uqgiudajbcas
125 | fafs
126 |
127 | G
128 | t
129 | s
130 |
131 |
132 | S
133 | gg
134 | gs
135 | t
136 | H
137 | "
138 |
139 | rg
140 |
141 | EH"
142 | eh
143 |
--------------------------------------------------------------------------------
/server/files/zero.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/server/files/zero.png
--------------------------------------------------------------------------------
/server/multiserver.py:
--------------------------------------------------------------------------------
1 | import socket
2 | import threading
3 | import os
4 |
5 | #49152-65535
6 | class ThreadedServer():
7 | def __init__(self):
8 | self.host = socket.gethostname()
9 | self.port = 49159
10 | self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
11 | self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #the SO_REUSEADDR flag tells the kernel to
12 | self.s.bind((self.host, self.port)) #reuse a local socket in TIME_WAIT state,
13 | #without waiting for its natural timeout to expire.
14 |
15 |
16 | def listen(self):
17 | self.s.listen(5)
18 | while True:
19 | c, addr = self.s.accept()
20 | c.settimeout(60)
21 | threading.Thread(target = self.listenToClient,args = (c,addr)).start()
22 |
23 | def listenToClient(self, c, addr):
24 | block_size = 1024
25 | print('Got connection from', addr)
26 |
27 | data = c.recv(1024)
28 |
29 | if (data.decode() == "download"):
30 | print(os.listdir("/Ambition/server/")) #Shows all the files at server side
31 |
32 | FileName = c.recv(1024)
33 | for file in os.listdir("/Ambition/server/"):
34 | if file == FileName.decode():
35 | FileFound = 1
36 | break
37 |
38 | if FileFound == 0:
39 | print(" Not Found On Server")
40 |
41 | else:
42 | print("File Found")
43 | upfile = FileName.decode()
44 | UploadFile = open("/Ambition/server/"+upfile,"rb")
45 | Read = UploadFile.read(1024)
46 | i = 1
47 | while Read:
48 | print("Sending...%d" %(i))
49 | #UploadFile.write("oooooooooooooooooooooooooooooooooooooooooooooooooooo")
50 | c.send(Read) #sends 1KB
51 | Read = UploadFile.read(1024)
52 | print("Done Sending")
53 | UploadFile.close()
54 | #s.shutdown(socket.SHUT_WR)
55 | c.close()
56 |
57 | elif (data.decode() == "upload"):
58 | FileName = c.recv(1024)
59 | downfile = FileName.decode()
60 | Data = c.recv(1024)
61 | DownloadFile = open(downfile,"wb")
62 | i = 1
63 | while Data:
64 | print('Recieving...%d' %(i))
65 | DownloadFile.write(Data)
66 | Data = c.recv(1024)
67 | i = i + 1
68 | print("Done Recieving")
69 | DownloadFile.close()
70 | c.close()
71 |
72 |
73 | if __name__ == "__main__":
74 | ThreadedServer().listen()
75 |
--------------------------------------------------------------------------------
/server/paris.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/server/paris.png
--------------------------------------------------------------------------------
/server/wmv.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/server/wmv.png
--------------------------------------------------------------------------------
/server/zero.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TejasTidke/Socket-Programming-TCP-Multithreading/409f2f46ef049dcbdd0f8ae8fc4164c478e02f17/server/zero.png
--------------------------------------------------------------------------------