Skip to content

Commit

Permalink
Add base code for socket communication
Browse files Browse the repository at this point in the history
  • Loading branch information
corentinlger committed Jan 30, 2024
1 parent f8161c1 commit 8eb14db
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
78 changes: 78 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,112 @@
antlr4-python3-runtime==4.9.3
asttokens==2.4.1
bidict==0.22.1
black==23.12.1
bleach==6.1.0
blinker==1.7.0
bokeh==3.3.4
certifi==2023.11.17
cfgv==3.4.0
charset-normalizer==3.3.2
click==8.1.7
colorcet==3.0.1
comm==0.2.1
contourpy==1.2.0
cycler==0.12.1
debugpy==1.8.0
decorator==5.1.1
distlib==0.3.8
evdev==1.6.1
exceptiongroup==1.2.0
executing==2.0.1
filelock==3.13.1
Flask==3.0.1
Flask-SocketIO==5.3.6
fonttools==4.47.2
h11==0.14.0
holoviews==1.18.1
hvplot==0.9.2
hydra-core==1.3.2
identify==2.5.33
idna==3.6
iniconfig==2.0.0
ipykernel==6.29.0
ipython==8.20.0
ipywidgets==8.1.1
itsdangerous==2.1.2
jax==0.4.23
jaxlib==0.4.23
jedi==0.19.1
Jinja2==3.1.3
jupyter-bokeh==3.0.7
jupyter_client==8.6.0
jupyter_core==5.7.1
jupyterlab-widgets==3.0.9
kiwisolver==1.4.5
linkify-it-py==2.0.2
Markdown==3.5.2
markdown-it-py==3.0.0
MarkupSafe==2.1.4
matplotlib==3.8.2
matplotlib-inline==0.1.6
mdit-py-plugins==0.4.0
mdurl==0.1.2
ml-dtypes==0.3.2
mypy-extensions==1.0.0
nest-asyncio==1.6.0
nodeenv==1.8.0
numpy==1.26.3
omegaconf==2.3.0
opt-einsum==3.3.0
packaging==23.2
pandas==2.2.0
panel==1.3.8
param==2.0.2
parso==0.8.3
pathspec==0.12.1
pexpect==4.9.0
pillow==10.2.0
platformdirs==4.1.0
pluggy==1.3.0
pre-commit==3.6.0
prompt-toolkit==3.0.43
psutil==5.9.8
ptyprocess==0.7.0
pure-eval==0.2.2
pyarrow==15.0.0
pyct==0.5.0
Pygments==2.17.2
pynput==1.7.6
pyparsing==3.1.1
PyQt5==5.15.10
PyQt5-Qt5==5.15.2
PyQt5-sip==12.13.0
pytest==7.4.4
python-dateutil==2.8.2
python-engineio==4.8.2
python-socketio==5.11.0
python-xlib==0.33
pytz==2023.4
pyviz_comms==3.0.1
PyYAML==6.0.1
pyzmq==25.1.2
requests==2.31.0
scipy==1.12.0
simple-websocket==1.0.0
six==1.16.0
stack-data==0.6.3
tomli==2.0.1
tornado==6.4
tqdm==4.66.1
traitlets==5.14.1
typing_extensions==4.9.0
tzdata==2023.4
uc-micro-py==1.0.2
urllib3==2.1.0
virtualenv==20.25.0
wcwidth==0.2.13
webencodings==0.5.1
Werkzeug==3.0.1
widgetsnbextension==4.0.9
wsproto==1.2.0
xyzservices==2023.10.1
24 changes: 24 additions & 0 deletions socket_training/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import socket
import pickle

HEADER = 64
PORT = 8080
FORMAT = 'utf-8'
SERVER = '10.204.2.189'
ADDR = (SERVER, PORT)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)

def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length)
client.send(message)

send("Hello World")
send("Hello Swag")
send("Hello Yo")
send("!DISCONNECT")
45 changes: 45 additions & 0 deletions socket_training/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import socket
import threading


HEADER = 64
PORT = 8080
SERVER = '10.204.2.189'
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(f"{ADDR = }")
server.bind(ADDR)

def handle_client(conn, addr):
print(f"[NEW COONECTION] {addr} connected.")

connected = True
while connected:
msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MESSAGE:
connected = False

print(f"[{addr}] {msg}")

conn.close()


def start():
server.listen()
print(f"[LISTENING] Server listening on {SERVER}")
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f"[ACTIVE CONNECTIONS] {threading.active_count() - 1}")


if __name__ == '__main__':
print("[Starting] server is starting ...")
start()

0 comments on commit 8eb14db

Please sign in to comment.