Synchronizing data transfer between client and server












0














I am working on a client/server tcp project, and I am wondering what a good method for "checking" that the server/client is ready for the data about to be sent.
This is because my client has a gui and I want the server to be ready for any input at any time, and should therefore make sure it isn't trying to send data whilst it is should be sending data



Here is my attempt



Client:



import socket
# Methods of a class
def receiveData(self, FROM=""):
if self.SENT_LAST:
print("DATA WAS LAST SENT BY CLIENT, NO CHECK REQUIRED")
else:
print("DATA LAST SENT BY CLIENT, CHECK REQUIRED")
self.sendPickledData("dataReady")
print("In receive data")
if FROM:
print("From:", FROM)
data = s.recv(4096)
try:
data = pickle.loads(data)

except pickle.UnpicklingError as err:
errorReport(err)
time.sleep(0.2)
self.SENT_LAST = False
if data != noData:
print("data :",data)
return data
else:
print("No data found")
return False

def sendPickledData(self, data, FROM=""):
if self.SENT_LAST:
print("DATA WAS LAST SENT BY CLIENT, CHECK REQUIRED")
checkData = self.receiveData("sendpickled")
print("CheckData :", checkData)
if checkData != "dataReady":
raise ValueError
else:
print("DATA LAST SENT BY CLIENT, NO CHECK REQUIRED")
print("In sendPickle, data :", data)
if FROM:
print("From:", FROM)
data = pickle.dumps(data)
s.sendall(data)
time.sleep(0.5)
self.SENT_LAST = True
print("Data sent")


Server:



import socket
def receiveData_v2(FROM, FUNC=):
global SENT_LAST, receive_count
receive_count += 1
print("Receive :",receive_count)
print("Receiving")
if SENT_LAST:
print("DATA WAS LAST SENT BY SERVER, NO CHECK REQUIRED")
if not SENT_LAST:
print("DATA LAST SENT BY SERVER, CHECK REQUIRED")
sendPickledData("dataReady")
SENT_LAST = False

print("In receive data")

if FROM:
print("From:", FROM)

data = conn.recv(4096)

try:
data = pickle.loads(data)
except pickle.UnpicklingError as err:
print("Pickling error:")
print(err)

if data == PROG_QUIT:
return data
elif data == noData:
return False
else:
if FUNC:
funcData = FUNC(data)
return funcData
else:
return data


def sendPickledData(data):
global SENT_LAST, send_count
send_count += 1
print("Send :",send_count)
print("sending")
if SENT_LAST:
print("DATA WAS LAST SENT BY SERVER, CHECK REQUIRED")
print("sent last")
checkData = receiveData_v2("sendpickled")
print("CheckData :", checkData)
if checkData != "dataReady":
raise ValueError
else:
print("DATA LAST SENT BY SERVER, NO CHECK REQUIRED")
print("In sendPickled,nSending:", data)
data = pickle.dumps(data)
conn.sendall(data)
SENT_LAST = True
conn.sendall(data)


Whenever I try to send data via this method, somehow it ends up de-synching and they will both end up trying to listen for data at the same time. Or receiveData (client) will receive only "dataReady"










share|improve this question






















  • I don't understand what the purpose is, sorry? You don't want the program to send data when it is supposed to?
    – immibis
    Nov 20 at 23:35












  • No. I want a way for my program to check that, when it sends data, the receiver is ready. Because if I send data, then a moment later need to send data again, I want to make sure the receiving end is prepared to receive those two separate pieces of data
    – James Green
    Nov 21 at 11:00
















0














I am working on a client/server tcp project, and I am wondering what a good method for "checking" that the server/client is ready for the data about to be sent.
This is because my client has a gui and I want the server to be ready for any input at any time, and should therefore make sure it isn't trying to send data whilst it is should be sending data



Here is my attempt



Client:



import socket
# Methods of a class
def receiveData(self, FROM=""):
if self.SENT_LAST:
print("DATA WAS LAST SENT BY CLIENT, NO CHECK REQUIRED")
else:
print("DATA LAST SENT BY CLIENT, CHECK REQUIRED")
self.sendPickledData("dataReady")
print("In receive data")
if FROM:
print("From:", FROM)
data = s.recv(4096)
try:
data = pickle.loads(data)

except pickle.UnpicklingError as err:
errorReport(err)
time.sleep(0.2)
self.SENT_LAST = False
if data != noData:
print("data :",data)
return data
else:
print("No data found")
return False

def sendPickledData(self, data, FROM=""):
if self.SENT_LAST:
print("DATA WAS LAST SENT BY CLIENT, CHECK REQUIRED")
checkData = self.receiveData("sendpickled")
print("CheckData :", checkData)
if checkData != "dataReady":
raise ValueError
else:
print("DATA LAST SENT BY CLIENT, NO CHECK REQUIRED")
print("In sendPickle, data :", data)
if FROM:
print("From:", FROM)
data = pickle.dumps(data)
s.sendall(data)
time.sleep(0.5)
self.SENT_LAST = True
print("Data sent")


Server:



import socket
def receiveData_v2(FROM, FUNC=):
global SENT_LAST, receive_count
receive_count += 1
print("Receive :",receive_count)
print("Receiving")
if SENT_LAST:
print("DATA WAS LAST SENT BY SERVER, NO CHECK REQUIRED")
if not SENT_LAST:
print("DATA LAST SENT BY SERVER, CHECK REQUIRED")
sendPickledData("dataReady")
SENT_LAST = False

print("In receive data")

if FROM:
print("From:", FROM)

data = conn.recv(4096)

try:
data = pickle.loads(data)
except pickle.UnpicklingError as err:
print("Pickling error:")
print(err)

if data == PROG_QUIT:
return data
elif data == noData:
return False
else:
if FUNC:
funcData = FUNC(data)
return funcData
else:
return data


def sendPickledData(data):
global SENT_LAST, send_count
send_count += 1
print("Send :",send_count)
print("sending")
if SENT_LAST:
print("DATA WAS LAST SENT BY SERVER, CHECK REQUIRED")
print("sent last")
checkData = receiveData_v2("sendpickled")
print("CheckData :", checkData)
if checkData != "dataReady":
raise ValueError
else:
print("DATA LAST SENT BY SERVER, NO CHECK REQUIRED")
print("In sendPickled,nSending:", data)
data = pickle.dumps(data)
conn.sendall(data)
SENT_LAST = True
conn.sendall(data)


Whenever I try to send data via this method, somehow it ends up de-synching and they will both end up trying to listen for data at the same time. Or receiveData (client) will receive only "dataReady"










share|improve this question






















  • I don't understand what the purpose is, sorry? You don't want the program to send data when it is supposed to?
    – immibis
    Nov 20 at 23:35












  • No. I want a way for my program to check that, when it sends data, the receiver is ready. Because if I send data, then a moment later need to send data again, I want to make sure the receiving end is prepared to receive those two separate pieces of data
    – James Green
    Nov 21 at 11:00














0












0








0







I am working on a client/server tcp project, and I am wondering what a good method for "checking" that the server/client is ready for the data about to be sent.
This is because my client has a gui and I want the server to be ready for any input at any time, and should therefore make sure it isn't trying to send data whilst it is should be sending data



Here is my attempt



Client:



import socket
# Methods of a class
def receiveData(self, FROM=""):
if self.SENT_LAST:
print("DATA WAS LAST SENT BY CLIENT, NO CHECK REQUIRED")
else:
print("DATA LAST SENT BY CLIENT, CHECK REQUIRED")
self.sendPickledData("dataReady")
print("In receive data")
if FROM:
print("From:", FROM)
data = s.recv(4096)
try:
data = pickle.loads(data)

except pickle.UnpicklingError as err:
errorReport(err)
time.sleep(0.2)
self.SENT_LAST = False
if data != noData:
print("data :",data)
return data
else:
print("No data found")
return False

def sendPickledData(self, data, FROM=""):
if self.SENT_LAST:
print("DATA WAS LAST SENT BY CLIENT, CHECK REQUIRED")
checkData = self.receiveData("sendpickled")
print("CheckData :", checkData)
if checkData != "dataReady":
raise ValueError
else:
print("DATA LAST SENT BY CLIENT, NO CHECK REQUIRED")
print("In sendPickle, data :", data)
if FROM:
print("From:", FROM)
data = pickle.dumps(data)
s.sendall(data)
time.sleep(0.5)
self.SENT_LAST = True
print("Data sent")


Server:



import socket
def receiveData_v2(FROM, FUNC=):
global SENT_LAST, receive_count
receive_count += 1
print("Receive :",receive_count)
print("Receiving")
if SENT_LAST:
print("DATA WAS LAST SENT BY SERVER, NO CHECK REQUIRED")
if not SENT_LAST:
print("DATA LAST SENT BY SERVER, CHECK REQUIRED")
sendPickledData("dataReady")
SENT_LAST = False

print("In receive data")

if FROM:
print("From:", FROM)

data = conn.recv(4096)

try:
data = pickle.loads(data)
except pickle.UnpicklingError as err:
print("Pickling error:")
print(err)

if data == PROG_QUIT:
return data
elif data == noData:
return False
else:
if FUNC:
funcData = FUNC(data)
return funcData
else:
return data


def sendPickledData(data):
global SENT_LAST, send_count
send_count += 1
print("Send :",send_count)
print("sending")
if SENT_LAST:
print("DATA WAS LAST SENT BY SERVER, CHECK REQUIRED")
print("sent last")
checkData = receiveData_v2("sendpickled")
print("CheckData :", checkData)
if checkData != "dataReady":
raise ValueError
else:
print("DATA LAST SENT BY SERVER, NO CHECK REQUIRED")
print("In sendPickled,nSending:", data)
data = pickle.dumps(data)
conn.sendall(data)
SENT_LAST = True
conn.sendall(data)


Whenever I try to send data via this method, somehow it ends up de-synching and they will both end up trying to listen for data at the same time. Or receiveData (client) will receive only "dataReady"










share|improve this question













I am working on a client/server tcp project, and I am wondering what a good method for "checking" that the server/client is ready for the data about to be sent.
This is because my client has a gui and I want the server to be ready for any input at any time, and should therefore make sure it isn't trying to send data whilst it is should be sending data



Here is my attempt



Client:



import socket
# Methods of a class
def receiveData(self, FROM=""):
if self.SENT_LAST:
print("DATA WAS LAST SENT BY CLIENT, NO CHECK REQUIRED")
else:
print("DATA LAST SENT BY CLIENT, CHECK REQUIRED")
self.sendPickledData("dataReady")
print("In receive data")
if FROM:
print("From:", FROM)
data = s.recv(4096)
try:
data = pickle.loads(data)

except pickle.UnpicklingError as err:
errorReport(err)
time.sleep(0.2)
self.SENT_LAST = False
if data != noData:
print("data :",data)
return data
else:
print("No data found")
return False

def sendPickledData(self, data, FROM=""):
if self.SENT_LAST:
print("DATA WAS LAST SENT BY CLIENT, CHECK REQUIRED")
checkData = self.receiveData("sendpickled")
print("CheckData :", checkData)
if checkData != "dataReady":
raise ValueError
else:
print("DATA LAST SENT BY CLIENT, NO CHECK REQUIRED")
print("In sendPickle, data :", data)
if FROM:
print("From:", FROM)
data = pickle.dumps(data)
s.sendall(data)
time.sleep(0.5)
self.SENT_LAST = True
print("Data sent")


Server:



import socket
def receiveData_v2(FROM, FUNC=):
global SENT_LAST, receive_count
receive_count += 1
print("Receive :",receive_count)
print("Receiving")
if SENT_LAST:
print("DATA WAS LAST SENT BY SERVER, NO CHECK REQUIRED")
if not SENT_LAST:
print("DATA LAST SENT BY SERVER, CHECK REQUIRED")
sendPickledData("dataReady")
SENT_LAST = False

print("In receive data")

if FROM:
print("From:", FROM)

data = conn.recv(4096)

try:
data = pickle.loads(data)
except pickle.UnpicklingError as err:
print("Pickling error:")
print(err)

if data == PROG_QUIT:
return data
elif data == noData:
return False
else:
if FUNC:
funcData = FUNC(data)
return funcData
else:
return data


def sendPickledData(data):
global SENT_LAST, send_count
send_count += 1
print("Send :",send_count)
print("sending")
if SENT_LAST:
print("DATA WAS LAST SENT BY SERVER, CHECK REQUIRED")
print("sent last")
checkData = receiveData_v2("sendpickled")
print("CheckData :", checkData)
if checkData != "dataReady":
raise ValueError
else:
print("DATA LAST SENT BY SERVER, NO CHECK REQUIRED")
print("In sendPickled,nSending:", data)
data = pickle.dumps(data)
conn.sendall(data)
SENT_LAST = True
conn.sendall(data)


Whenever I try to send data via this method, somehow it ends up de-synching and they will both end up trying to listen for data at the same time. Or receiveData (client) will receive only "dataReady"







python-3.x sockets tcp server client






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 23:01









James Green

407




407












  • I don't understand what the purpose is, sorry? You don't want the program to send data when it is supposed to?
    – immibis
    Nov 20 at 23:35












  • No. I want a way for my program to check that, when it sends data, the receiver is ready. Because if I send data, then a moment later need to send data again, I want to make sure the receiving end is prepared to receive those two separate pieces of data
    – James Green
    Nov 21 at 11:00


















  • I don't understand what the purpose is, sorry? You don't want the program to send data when it is supposed to?
    – immibis
    Nov 20 at 23:35












  • No. I want a way for my program to check that, when it sends data, the receiver is ready. Because if I send data, then a moment later need to send data again, I want to make sure the receiving end is prepared to receive those two separate pieces of data
    – James Green
    Nov 21 at 11:00
















I don't understand what the purpose is, sorry? You don't want the program to send data when it is supposed to?
– immibis
Nov 20 at 23:35






I don't understand what the purpose is, sorry? You don't want the program to send data when it is supposed to?
– immibis
Nov 20 at 23:35














No. I want a way for my program to check that, when it sends data, the receiver is ready. Because if I send data, then a moment later need to send data again, I want to make sure the receiving end is prepared to receive those two separate pieces of data
– James Green
Nov 21 at 11:00




No. I want a way for my program to check that, when it sends data, the receiver is ready. Because if I send data, then a moment later need to send data again, I want to make sure the receiving end is prepared to receive those two separate pieces of data
– James Green
Nov 21 at 11:00

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402894%2fsynchronizing-data-transfer-between-client-and-server%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402894%2fsynchronizing-data-transfer-between-client-and-server%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'