This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import pickle | |
import time | |
import threading | |
class SocketThread(threading.Thread): | |
def __init__(self, connection, client_info, buffer_size=1024, recv_timeout=5): | |
threading.Thread.__init__(self) | |
self.connection = connection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while True: | |
try: | |
connection, client_info = soc.accept() | |
print("New Connection from {client_info}.".format(client_info=client_info)) | |
socket_thread = SocketThread(connection=connection, | |
client_info=client_info, | |
buffer_size=1024, | |
recv_timeout=10) | |
socket_thread.start() | |
except: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
class SocketThread(threading.Thread): | |
def __init__(self, connection, client_info, buffer_size=1024, recv_timeout=5): | |
threading.Thread.__init__(self) | |
self.connection = connection | |
self.client_info = client_info | |
self.buffer_size = buffer_size | |
self.recv_timeout = recv_timeout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while True: | |
accept_timeout = 10 | |
soc.settimeout(accept_timeout) | |
try: | |
connection, address = soc.accept() | |
print("Connected to a client: {client_info}.".format(client_info=address)) | |
received_data = b'' | |
while True: | |
recv_start_time = time.time() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
connected = False | |
accept_timeout = 10 | |
soc.settimeout(accept_timeout) | |
try: | |
connection, address = soc.accept() | |
print("Connected to a client: {client_info}.".format(client_info=address)) | |
connected = True | |
except socket.timeout: | |
print("A socket.timeout exception occurred because the server did not receive any connection for {accept_timeout} seconds.".format(accept_timeout=accept_timeout)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
msg = "Another message from the client." | |
msg = pickle.dumps(msg) | |
soc.sendall(msg) | |
print("Client sent a message to the server.") | |
received_data = b'' | |
while str(received_data)[-2] != '.': | |
data = soc.recv(8) | |
received_data += data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import pickle | |
soc = socket.socket() | |
print("Socket is created.") | |
soc.connect(("localhost", 10000)) | |
print("Connected to the server.") | |
msg = "A message from the client." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import pickle | |
import time | |
def recv(connection, buffer_size, recv_timeout): | |
recv_start_time = time.time() # Time at which last chunk is received from the client. | |
received_data = b"" | |
while True: | |
try: | |
data = connection.recv(buffer_size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
received_data = b'' | |
if connected: | |
while True: | |
recv_start_time = time.time() | |
time_struct = time.gmtime() | |
date_time = "Waiting to Receive Data Starting from {day}/{month}/{year} {hour}:{minute}:{second} GMT".format(year=time_struct.tm_year, month=time_struct.tm_mon, day=time_struct.tm_mday, hour=time_struct.tm_hour, minute=time_struct.tm_min, second=time_struct.tm_sec) | |
print(date_time) | |
recv_timeout = 5 | |
received_data, status = recv(connection, 8, recv_timeout) | |
print("Received data from the client: {received_data}".format(received_data=received_data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def recv(connection, buffer_size, recv_timeout): | |
recv_start_time = time.time() # Time at which last chunk is received from the client. | |
received_data = b"" | |
while True: | |
try: | |
data = connection.recv(buffer_size) | |
received_data += data | |
if data == b'': # Nothing received from the client. | |
received_data = b"" |