Reputation: 43
import socket
server=port = 2160
client_socket = socket.socket(AF_INET, socket.SOCK_DGRAM)
input_s = 'hello, server!' # the message
client_socket.sendto(bytes(inpus_s, encoding='utf8'),('127.0.0.1', server_port)) # sending message
input_s_modified, adress = client_socket.recvfrom(65535) # receiving from server
print ('[CLIENT] Response from server {}, is: "{}"'.format(adress, str(input_s_modified.decode('utf8'))))
client_socket.close() # closing socket
Upvotes: 1
Views: 2139
Reputation: 43
# import socket
# Import the time module
from time import time
server_port = 2160
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Add prefix with client timestamp
input_s = str(time()) + '|hello, server!' # the message
client_socket.sendto(bytes(input_s, encoding='utf8'),('127.0.0.1', server_port)) #
sending message
input_s_modified, adress = client_socket.recvfrom(65535) # receiving from server
print ('[CLIENT] Response from server {}, is: "{}"'.format(adress,
str(input_s_modified.decode('utf8'))))
client_socket.close() # closing socket
Upvotes: 0
Reputation: 3619
You can get the time difference by sending the current timestamp, then having the server send back the difference between the server time and the time that it received in its data.
On the client side, you can prepend the data like so:
# Import the time module
from time import time
...
# Add prefix with client timestamp
input_s = str(time()) + '|hello, server!' # the message
And in the backend / server side code, you can modify the data like so:
# Get the client time
client_time = data.split(b"|")[0]
# Get the time difference
tiime_dif = time() - float(client_time)
# Modify the send data to include the time difference
connection.sendall(str(time_dif).encode() + "|".encode() + data)
When you put all of this info together, you end up with something like this:
# CLIENT
import socket
# Import the time module
from time import time
server=port = 2160
client_socket = socket.socket(AF_INET, socket.SOCK_DGRAM)
# Add prefix with client timestamp
input_s = str(time()) + '|hello, server!' # the message
client_socket.sendto(bytes(inpus_s, encoding='utf8'),('127.0.0.1', server_port)) # sending message
input_s_modified, adress = client_socket.recvfrom(65535) # receiving from server
print ('[CLIENT] Response from server {}, is: "{}"'.format(adress, str(input_s_modified.decode('utf8'))))
client_socket.close() # closing socket
# SERVER
# Get the client time
client_time = data.split(b"|")[0]
# Get the time difference
tiime_dif = time() - float(client_time)
# Modify the send data to include the time difference
connection.sendall(str(time_dif).encode() + "|".encode() + data)
Upvotes: 1