0
I separated using a function, but the code does not receive the 'date'.
COMPLETE SERVER CODE:
import socket
import json
import sys
dicty = {
'the': 'o, a, os, as',
'hello': 'ola',
'hi': 'oi',
'good': 'bom',
'ugly': 'feio',
'bad': 'ruim, mal',
'pretty': 'lindo(a)',
'happy': 'feliz',
'sad': 'triste',
'yes': 'sim',
'no': 'nao'
}
def buscar():
data, adress = sock.recvfrom(4000)
if data.decode('ascii') in dicty:
sock.sendto(dicty[data.decode('ascii')].encode('ascii'), adress)
else:
sock.sendto(('0'.encode('ascii')), adress)
port = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = socket.gethostbyname(socket.gethostname())
sock.bind((host, port))
dado, adress = sock.recvfrom(3000)
while True:
if dado.decode('ascii') == 'Buscar':
buscar()
if dado.decode('ascii') != 'Buscar':
sys.exit(0)
COMPLETE CLIENT CODE:
import socket
from tkinter import *
import sys
port = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = socket.gethostbyname(socket.gethostname())
sock.connect((host,port))
def buscar(): #função de buscar palavras
sock.sendto(('Buscar'.encode('ascii')), (host,port))
sock.sendto((e1.get().encode('ascii')), (host, port))
data, adress = sock.recvfrom(4000)
if data.decode('ascii')=='0':
lb2["text"] = 'Esse nome nao existe em nosso dicionario. Ajude-nos cadastrando uma nova palavra'
else:
lb2["text"] = data.decode('ascii')
janela = Tk()
janela.geometry("900x500+100+100")
janela.title("DICIONARIO COLABORATIVO")
lb1 = Label(text="DICIONARIO INGLES-PORTUGUES") #titulo
lb1.place(x=4, y =30)
btn1 = Button(text="Buscar", command=buscar) #botao de busca
btn1.place(x=4, y=100)
lb2 = Label(text="") #local do significado
lb2.place(x=200, y=90)
lb3 = Label(text="Significado")
lb3.place(x=200,y=50)
e1 = Entry(janela) #recebe palavra
e1.place(x=4, y=50)
btn3 = Button(text='SAIR', command=janela.quit)
btn3.place(x=300,y=300)
janela.mainloop()
No errors. The server simply waits for the client to send something eternally.
Why you have "recvfrom(3000)" and also "recvfrom(4000)" on the server ? This will crash the program...but I think the first error is that the "bind" of port 3000 is missing in the client.
– zentrunix