From 4c1e5d762503011c023d5dbae30af6a99e94074e Mon Sep 17 00:00:00 2001 From: dayannat Date: Sat, 1 Apr 2023 15:33:17 -0400 Subject: [PATCH 1/7] Socket programs to transfer data line by line --- Socket Programming/client_socket.py | 71 +++++++++++++++++++++++++++++ Socket Programming/server_socket.py | 57 +++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 Socket Programming/client_socket.py create mode 100644 Socket Programming/server_socket.py diff --git a/Socket Programming/client_socket.py b/Socket Programming/client_socket.py new file mode 100644 index 0000000..1e07709 --- /dev/null +++ b/Socket Programming/client_socket.py @@ -0,0 +1,71 @@ +import socket +import time +import pickle +import pandas + +# def sim_live_data(): +# file = open('out.csv','r') +# count = 0 + +# while True: +# count += 1 + +# #wait 1 second between each line +# time.sleep(1) + +# # Get next line from file +# line = file.readline() + +# # if line is empty +# # end of file is reached +# if not line: +# break + +# file.close() + +def client_program(): + host = socket.gethostname() + port = 65400 #random unprivileged port + + """ Read file""" + file = pandas.read_csv('out.csv') + + """ Starting a TCP socket. """ + client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + """ Connecting to the server. """ + client_socket.connect((host, port)) + + # """ Opening and reading the file data. """ + # file = open("out.csv", "r") + # data = file.read() + + # """ Sending the filename to the server. """ + # client_socket.send("out.csv".encode()) + # msg = client_socket.recv(1024).decode() + # print(f"[SERVER]: {msg}") + + # """ Sending the file data to the server. """ + # client_socket.send(data.encode()) + # msg = client_socket.recv(1024).decode() + # print(f"[SERVER]: {msg}") + + client_socket.send(bytes("Sending file...", 'utf-8')) + + Buffer = [] + + for inx in range(file.shape[0]): + Buffer = file.loc[inx] + print(f"Sending \n {Buffer}") + client_socket.send(pickle.dumps(Buffer)) + time.sleep(2) # Wait 2 sec + + + """ Close the file. """ + file.close() + + """ Close the connection from the server. """ + client_socket.close() + + +if __name__ == '__main__': + client_program() \ No newline at end of file diff --git a/Socket Programming/server_socket.py b/Socket Programming/server_socket.py new file mode 100644 index 0000000..a2d2564 --- /dev/null +++ b/Socket Programming/server_socket.py @@ -0,0 +1,57 @@ +import socket +import pickle + + +def server_program(): + host = socket.gethostname() + port = 65400 #random unprivileged port + + """ Starting a TCP socket. """ + server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + """ Bind the IP and PORT to the server. """ + server_socket.bind((host, port)) + print("[STARTING] Server is starting.") + + """ Start Server Listening""" + server_socket.listen() + print("[LISTENING] Server is listening.") + + """ Server accepts connection from client""" + conn, address = server_socket.accept() + print("Connection from: " + str(address)) + + data = conn.recv(1024).decode() + buffer = [] + i = 0 + + while True: + + data = conn.recv(10000) + buffer.append(pickle.loads(data)) + print(buffer[i]) + i = i + 1 + + # """ Receiving the filename from the client. """ + # filename = conn.recv(1024).decode() + # print(f"[RECV] Receiving the filename.") + # file = open(filename, "w") + # conn.send("Filename received.".encode()) + + # """ Receiving the file data from the client. """ + # data = conn.recv(1024).decode() + # print(f"[RECV] Receiving the file data.") + # file.write(data) + # conn.send("File data received".encode()) + + # """ Close the file""" + # file.close() + # """ Close the connection from the client""" + + conn.close() + print(f"[DISCONNECTED] {address} disconnected.") + + +if __name__ == '__main__': + server_program() + From 91c3ef3ffd1f2477144c72a7d8b6352cabb6ab21 Mon Sep 17 00:00:00 2001 From: dayannat Date: Wed, 5 Apr 2023 15:28:11 -0400 Subject: [PATCH 2/7] Attempt to generalize sockets This code is attempting to receive data directly from the python CLI program that simulates board data --- Socket Programming/client_socket.py | 57 ++++------------------------- Socket Programming/server_socket.py | 23 ++---------- 2 files changed, 10 insertions(+), 70 deletions(-) diff --git a/Socket Programming/client_socket.py b/Socket Programming/client_socket.py index 1e07709..db9c611 100644 --- a/Socket Programming/client_socket.py +++ b/Socket Programming/client_socket.py @@ -2,69 +2,26 @@ import time import pickle import pandas +import boardStreamer +from boardStreamer import __main__ -# def sim_live_data(): -# file = open('out.csv','r') -# count = 0 - -# while True: -# count += 1 - -# #wait 1 second between each line -# time.sleep(1) - -# # Get next line from file -# line = file.readline() - -# # if line is empty -# # end of file is reached -# if not line: -# break - -# file.close() def client_program(): host = socket.gethostname() port = 65400 #random unprivileged port - - """ Read file""" - file = pandas.read_csv('out.csv') """ Starting a TCP socket. """ client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) """ Connecting to the server. """ client_socket.connect((host, port)) - # """ Opening and reading the file data. """ - # file = open("out.csv", "r") - # data = file.read() - - # """ Sending the filename to the server. """ - # client_socket.send("out.csv".encode()) - # msg = client_socket.recv(1024).decode() - # print(f"[SERVER]: {msg}") - - # """ Sending the file data to the server. """ - # client_socket.send(data.encode()) - # msg = client_socket.recv(1024).decode() - # print(f"[SERVER]: {msg}") - - client_socket.send(bytes("Sending file...", 'utf-8')) - - Buffer = [] + # Buffer = [] - for inx in range(file.shape[0]): - Buffer = file.loc[inx] - print(f"Sending \n {Buffer}") - client_socket.send(pickle.dumps(Buffer)) - time.sleep(2) # Wait 2 sec - - - """ Close the file. """ - file.close() + while __main__: + client_socket.send(pickle.dumps(__main__.data)) - """ Close the connection from the server. """ - client_socket.close() + """ Close the connection from the server. """ + client_socket.close() if __name__ == '__main__': diff --git a/Socket Programming/server_socket.py b/Socket Programming/server_socket.py index a2d2564..f67ce93 100644 --- a/Socket Programming/server_socket.py +++ b/Socket Programming/server_socket.py @@ -1,5 +1,7 @@ import socket import pickle +import boardStreamer +from boardStreamer import __main__ def server_program(): @@ -11,42 +13,23 @@ def server_program(): """ Bind the IP and PORT to the server. """ server_socket.bind((host, port)) - print("[STARTING] Server is starting.") """ Start Server Listening""" server_socket.listen() - print("[LISTENING] Server is listening.") """ Server accepts connection from client""" conn, address = server_socket.accept() print("Connection from: " + str(address)) - data = conn.recv(1024).decode() buffer = [] i = 0 while True: - data = conn.recv(10000) + data = conn.recv(1024) buffer.append(pickle.loads(data)) print(buffer[i]) i = i + 1 - - # """ Receiving the filename from the client. """ - # filename = conn.recv(1024).decode() - # print(f"[RECV] Receiving the filename.") - # file = open(filename, "w") - # conn.send("Filename received.".encode()) - - # """ Receiving the file data from the client. """ - # data = conn.recv(1024).decode() - # print(f"[RECV] Receiving the file data.") - # file.write(data) - # conn.send("File data received".encode()) - - # """ Close the file""" - # file.close() - # """ Close the connection from the client""" conn.close() print(f"[DISCONNECTED] {address} disconnected.") From 50661e2839ccbd17464f46e6572b012be0b6a32e Mon Sep 17 00:00:00 2001 From: dayannat Date: Wed, 5 Apr 2023 15:33:22 -0400 Subject: [PATCH 3/7] Attempt to generalize sockets This code makes attempts to grab the data from the CLI program that simulates board data --- Socket Programming/client_socket.py | 57 ++++------------------------- Socket Programming/server_socket.py | 23 ++---------- 2 files changed, 10 insertions(+), 70 deletions(-) diff --git a/Socket Programming/client_socket.py b/Socket Programming/client_socket.py index 1e07709..db9c611 100644 --- a/Socket Programming/client_socket.py +++ b/Socket Programming/client_socket.py @@ -2,69 +2,26 @@ import time import pickle import pandas +import boardStreamer +from boardStreamer import __main__ -# def sim_live_data(): -# file = open('out.csv','r') -# count = 0 - -# while True: -# count += 1 - -# #wait 1 second between each line -# time.sleep(1) - -# # Get next line from file -# line = file.readline() - -# # if line is empty -# # end of file is reached -# if not line: -# break - -# file.close() def client_program(): host = socket.gethostname() port = 65400 #random unprivileged port - - """ Read file""" - file = pandas.read_csv('out.csv') """ Starting a TCP socket. """ client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) """ Connecting to the server. """ client_socket.connect((host, port)) - # """ Opening and reading the file data. """ - # file = open("out.csv", "r") - # data = file.read() - - # """ Sending the filename to the server. """ - # client_socket.send("out.csv".encode()) - # msg = client_socket.recv(1024).decode() - # print(f"[SERVER]: {msg}") - - # """ Sending the file data to the server. """ - # client_socket.send(data.encode()) - # msg = client_socket.recv(1024).decode() - # print(f"[SERVER]: {msg}") - - client_socket.send(bytes("Sending file...", 'utf-8')) - - Buffer = [] + # Buffer = [] - for inx in range(file.shape[0]): - Buffer = file.loc[inx] - print(f"Sending \n {Buffer}") - client_socket.send(pickle.dumps(Buffer)) - time.sleep(2) # Wait 2 sec - - - """ Close the file. """ - file.close() + while __main__: + client_socket.send(pickle.dumps(__main__.data)) - """ Close the connection from the server. """ - client_socket.close() + """ Close the connection from the server. """ + client_socket.close() if __name__ == '__main__': diff --git a/Socket Programming/server_socket.py b/Socket Programming/server_socket.py index a2d2564..f67ce93 100644 --- a/Socket Programming/server_socket.py +++ b/Socket Programming/server_socket.py @@ -1,5 +1,7 @@ import socket import pickle +import boardStreamer +from boardStreamer import __main__ def server_program(): @@ -11,42 +13,23 @@ def server_program(): """ Bind the IP and PORT to the server. """ server_socket.bind((host, port)) - print("[STARTING] Server is starting.") """ Start Server Listening""" server_socket.listen() - print("[LISTENING] Server is listening.") """ Server accepts connection from client""" conn, address = server_socket.accept() print("Connection from: " + str(address)) - data = conn.recv(1024).decode() buffer = [] i = 0 while True: - data = conn.recv(10000) + data = conn.recv(1024) buffer.append(pickle.loads(data)) print(buffer[i]) i = i + 1 - - # """ Receiving the filename from the client. """ - # filename = conn.recv(1024).decode() - # print(f"[RECV] Receiving the filename.") - # file = open(filename, "w") - # conn.send("Filename received.".encode()) - - # """ Receiving the file data from the client. """ - # data = conn.recv(1024).decode() - # print(f"[RECV] Receiving the file data.") - # file.write(data) - # conn.send("File data received".encode()) - - # """ Close the file""" - # file.close() - # """ Close the connection from the client""" conn.close() print(f"[DISCONNECTED] {address} disconnected.") From 56b2bdd3acd0b4873ba3218eaf81fece391c24a4 Mon Sep 17 00:00:00 2001 From: dayannat Date: Wed, 5 Apr 2023 21:29:28 -0400 Subject: [PATCH 4/7] Fixed errors --- boardStreamer/__main__.py | 14 +++++++++++++- .../client_socket.py => client_socket.py | 8 ++++---- .../server_socket.py => server_socket.py | 6 ++---- 3 files changed, 19 insertions(+), 9 deletions(-) rename Socket Programming/client_socket.py => client_socket.py (81%) rename Socket Programming/server_socket.py => server_socket.py (87%) diff --git a/boardStreamer/__main__.py b/boardStreamer/__main__.py index 5d28d8c..db5ef4c 100644 --- a/boardStreamer/__main__.py +++ b/boardStreamer/__main__.py @@ -2,6 +2,8 @@ from find_cyton import find_cyton from brainflow.board_shim import BoardShim, BoardIds, BrainFlowInputParams from time import sleep +import socket +import pickle from functools import cached_property @@ -96,7 +98,17 @@ def get_data(self) -> list: board = CytonBoard(**args) +# setup socket connection +host = socket.gethostname() +port = 50000 #random unprivileged port +""" Starting a TCP socket. """ +client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +""" Connecting to the server. """ +client_socket.connect((host, port)) + while True: data = board.get_data() + + client_socket.send(pickle.dumps(data)) - # insert socket code here + diff --git a/Socket Programming/client_socket.py b/client_socket.py similarity index 81% rename from Socket Programming/client_socket.py rename to client_socket.py index db9c611..5ec9bff 100644 --- a/Socket Programming/client_socket.py +++ b/client_socket.py @@ -1,14 +1,14 @@ +# IGNORE THIS FILE - CURRENTLY NOT IN USE + import socket import time import pickle import pandas -import boardStreamer -from boardStreamer import __main__ - +from boardStreamer import __main__ def client_program(): host = socket.gethostname() - port = 65400 #random unprivileged port + port = 50000 #random unprivileged port """ Starting a TCP socket. """ client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) diff --git a/Socket Programming/server_socket.py b/server_socket.py similarity index 87% rename from Socket Programming/server_socket.py rename to server_socket.py index f67ce93..a7dca60 100644 --- a/Socket Programming/server_socket.py +++ b/server_socket.py @@ -1,12 +1,10 @@ import socket import pickle import boardStreamer -from boardStreamer import __main__ - def server_program(): host = socket.gethostname() - port = 65400 #random unprivileged port + port = 50000 #random unprivileged port """ Starting a TCP socket. """ server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -26,7 +24,7 @@ def server_program(): while True: - data = conn.recv(1024) + data = conn.recv(10000) buffer.append(pickle.loads(data)) print(buffer[i]) i = i + 1 From 3668ebe09116f7c0f2dd910a2b5d6198198bd1a8 Mon Sep 17 00:00:00 2001 From: Mitchell Gilmore Date: Thu, 20 Apr 2023 16:24:05 -0400 Subject: [PATCH 5/7] removed 'Unused File' --- client_socket.py | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 client_socket.py diff --git a/client_socket.py b/client_socket.py deleted file mode 100644 index 5ec9bff..0000000 --- a/client_socket.py +++ /dev/null @@ -1,28 +0,0 @@ -# IGNORE THIS FILE - CURRENTLY NOT IN USE - -import socket -import time -import pickle -import pandas -from boardStreamer import __main__ - -def client_program(): - host = socket.gethostname() - port = 50000 #random unprivileged port - - """ Starting a TCP socket. """ - client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - """ Connecting to the server. """ - client_socket.connect((host, port)) - - # Buffer = [] - - while __main__: - client_socket.send(pickle.dumps(__main__.data)) - - """ Close the connection from the server. """ - client_socket.close() - - -if __name__ == '__main__': - client_program() \ No newline at end of file From e70298963d1aba2cace74757b96a0e09f2048beb Mon Sep 17 00:00:00 2001 From: Mitchell Gilmore <102109623+mgilmore42@users.noreply.github.com> Date: Sun, 23 Apr 2023 01:15:10 -0400 Subject: [PATCH 6/7] removed server_socket.py --- server_socket.py | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 server_socket.py diff --git a/server_socket.py b/server_socket.py deleted file mode 100644 index a7dca60..0000000 --- a/server_socket.py +++ /dev/null @@ -1,38 +0,0 @@ -import socket -import pickle -import boardStreamer - -def server_program(): - host = socket.gethostname() - port = 50000 #random unprivileged port - - """ Starting a TCP socket. """ - server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - - """ Bind the IP and PORT to the server. """ - server_socket.bind((host, port)) - - """ Start Server Listening""" - server_socket.listen() - - """ Server accepts connection from client""" - conn, address = server_socket.accept() - print("Connection from: " + str(address)) - - buffer = [] - i = 0 - - while True: - - data = conn.recv(10000) - buffer.append(pickle.loads(data)) - print(buffer[i]) - i = i + 1 - - conn.close() - print(f"[DISCONNECTED] {address} disconnected.") - - -if __name__ == '__main__': - server_program() - From 18618e1d9f2b8fadd0e620c3abaee17401b233cd Mon Sep 17 00:00:00 2001 From: Mitchell Gilmore <102109623+mgilmore42@users.noreply.github.com> Date: Sun, 23 Apr 2023 01:18:00 -0400 Subject: [PATCH 7/7] changed to assign port number based on CLI args --- boardStreamer/__main__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/boardStreamer/__main__.py b/boardStreamer/__main__.py index db5ef4c..11eb9b9 100644 --- a/boardStreamer/__main__.py +++ b/boardStreamer/__main__.py @@ -100,11 +100,10 @@ def get_data(self) -> list: # setup socket connection host = socket.gethostname() -port = 50000 #random unprivileged port """ Starting a TCP socket. """ client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) """ Connecting to the server. """ -client_socket.connect((host, port)) +client_socket.connect((host, args['port'])) while True: data = board.get_data()