I have a simple client that sends data to a server, and a simple server that takes the data from the client and just sends it back to the client, and so the client receives it again from the server and prints it (look the code below). But when I don't write anything at the client, when I only press ENTER and nothing else at all, the server seems to freeze and the commands below
var0 = connectionSocket.recv(1024)
don't get executed at all.
EDIT: Let me rephrase the question, I don't want to make any change at the client's code. But only write/change the server's code that way that when nothing is written by the client but a "" is sent to the server, I don't want the server to stop working. Is this even possible and if yes, how?
I have this code for the server:
from socket import *
serverPort = 11000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('127.0.0.1', serverPort))
serverSocket.listen(10)
while 1:
connectionSocket, addr = serverSocket.accept()
var0 = connectionSocket.recv(1024)
var0 = str(var0)
if(var0==""):
print("THIS COMMAND HERE IS WHAT I WANT TO BE EXCECUTED, BUT IT NEVER DOES")
connectionSocket.send(str.encode(var0))
connectionSocket.close()
And this code for the client:
import socket
serverName = "localhost"
serverPort = 11000
while 1:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((serverName, serverPort))
var = input("Write a sentence: ")
if(var==""):
print("ODD ISN'T IT?")
s.sendall(str.encode(var))
data = s.recv(1024)
data = str(data)
print(data)
s.close()