I have an issue where i try to have the client loop till it receives a connection from server. When i try to put the socket as global variable. Then use start to establish a connect, it connects. but when i use send with a valid data variable, i get this error: Socket is not connected. this works only if i put all the code in start and it'll send information. This will code work if i start the server before the client, but not the other way around. Trying to get it working for any scenario.
#global variable definition
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def start(host, port):
connected = False
while not connected:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
connected = True
print("Connected")
except:
print("connecting again!")
sleep(5)
#i do put a valid json to send!
def send():
try:
s.sendall(json.dumps(data).encode('utf-8'))