Server-to-client communication

0
Hello, I'm starting programming now, and in networking class I need to create UDP and TCP communication between client and server in Python. I have achieved continuous communication of UDP client and server, with only the server sending messages. However, to do one of the exercises I need to make both the client and the server send messages, but I am not able to send a message from the server to the client. I'll put my client-to-server communication codes, please help me modify so that I can send message from server to client

Server

import socket
host = '172.19.29.83'
port = 7000
addr = (host, port)
serv_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serv_socket.bind(addr)
serv_socket.listen(10)
print ('aguardando conexao')
con, cliente = serv_socket.accept()
print ('conectado')
print ("aguardando mensagem")
recebe = con.recv(1024)
while recebe!= "Sair": #Socket só encerra quando cliente enviar Sair
    print ("mensagem recebida: ", recebe)
    recebe = con.recv(1024)
else:
    serv_socket.close()

Customer

import socket
ip = '172.19.29.83'
port = 7000
addr = ((ip,port))
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(addr)
mensagem = input("digite uma mensagem para enviar ao servidor")
while mensagem != "Sair":#Socket só encerra quando cliente enviar Sair
    mensagem = input("digite uma mensagem para enviar ao servidor")
    client_socket.send(mensagem.encode())
    print ('mensagem enviada')
else:
    client_socket.close()

With these codes, despite giving a very strange error in the end, I was able to get the client to send as many messages as he wants to the server, until the client types Exit and then closes the processes.

How do I pro-send a message to the client and receive the client?

    
asked by anonymous 13.03.2018 / 22:36

0 answers