RESOLVED Client python server 3

-1

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.

    
asked by dowar 06.11.2018 в 10:40
source

2 answers

1

Problem solved, so are the while.

The modifications were made in the if. server

while True:
    datos = cliente.recv(1024) #El número indica el número maximo de bytes
    if datos == b'exit': #se agrego  una b antes del mensaje
        msg ="exit"
        cliente.sendall(msg.encode("utf-8"))
        break
    print("RECIBIDO: %s" %datos)
    msg='--Recibido--'

customer

while True:
    msg = input("> ")
    cliente.sendall(msg.encode("utf-8"))
    respuesta = cliente.recv(4096)
    print(respuesta)
    if respuesta == b'exit': #se agrego  una b antes del mensaje
        break
cliente.sendall(msg.encode("utf-8"))
    
answered by 06.11.2018 в 20:43
0

As I understand it, both client and server should end when you type "exit" in the client. But it works like this: When the server receives "exit" , it returns it to the client, and the server ends. In turn, the client receives "exit" , ends.

The problem I see is that the server only ends and sends "exit" to the client when it receives "b's'" , not "exit" . It could be that this was part of the original code.

# Esta es la condición para que el servidor termine y envíe
# "exit" al cliente
if datos == "b's'": 
        msg ="exit"
        cliente.sendall(msg.encode("utf-8"))
        break
    
answered by 06.11.2018 в 13:54