Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions socketTest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import socket
import time

HEARDSIZE = 10

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(),1234))
Expand All @@ -7,5 +10,13 @@
while True:
clientSocket, address = s.accept()
print(f"Connection from {address} has beem established")
clientSocket.send(bytes("Welcome to the server","utf-8"))
clientSocket.close()

msg = "Welcome to the server!"
msg = f'{len(msg):<{HEARDSIZE}}'+msg

clientSocket.send(bytes(msg,"utf-8"))
while True:
time.sleep(3)
msg = f"The time is: {time.time()}"
msg = f'{len(msg):<{HEARDSIZE}}'+msg
clientSocket.send(bytes(msg, "utf-8"))
28 changes: 21 additions & 7 deletions socketTestclient.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import socket

HEARDSIZE = 10

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(),1234))

full_msg = ''

while True:
msg = s.recv(8)
if len(msg) <= 0:
break
full_msg += msg.decode("utf-8")
print(full_msg)

full_msg = ''
new_msg = True
while True:
msg = s.recv(16)
if new_msg:
print(f"New Message length: {msg[:HEARDSIZE]}")
msglen = int(msg[:HEARDSIZE])
new_msg = False

full_msg += msg.decode("utf-8")

if len(full_msg)-HEARDSIZE == msglen:
print("Full msg recvd")
print(full_msg[HEARDSIZE:])
new_msg = True
full_msg = ''

print(full_msg)