Sockets and threads problem

Sockets and threads problem

Postby DjDexter5GHz » Thu Apr 11, 2013 11:28 am

I am new to python and i have problem. I tried searching on google but nothing helped.
When i run this code interface comes up fine, but when i connect to server i get infinite loop on waiting data, and i can't press any button on interface.
Why is this happennenig? I started new thread to do this, shouldn't main thread with gui be continuing its work? This code connects to Java server (which code is ok).
If nobody knows whats the problem here, can that person provide me some kind of replacement for this socket receiving function?
Thanks in advance!

Code: Select all
from tkinter import *
from threading import *
import time
import socket
import sys

comPort=0
msg=""
name=socket.gethostbyname(socket.gethostname())
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
testArea=0
thread=0

def threadFunc():
    global sock
    global textArea
    data=""
    while(data==""):
        try:
            data=str((sock.recvfrom(256)),"UTF-8")
        except BlockingIOError:
            print("failed")
            time.sleep(1)
            data=""
    textArea.add((data+"\n"))

def sendMsg():
    global sock
    message=name+": "+msg.get()+"\n"
    sock.send(bytes(message,"UTF-8"))

def aboutCommand():
    messagebox.showinfo(title="About",message="This is first (serious) aplication in python.")

def helpCommand():
    messagebox.showinfo(title="Help",message="BAZINGA!")

def Connect():
    global sock
    global thread
    sock.connect((serverIPString.get(), 16000))
    sock.send(bytes("#connect request#\n",'UTF-8'))
    data=sock.recvfrom(256)
    reply=str(data[0],"UTF-8")
    answer=(reply.split("#",3))
    if(answer[1]!="connected"):
        messagebox.showinfo(title="Error",message="Connection failed!")
        return
    sock.close();
    sock=None
    comPort=int(answer[2])       #for new connection
    sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((serverIPString.get(), comPort))
    sock.setblocking(0)
    thread=Thread(target=threadFunc())
    thread.start()

def Disconnect():
    global sock
    sock.send(bytes("#END#\n","UTF-8"))
    sock.close()

def ExitApplication():
    Disconnect()
    mGui.destroy()
    return

mGui=Tk()
msg=StringVar()
serverIPString=StringVar()
mGui.geometry("400x600+200+20")
mGui.title("Chat client v1.0")

labServer=Label(mGui,text="Server IP adress: ").pack()
entServer=Entry(mGui,textvariable=serverIPString).pack()
btnConnect=Button(mGui,text="Connect",fg="red",bg="blue",command=Connect).pack()
btnDisconnect=Button(mGui,text="Disconnect",fg="red",bg="blue",command=Disconnect).pack()
textArea=Text(mGui,bg="yellow").pack()
entMessage=Entry(mGui,textvariable=msg).pack()
btnSendMsg=Button(mGui,text="Send message",fg="white",bg="black",command=sendMsg).pack()

menuBar=Menu(mGui)

fileMenu=Menu(menuBar,tearoff=0)
fileMenu.add_command(label="Exit",command=ExitApplication)
menuBar.add_cascade(label="File",menu=fileMenu)

optionsMenu=Menu(menuBar,tearoff=0)
optionsMenu.add_command(label="Change chat name")
optionsMenu.add_command(label="Connect",command=Connect)
optionsMenu.add_command(label="Disconnect",command=Disconnect)
menuBar.add_cascade(label="Options",menu=optionsMenu)

helpMenu=Menu(menuBar,tearoff=0)
helpMenu.add_command(label="Help",command=helpCommand)
helpMenu.add_command(label="About",command=aboutCommand)
menuBar.add_cascade(label="Info",menu=helpMenu)
mGui.config(menu=menuBar)
mGui.mainloop()

DjDexter5GHz
 
Posts: 2
Joined: Thu Apr 11, 2013 11:22 am

Re: Sockets and threads problem

Postby setrofim » Thu Apr 11, 2013 11:53 am

Code: Select all
    thread=Thread(target=threadFunc())

should be
Code: Select all
    thread=Thread(target=threadFunc)

i.e. you specify the function as the target, not the result of calling that function.

There are a number of other issues with that code (e.g. globals in threads is a no-no). Save yourself a lot of pain and use Twisted
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Sockets and threads problem

Postby DjDexter5GHz » Thu Apr 11, 2013 1:36 pm

Thank you very much! I haven't seen that error!
DjDexter5GHz
 
Posts: 2
Joined: Thu Apr 11, 2013 11:22 am


Return to Web Development

Who is online

Users browsing this forum: No registered users and 6 guests