Searching the web I found this code for communication between a client and a server Server
#!/usr/bin/python
import socket #utilidades de red y conexion
ip = "192.168.0.18"
puerto = 4445
dataConection = (ip, puerto)
conexionesMaximas = 5
socketServidor = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socketServidor.bind(dataConection)
socketServidor.listen(conexionesMaximas)
print("Esperando conexiones en %s:%s" %(ip, puerto))
cliente, direccion = socketServidor.accept()
print("Conexion establecida con %s:%s" %(direccion[0], direccion[1]))
while True:
datos = cliente.recv(1024)
if datos == "exit":
msg ="exit"
cliente.sendall(msg.encode("utf-8"))
break
print("RECIBIDO: %s" %datos)
msg='--Recibido--'
cliente.sendall(msg.encode("utf-8"))
print("------- CONEXIÓN CERRADA ---------")
socketServidor.close()
customer
#!/usr/bin/python
import socket #utilidades de red y conexion
ipServidor = "192.168.0.18" #es lo mismo que "localhost" o "0.0.0.0"
puertoServidor = 4445
cliente = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cliente.connect((ipServidor, puertoServidor))
print("Conectado con el servidor ---> %s:%s" %(ipServidor, puertoServidor))
while True:
msg = input("> ")
cliente.sendall(msg.encode("utf-8"))
respuesta = cliente.recv(4096)
print(respuesta)
if respuesta == "exit":
break
print("------- CONEXIÓN CERRADA ---------")
cliente.close()
The program made some changes because originally it did not work but the only thing that I can not make it work is the fragment of code where it has to end when entering "exit", maybe my problem is very insignificant but I do not know much about python I hope you can help me. The code if it establishes communication, sends and receives messages what does not work is the fact that it does not end when you enter exit.