Skip to content

Commit 83c59e6

Browse files
committed
added list command in client
1 parent 50508ee commit 83c59e6

File tree

6 files changed

+165
-160
lines changed

6 files changed

+165
-160
lines changed

Client_A/client.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ def create_directory(directory_name):
7272
print("Directory ", directory_name, " already exists")
7373

7474

75-
def make_socket():
75+
def main():
76+
print("Project DFS!")
77+
7678
# create a socket object
7779
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7880

@@ -89,24 +91,18 @@ def make_socket():
8991
# connection to hostname on the port.
9092
s.connect((host, port))
9193

92-
return s
93-
94-
95-
def main():
96-
print("Project DFS!")
97-
print("Write a command to execute or type help")
98-
9994
# Receive no more than 1024 bytes
100-
s = make_socket()
101-
10295
msg = s.recv(1024)
103-
print(msg.decode("utf-8"))
96+
print(msg.decode())
10497

10598
while True:
99+
print("Write a command to execute or type help")
106100
command = input()
107101
command_split = command.split()
108102
if len(command_split) < 1:
109103
print("Please write a command or type help")
104+
elif command_split[0] == "hello":
105+
print("hello from client")
110106
elif command_split[0] == "open":
111107
open_file(command_split[1])
112108
elif command_split[0] == "read":
@@ -117,19 +113,22 @@ def main():
117113
elif command_split[0] == "create":
118114
create_file(command_split[1])
119115
elif command_split[0] == "write":
116+
s.send(command)
117+
recieve_file(s, command_split[1])
120118
str_temp = " ".join(str(x) for x in command_split[2:])
121119
write_to_file(command_split[1], str_temp)
120+
# add a method to let server know that you are know sending a file
121+
send_file(s, command_split[1])
122122
elif command_split[0] == "append":
123123
str_temp = " ".join(str(x) for x in command_split[2:])
124124
append_to_file(command_split[1], str_temp)
125125
elif command_split[0] == "list":
126-
s.send(command)
126+
s.sendall("list".encode())
127127
while True:
128-
data = s.recv(1024)
129-
# print data
128+
data = s.recv(1024).decode()
129+
print(data)
130130
if not data:
131131
break
132-
# print data
133132
print(data)
134133
elif command_split[0] == "mkdir":
135134
create_directory(command_split[1])

Server_A/server.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def listen_server(serversocket):
5252
lock.acquire(True)
5353
global_file_list.append(data[:-3])
5454
lock.release()
55-
list_str = " ".join(local_file_list)
55+
list_str = " ".join(list_local("Root"))
5656
msg = str(addr[1]) + " " + list_str + " " + "###"
5757
lock.acquire(True)
5858
server_sock_accept.sendall(msg.encode())
@@ -66,18 +66,20 @@ def listen_server(serversocket):
6666

6767

6868
def listen_client(clientsocket):
69+
global clients_connected, global_file_list
70+
print('client listening thread')
6971
# queue up to 5 requests
7072
clientsocket.listen(5)
71-
7273
while True:
73-
7474
client_sock_accept, addr = clientsocket.accept()
75-
7675
print("Got a connection from %s" % str(addr))
77-
78-
msg = "Thank you for connecting"
79-
client_sock_accept.send(msg.encode("utf-8"))
80-
clients_connected.append(client_sock_accept)
76+
print('Listening Connection Successful', addr)
77+
sock_temp = [addr[0], addr[1], client_sock_accept]
78+
lock.acquire(True)
79+
clients_connected.append(sock_temp)
80+
lock.release()
81+
msg = "you are connected to server" + str(port)
82+
client_sock_accept.sendall(msg.encode())
8183

8284
thread_recieve = threading.Thread(
8385
target=recieve_from_client, kwargs={"socket": client_sock_accept}
@@ -89,48 +91,46 @@ def listen_client(clientsocket):
8991
def recieve_from_server(socket):
9092
global local_file_list
9193
while True:
92-
msg = socket.recv(1024)
94+
msg = socket.recv(1024).decode()
9395
if len(msg) < 1:
9496
socket.close()
9597
elif msg == "list":
9698
temp_list = local_file_list
9799
msg = "list | " + temp_list
98-
socket.send()
100+
socket.sendall().encode()
99101
else:
100102
print(msg)
101103

102104

103105
def recieve_from_client(socket):
104106
global global_file_list
105107
while True:
106-
command = socket.recv(1024)
107-
command_split = command.split()
108-
if len(command_split) < 1:
108+
command = socket.recv(1024).decode()
109+
if len(command) < 1:
109110
socket.close()
110-
elif command_split[0] == "list":
111-
socket.send(global_file_list)
112-
elif command_split[0] == "read" or command_split[0] == "write":
113-
send_file(socket, command_split[1])
111+
elif command == "list":
112+
socket.sendall((str(global_file_list)).encode())
113+
elif command == "read" or command == "write":
114+
send_file(socket, command[5:])
114115
else:
115-
socket.send("error")
116+
socket.sendall("error").encode()
116117

117118

118119
def list_local(directory):
119-
global local_file_list
120120
temp_list = os.listdir(directory)
121-
local_file_list = temp_list
121+
return temp_list
122122

123123

124124
def send_file(client_sock, file_name):
125125
with open(file_name, "rb") as file_to_send:
126126
for data in file_to_send:
127-
client_sock.sendall(data)
127+
client_sock.sendall(data).encode()
128128

129129

130130
def recieve_file(client_sock, file_name):
131131
with open(file_name, "wb") as file_to_write:
132132
while True:
133-
data = client_sock.recv(1024)
133+
data = client_sock.recv(1024).decode()
134134
if not data:
135135
break
136136
file_to_write.write(data)
@@ -165,7 +165,7 @@ def main():
165165
except:
166166
print("Error in thread: listen_server")
167167

168-
# Connection Requests
168+
# Connection Requests to servers
169169
for sock in servers_to_connect:
170170
lock.acquire(True)
171171
bool = is_in_servers_to_connect(sock, servers_connected)
@@ -187,7 +187,7 @@ def main():
187187
print('Connect successful')
188188
lock.release()
189189
print('sending list')
190-
list_str = " ".join(local_file_list)
190+
list_str = " ".join(list_local("Root"))
191191
msg = str(sock[1]) + " " + list_str + " " + "###"
192192
lock.acquire(True)
193193
server_conn.sendall(msg.encode())
@@ -202,23 +202,25 @@ def main():
202202
except socket.error:
203203
print("connect failed on " + sock[0], sock[1])
204204

205-
# clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
206-
# clientsocket.bind((host, c_port))
207-
208-
# thread_listen_client = threading.Thread(
209-
# target=listen_client, kwargs={"clientsocket": clientsocket}
210-
# )
211-
# thread_listen_client.daemon = True
212-
# thread_listen_client.start()
205+
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
206+
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
207+
clientsocket.bind((host, c_port))
213208

214-
print(servers_connected)
209+
thread_listen_client = threading.Thread(
210+
target=listen_client, kwargs={"clientsocket": clientsocket}
211+
)
212+
thread_listen_client.daemon = True
213+
thread_listen_client.start()
215214

216215
while True:
217216
command = input()
218217
if(command == 'close' or command == 'exit'):
219218
sys.exit()
220-
if(command == 'list'):
219+
if(command == 'listg'):
221220
print(global_file_list)
221+
if(command == 'listl'):
222+
temp_list = list_local('Root')
223+
print(temp_list)
222224

223225

224226
main()

Server_B/server.py

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# set port for server
1010
port = 5556
1111
# set port for server client socket
12-
c_port = 9999
12+
c_port = 9998
1313
servers_to_connect = [['127.0.0.1', 5555], ['127.0.0.1', 5557]]
1414

1515
# locking mechanism
@@ -52,7 +52,7 @@ def listen_server(serversocket):
5252
lock.acquire(True)
5353
global_file_list.append(data[:-3])
5454
lock.release()
55-
list_str = " ".join(local_file_list)
55+
list_str = " ".join(list_local("Root"))
5656
msg = str(addr[1]) + " " + list_str + " " + "###"
5757
lock.acquire(True)
5858
server_sock_accept.sendall(msg.encode())
@@ -66,18 +66,20 @@ def listen_server(serversocket):
6666

6767

6868
def listen_client(clientsocket):
69+
global clients_connected, global_file_list
70+
print('client listening thread')
6971
# queue up to 5 requests
7072
clientsocket.listen(5)
71-
7273
while True:
73-
7474
client_sock_accept, addr = clientsocket.accept()
75-
7675
print("Got a connection from %s" % str(addr))
77-
78-
msg = "Thank you for connecting"
79-
client_sock_accept.send(msg.encode("utf-8"))
80-
clients_connected.append(client_sock_accept)
76+
print('Listening Connection Successful', addr)
77+
sock_temp = [addr[0], addr[1], client_sock_accept]
78+
lock.acquire(True)
79+
clients_connected.append(sock_temp)
80+
lock.release()
81+
msg = "you are connected to server" + str(port)
82+
client_sock_accept.sendall(msg.encode())
8183

8284
thread_recieve = threading.Thread(
8385
target=recieve_from_client, kwargs={"socket": client_sock_accept}
@@ -89,48 +91,46 @@ def listen_client(clientsocket):
8991
def recieve_from_server(socket):
9092
global local_file_list
9193
while True:
92-
msg = socket.recv(1024)
94+
msg = socket.recv(1024).decode()
9395
if len(msg) < 1:
9496
socket.close()
9597
elif msg == "list":
9698
temp_list = local_file_list
9799
msg = "list | " + temp_list
98-
socket.send()
100+
socket.sendall().encode()
99101
else:
100102
print(msg)
101103

102104

103105
def recieve_from_client(socket):
104106
global global_file_list
105107
while True:
106-
command = socket.recv(1024)
107-
command_split = command.split()
108-
if len(command_split) < 1:
108+
command = socket.recv(1024).decode()
109+
if len(command) < 1:
109110
socket.close()
110-
elif command_split[0] == "list":
111-
socket.send(global_file_list)
112-
elif command_split[0] == "read" or command_split[0] == "write":
113-
send_file(socket, command_split[1])
111+
elif command == "list":
112+
socket.sendall((str(global_file_list)).encode())
113+
elif command == "read" or command == "write":
114+
send_file(socket, command[5:])
114115
else:
115-
socket.send("error")
116+
socket.sendall("error").encode()
116117

117118

118119
def list_local(directory):
119-
global local_file_list
120120
temp_list = os.listdir(directory)
121-
local_file_list = temp_list
121+
return temp_list
122122

123123

124124
def send_file(client_sock, file_name):
125125
with open(file_name, "rb") as file_to_send:
126126
for data in file_to_send:
127-
client_sock.sendall(data)
127+
client_sock.sendall(data).encode()
128128

129129

130130
def recieve_file(client_sock, file_name):
131131
with open(file_name, "wb") as file_to_write:
132132
while True:
133-
data = client_sock.recv(1024)
133+
data = client_sock.recv(1024).decode()
134134
if not data:
135135
break
136136
file_to_write.write(data)
@@ -165,7 +165,7 @@ def main():
165165
except:
166166
print("Error in thread: listen_server")
167167

168-
# Connection Requests
168+
# Connection Requests to servers
169169
for sock in servers_to_connect:
170170
lock.acquire(True)
171171
bool = is_in_servers_to_connect(sock, servers_connected)
@@ -187,7 +187,7 @@ def main():
187187
print('Connect successful')
188188
lock.release()
189189
print('sending list')
190-
list_str = " ".join(local_file_list)
190+
list_str = " ".join(list_local("Root"))
191191
msg = str(sock[1]) + " " + list_str + " " + "###"
192192
lock.acquire(True)
193193
server_conn.sendall(msg.encode())
@@ -202,23 +202,25 @@ def main():
202202
except socket.error:
203203
print("connect failed on " + sock[0], sock[1])
204204

205-
# clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
206-
# clientsocket.bind((host, c_port))
207-
208-
# thread_listen_client = threading.Thread(
209-
# target=listen_client, kwargs={"clientsocket": clientsocket}
210-
# )
211-
# thread_listen_client.daemon = True
212-
# thread_listen_client.start()
205+
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
206+
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
207+
clientsocket.bind((host, c_port))
213208

214-
print(servers_connected)
209+
thread_listen_client = threading.Thread(
210+
target=listen_client, kwargs={"clientsocket": clientsocket}
211+
)
212+
thread_listen_client.daemon = True
213+
thread_listen_client.start()
215214

216215
while True:
217216
command = input()
218217
if(command == 'close' or command == 'exit'):
219218
sys.exit()
220-
if(command == 'list'):
219+
if(command == 'listg'):
221220
print(global_file_list)
221+
if(command == 'listl'):
222+
temp_list = list_local('Root')
223+
print(temp_list)
222224

223225

224226
main()

0 commit comments

Comments
 (0)