Getting an error in tkinter and message encoding - python

I Was working a tcp chat room and when i run it it shows nicknames and who joined but the message doesnt go through and after trying to send a message the 4th time it show the error
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\f\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "besthost.py", line 59, in write
self.sock.send(message.encode('utf-8'))
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
The host file is
import socket
import threading
import tkinter
import tkinter.scrolledtext
from tkinter import simpledialog
HOST = '127.0.0.1'
PORT = 9091
class Client:
def __init__(self, host, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
msg = tkinter.Tk()
msg.withdraw()
self.nickname = simpledialog.askstring("nickname", "please choose a nickname", parent=msg)
self.gui_done = False
self.running = True
gui_thread = threading.Thread(target=self.gui_loop)
receive_thread = threading.Thread(target=self.receive)
gui_thread.start()
receive_thread.start()
def gui_loop(self):
self.win = tkinter.Tk()
self.win.configure(bg="darkolivegreen")
self.chat_label = tkinter.Label(self.win, text="chat", bg="lightgray")
self.chat_label.config(font=("arial", 12))
self.chat_label.pack(padx=20, pady=5)
self.text_area = tkinter.scrolledtext.ScrolledText(self.win)
self.text_area.pack(padx=20, pady=5)
self.text_area.config(state='disabled')
self.msg_label = tkinter.Label(self.win, text="message", bg="lightgray")
self.msg_label.config(font=("arial", 12))
self.msg_label.pack(padx=20, pady=5)
self.input_area = tkinter.Text(self.win, height =3)
self.input_area.pack(padx=20,pady=5)
self.send_button = tkinter.Button(self.win, text="send", command=self.write)
self.send_button.config(font=("arial", 12))
self.send_button.pack(padx=20, pady=5)
self.gui_done = True
self.win.protocol("WM_DELETE_WINDOW", self.stop)
self.win.mainloop()
def write(self):
message = f"{self.nickname}:{self.input_area.get('1.0', 'end')}"
self.sock.send(message.encode('utf-8'))
self.input_area.delete('1.0', 'end')
def stop(self):
self.running = False
self.win.destroy()
self.sock.close()
exit(0)
def receive(self):
while self.running:
try:
message = self.sock.recv(1024).decode('utf-8')
if message=='NICK':
self.sock.send(self.nickname.encode('utf-8'))
else:
if self.gui_done:
self.text_area.config(state='normal')
self.text_area.insert('end', message)
self.text_area.yview('end')
self.text_area.config(state='disabled')
except ConnectionAbortedError:
break
except:
print("error")
self.sock.close()
break
client = Client(HOST, PORT)
The server file is
import socket
import threading
HOST = '127.0.0.1'
PORT = 9091
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen()
clients = []
nicknames = []
def broadcast(message):
for client in clients:
client.send(message)
def handle(client):
while True:
try:
message = client.recv(1024).decode('utf-8')
print(f"{nicknames[client.index(client)]})")
broadcast(message)
except:
index = clients.index(client)
clients.remove(client)
client.close()
nickname = nicknames[index]
nicknames.remove(nickname)
break
def receive():
while True:
client, address = server.accept()
print(f"connected with {str(address)}!")
client.send("NICK".encode('utf-8'))
nickname = client.recv(1024)
nicknames.append(nickname)
clients.append(client)
print(f"nicknames of client is {nickname}")
broadcast(f"{nickname} connected successfully!\n".encode('utf-8'))
client.send("connected to the server".encode('utf-8'))
thread = threading.Thread(target=handle, args=(client,))
thread.start()
print("server running")
receive()
I tried searching but i didnt see anyone with this issue and i dont think there are any programming errors
If anyone has an idea whats wrong please let me know .
Thanks

Your the biggest mistake is except: without printing information about problem.
I use
except Exception as ex:
print (ex)
# ... rest ...
and first it shows me
'socket' object has no attribute 'index'
because you forgot s in word clients in line
print(f"{nicknames[clients.index(client)]})")
Next it shows me
a bytes-like object is required, not 'str'
because you send str instead of bytes in next line - and you need encode()
broadcast(message.encode('utf-8'))
After these changes it start working for me.
def handle(client):
while True:
try:
message = client.recv(1024).decode('utf-8') # convert bytes to string
print(f"{nicknames[clients.index(client)]})") # forgot `s`
broadcast(message.encode('utf-8')) # convert string back to bytes
except Exception as ex:
print('Exception:', ex)
index = clients.index(client)
clients.remove(client)
client.close()
nickname = nicknames[index]
nicknames.remove(nickname)
break

Related

Cannot connect the python application with sockets properly to a public IP address

I developed a simple chat app that allows users to connect to a server and chat. This is the code for the server.
import os
from datetime import datetime
def log_file_name():
n = str(datetime.now())
m = n.replace(':','_').replace(' ','---')
l = m.split('.')[0]
l = f'{l}.txt'
return l
HOST = '127.0.0.1'
PORT = 10001
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#print(type(server_socket))
server.bind((HOST, PORT))
server.listen()
print('server_initiated...')
logfile = os.path.join('logs', log_file_name())
with open(logfile, 'w') as fh:
fh.write(f'{str(datetime.now())} log file initiated!\n\n')
clients = []
nicknames = []
#broadcast
def broadcast(message):
#print(clients)
for client in clients:
client.send(message)
def handle(client):
while True:
try:
message = client.recv(1024)
#print(f'{nicknames[clients.index(client)]} says {message}')
broadcast(message)
with open(logfile, 'a') as fh:
fh.write(f"{nicknames[clients.index(client)]} says {message.decode('utf-8')}\n")
except Exception:
index = clients.index(client)
clients.remove(client)
client.close()
left_nick = nicknames[index]
leaving_notes = f"{left_nick} just left the chat!! -- time {str(datetime.now()).split(' ')[1].split('.')[0]}"
broadcast(leaving_notes.encode('utf-8'))
with open(logfile,'a') as fh:
fh.write(f'{leaving_notes}\n')
nicknames.remove(left_nick)
break
#recieve message
def recieve():
while True:
client, address = server.accept()
#print(dir(client))
#print(client._io_refs)
#print(f'Connected with {str(address)}!!')
with open(logfile, 'a') as fh:
fh.write(f'Connected with {str(address)}!!\n')
client.send('Nickname'.encode('utf-8'))
nickname = client.recv(512).decode('utf-8')
nicknames.append(nickname)
clients.append(client)
#print(f'from 1st step clients are {clients}')
#print(f'Nickname of the client is {nickname}')
with open(logfile, 'a') as fh:
fh.write(f'Nickname of the client is {nickname}\n')
broadcast(f'Notice from server!! {nickname} just connected to the server!\n'.encode('utf-8'))
client.send(f'Connected to the server as {nickname}\n'.encode('utf-8'))
thread = threading.Thread(target=handle, args=(client,))
thread.start()
recieve()
Then I exposed that port to public using ngrok (ngrok http 10001), and it generated a public ip for me.
I coded a simple client gui using tkinter and I tried to connect the socket in to to above ngrok server I got. But I cannot broadcast messages. When I try to broadcast a message the GUI automatically closes. The reason is it meets a ConnectionAbortError.
Code for client-
import threading
import tkinter
import socket
import tkinter.simpledialog
import tkinter.scrolledtext
import os
#HOST = socket.gethostbyname(socket.gethostname())
#PORT = 10001
HOST = '3a7d-2402-4000-1245-cd8a-5d64-9c3f-25a3-79a4.in.ngrok.io'
PORT = 443 #or 80
class Client:
def __init__(self,host,port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
msg = tkinter.Tk()
msg.withdraw()
self.nickname = tkinter.simpledialog.askstring('Nickname', 'Please enter a nickname', parent=msg)
self.gui_done = False
self.running = True
gui_t=threading.Thread(target=self.gui_loop)
recieve_t=threading.Thread(target=self.recieve)
gui_t.start()
recieve_t.start()
def gui_loop(self):
self.win = tkinter.Tk()
self.win.configure(bg='lightgray')
self.chat_label = tkinter.Label(self.win, text='Chat:', bg='lightgray')
self.chat_label.config(font=('Arial',12))
self.chat_label.pack(padx=20, pady=5)
self.text_area = tkinter.scrolledtext.ScrolledText(self.win)
self.text_area.pack(padx=20,pady=5)
#self.text_area.config(state='disabled')
self.msg_label = tkinter.Label(self.win, text='Message:', bg='lightgray')
self.msg_label.config(font=('Arial', 12))
self.msg_label.pack(padx=20, pady=5)
self.input_area = tkinter.Text(self.win, height=3)
self.input_area.pack(padx=20, pady=5)
self.send_button = tkinter.Button(self.win, text='Send', command=self.write)
self.send_button.config(font=('Arial', 12))
self.send_button.pack(padx=20, pady=5)
self.gui_done = True
self.win.protocol('WM_DELETE_WINDOW', self.stop)
self.win.mainloop()
def write(self):
message = f"{self.nickname}: {self.input_area.get('1.0', 'end')}"
#print(message)
self.sock.send(message.encode('utf-8'))
self.input_area.delete('1.0','end')
def recieve(self):
while self.running:
try:
message = self.sock.recv(1024).decode('utf-8')
print(message)
if message == 'Nickname':
self.sock.send(self.nickname.encode('utf-8'))
else:
if self.gui_done:
#print('I hit here often')
self.text_area.config(state='normal')
self.text_area.insert('end', message)
self.text_area.yview('end')
#self.text_area.config(status='disabled') #never use this inside a loop, disabled blocks the code....
except ConnectionAbortedError: #when closed the window I come here..
print('Am i here?')
#break
os._exit(0)
#break
except:
break
print('Error')
self.sock.close()
break
def stop(self):
self.running = False
self.win.destroy()
self.sock.close()
exit(0)
client = Client(HOST, PORT)
If you run this please make sure to create a directory called logs to save the log of communication. Or else it will give an error(You can try it on localhost). I think I am doing something that doesn't work here. Can you point it out to me? or can you give me an alternative way of doing it?

Python Mutli-chat socket errors

I am writing a multi-chat which consists of the Client handler, the server and chat record. The Client should allow multiple chats. At the moment it doesn't allow for even one chat, I get an error message after the name has been entered.
This is the Client handler file
from socket import*
from codecs import decode
from chatrecord import ChatRecord
from threading import Thread
from time import ctime
class ClientHandler (Thread):
def __init__(self, client, record):
Thread.__init__(self)
self._client = client
self._record = record
def run(self):
self._client.send(str('Welcome to the chatroom!'))
self._name = decode(self._client.recv(BUFSIZE),CODE)
self._client.send(str(self._record),CODE)
while True:
message = decode(self._client.recv(BUFSIZE),CODE)
if not message:
print('Client disconnected')
self._client.close()
break
else:
message=self._name +'' + \
ctime()+'\n'+message
self._record.add(message)
self._client.send(str(self._record),CODE)
HOST ='localhost'
PORT = 5000
ADDRESS = (HOST,PORT)
BUFSIZE = 1024
CODE = 'ascii'
record = ChatRecord()
server = socket(AF_INET,SOCK_STREAM)
server.bind(ADDRESS)
server.listen(5)
while True:
print ('Waiting for connection...')
client,address = server.accept()
print ('...connected from:',address)
handler = ClientHandler(client,record)
handler.start()
This is the server file
from socket import *
from codecs import decode
HOST ='localhost'
PORT = 5000
BUFSIZE = 1024
ADDRESS = (HOST,PORT)
CODE = 'ascii'
server = socket(AF_INET,SOCK_STREAM)
server.connect(ADDRESS)
print (decode(server.recv(BUFSIZE), CODE))
name = raw_input('Enter your name:')
server.send(name)
while True:
record = server.recv(BUFSIZE)
if not record:
print ('Server disconnected')
break
print (record)
message = raw_input('>')
if not message:
print ('Server disconnected')
break
server.send(message, CODE)
server.close()
This is the Chartrecord
class ChatRecord(object):
def __init__(self):
self.data=[]
def add(self,s):
self.data.append(s)
def __str__(self):
if len(self.data)==0:
return 'No messages yet!'
else:
return'\n'.join(self.data)
This is the error message I get when running the chat record. I can enter a name then after that I get the error message below
Waiting for connection...
('...connected from:', ('127.0.0.1', 51774))
Waiting for connection...
Exception in thread Thread-1:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threadin g.py", line 532, in __bootstrap_inner
self.run()
File "/Users/basetsanamanele/Documents/workspace/HAAAAAAAFF/ClientHandler", line 17, in run
self._client.send(str(self._record),CODE)
TypeError: an integer is required
Please assist
Edit: Also, your server isn't accepting/listing for connections
You should make the server multithreaded so that it can send and receive at the same time. Here's how it might look:
import socket
import os
from threading import Thread
import thread
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(10)
serverThreads = []
while True:
print "Server is listening for connections..."
client, address = s.accept()
serverThreads.append(Thread(target=runserver, args=(client,address)).start())
s.close()
def runserver(client, address):
clients = set()
lockClients = threading.Lock()
print ("Connection from: " + address)
with lockClients:
clients.add(client)
try:
while True:
data = client.recv(1024)
if data:
print data.decode()
with lockClients:
for c in clients:
c.sendall(data)
else:
break
finally:
with lockClients:
clients.remove(client)
client.close()
When you send a string over TCP you need to encode it to bytes. So your client file should look like this instead:
def run(self):
self._client.send(('Welcome to the chatroom!').encode())
self._name = self._client.recv(BUFSIZE).decode()
self._client.send(str(self._record),CODE)
while True:
message = self._client.recv(BUFSIZE).decode()
if not message:
print('Client disconnected')
self._client.close()
break
else:
message=self._name +'' + \
ctime()+'\n'+message
self._record.add(message)
self._client.send(self._record.encode())
I much prefer the .encode() and .decode() syntax as it makes the code a little more readable IMO.

python3 OSError: [Errno 107] Transport endpoint is not connected

I try to make a chat on Python3. Here is my code:
import socket
import threading
print("Server starts working")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("", 57054))
sock.listen(2)
conn, addr = sock.accept()
def get_message():
while True:
data = sock.recv(1024).decode()
if len(data) != 0:
print("Some guy: ", data)
def send_message():
while True:
message = input()
if len(message) != 0:
message = str.encode(message)
sock.send(message)
print("You: ", message)
def run():
get_message_thread = threading.Thread(target=get_message())
send_message_thread = threading.Thread(target=send_message())
get_message_thread.daemon = True
send_message_thread.daemon = True
get_message_thread.start()
send_message_thread.start()
run()
sock.close()
But after the execution and sending a message from other client I get an error message:
Server starts working
Traceback (most recent call last):
File "/home/ptrknvk/Documents/Study/Python/chat/chat.py", line 40, in <module>
run()
File "/home/ptrknvk/Documents/Study/Python/chat/chat.py", line 30, in run
get_message_thread = threading.Thread(target=get_message())
File "/home/ptrknvk/Documents/Study/Python/chat/chat.py", line 15, in get_message
data = sock.recv(1024).decode()
OSError: [Errno 107] Transport endpoint is not connected
Process finished with exit code 1
I've read, that there are some troubles with sock.accept(), but everything's alright here, as I think.
Your program has many flaws. As zondo mentioned, you are incorrectly passing the target. They should be like threading.Thread(target=get_message). Second problem is, you should use conn (and not sock) for sending and receiving data. Third problem is, main thread was blocking at accept call and will wait for the connection. But soon as it accepts a connection, it will exit. From the main thread, you should wait for get_message_thread and send_message_thread. Try the modified code:
import socket
import threading
print("Server starts working")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("", 57054))
sock.listen(2)
conn, addr = sock.accept()
def get_message():
while True:
data = conn.recv(1024).decode()
if len(data) != 0:
print("Some guy: ", data)
def send_message():
while True:
message = input()
if len(message) != 0:
message = str.encode(message)
conn.send(message)
print("You: ", message)
def run():
get_message_thread = threading.Thread(target=get_message)
send_message_thread = threading.Thread(target=send_message)
get_message_thread.daemon = True
send_message_thread.daemon = True
get_message_thread.start()
send_message_thread.start()
get_message_thread.join()
send_message_thread.join()
run()
sock.close()

Python Socket client crashes after a restart

I have created a chat system using sockets , select and tkinter , however if the client restarts , it would fail to connect again until i restart the server.
This is the client code:
from tkinter import *
import tkinter.scrolledtext as tkst
import socket
import threading
import time
class myChatProgram(threading.Thread):
def __init__(self):
self.root = Tk()
self.myFrame = Frame(master = self.root)
self.myFrame.pack(fill='both', expand='yes')
self.chatArea = tkst.ScrolledText(
master = self.myFrame,
wrap = WORD,
width = 50,
height = 20
)
self.chatArea.configure(state='disabled')
self.chatArea.pack(padx=10, pady=10, fill=BOTH, expand=True)
self.bottomFrame = Frame(master = self.root)
self.bottomFrame.pack(fill='x', expand='yes' , side = "bottom")
self.entryBox = Entry(self.bottomFrame)
self.entryBox.pack(padx=10, pady=10,fill='x', expand='yes' , side = "left")
self.entryButton = Button(self.bottomFrame, text = "Send", command = self.sendText ,padx=10)
self.entryButton.pack(side = "right")
self.host = '192.168.1.150'
self.port = 50003
threading.Thread.__init__(self)
self.start()
self.root.mainloop()
def addText(self,message):
if message != "":
self.chatArea.configure(state='normal')
self.chatArea.insert(END, str(message) + "\n")
self.chatArea.configure(state='disabled')
def sendText(self):
message = bytes(self.entryBox.get(),"utf-8")
self.addText(self.entryBox.get())
if message:
self.mySocket.send(message)
self.entryBox.delete(0, END)
def run(self):
self.mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.mySocket.connect((self.host,self.port))
time.sleep(1)
while True:
self.addText(str(self.mySocket.recv(4096).decode("utf-8")))
myClass = myChatProgram()
And the server code is:
import socket
import select
import threading
class server(threading.Thread):
def __init__(self):
host = '192.168.1.150'
port = 50003
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.bind((host,port))
print("Your ip is {0} ".format(socket.gethostbyname(socket.gethostname())))
self.server.listen(5)
self.sockets = []
self.sockets.append(self.server)
def main(self):
while True:
inputList , outputList , errorList = select.select(self.sockets, [], self.sockets)
for mySocket in inputList:
if mySocket == self.server:
newClient , addr = self.server.accept()
print(addr , " Connected")
self.sockets.append(newClient)
else:
try:
data = mySocket.recv(4096)
if data:
self.broadcast(mySocket,data)
else:
print("Connection sent 0 bytes.")
mySocket.close()
self.sockets.remove(mySocket)
except socket.error , e:
print("Client disconnected while sending message.")
mySocket.close()
self.sockets.remove(mySocket)
continue
for mySocket in errorList:
print("Select flagged client as crashed.")
mySocket.close()
self.sockets.remove(mySocket)
continue
def broadcast(self,mySocket,message):
print("sending {0}".format(message))
for client in self.sockets:
if client != mySocket and socket != self.server:
try:
client.send(bytes(message,"utf-8"))
except:
client.close()
self.sockets.remove(client)
continue
myClass = server()
myClass.main()
The the error is :
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python33\lib\threading.py", line 901, in _bootstrap_inner
self.run()
File "F:\Sockets\chatClient.py", line 49, in run
self.mySocket.connect((self.host,self.port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
However it works fine unless the client restarts , then it crashes. Any ideas what is wrong?
I guess you have to set SO_REUSEADDR:
socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

close socket from client so that asyncore server detects it

I am building a chat server client system. a socket is created at the client side to connect to the server listening at a port. The problem is when I try to close the socket from the client side, the asyncore handle_close does not detect it.
Server code:
clients = {}
identity={}
class MainServerSocket(asyncore.dispatcher):
def __init__(self, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind(('',port))
self.listen(5)
def handle_accept(self):
newSocket, address = self.accept( )
clients[address] = newSocket
#try :
#hndl=newSocket.recv(1024)
#print "Connected from", address,hndl
#except :
#e = sys.exc_info()[1]
print "Connected from", address
handler=SecondaryServerSocket(newSocket)
handler.setAddr(address)
every = clients.values()
for one in every:
one.send('Server message:'+str(address)+' joined'+'\n')
class SecondaryServerSocket(asyncore.dispatcher_with_send):
def setAddr(self, addr):
self.addr = addr
def isid(self,txt):
pattern=re.compile('\*\$\$\$\*(.+)\*\$\$\$\*')
match=pattern.search(txt)
rv=0
if match :
self.hndl=match.group(1)
rv=1
if self.hndl not in identity.values():
identity[self.addr]=self.hndl
every=clients.values()
for one in every:
one.send('Server message:'+str(self.addr)+' is known as '+self.hndl+'\n')
else:
clients[self.addr].send("Server message: handle in use, retry. Disconnecting.")
self.close()
return rv #del clients[self.addr]
def handle_read(self):
receivedData = self.recv(8192)
if receivedData:
if not self.isid(receivedData):
#self.addr
every = clients.values()
for one in every:
one.send(receivedData)
else: self.close( )
def handle_close(self):
print "Disconnected from", self.getpeername( )
one = self.getpeername( )
del identity[one]
del clients[one]
every=clients.values()
try:
for one in every:
one.send('server message:'+str(self.addr)+' quit'+'\n')
except: pass
print "server started"
MainServerSocket(21567)
asyncore.loop( )
client code(relevant bits):
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.serv_ip=StringVar()
self.port=StringVar()
self.handle=StringVar()
self.serv_ip.set('127.0.0.1')
self.port.set('21567')
self.grid()
self.create_widgets()
self.lastname=""
self.currentname=""
self.delim=('-'*45)+'\n'
def connect(self):
if len(self.handle.get())>0 and not self.handle.get().isspace() :
HOST = self.serv_ip.get()
PORT = int(self.port.get())
#BUFSIZE = 1024
ADDR = (HOST, PORT)
try:
self.tcpCliSock = socket(AF_INET, SOCK_STREAM)
self.tcpCliSock.connect(ADDR)
idtext='*$$$*'+self.handle.get()+'*$$$*'
self.tcpCliSock.send(idtext)
self.add("sys message: CONNECTED TO "+str(HOST)+':'+str(PORT)+'\n')
self.socket()
self.con_button.configure(text="DISCONNECT!",command=self.disconnect)
self.entry_field.configure(state=NORMAL)
self.handle_entry.configure(state=DISABLED)
except:
e = sys.exc_info()[1]
self.add("sys message: ERROR CONNECTING to "+str(HOST)+':'+str(PORT)+" "+str(e)+'\n')
#pass
else : self.add("sys message: Enter handle/nickname and try again."+'\n')
def disconnect(self):
#try : self.tcpCliSock.shutdown(2)
#except: pass
#self.tcpCliSock.close()
self.tcpCliSock.send('')
self.tcpCliSock.close()
self.closeflag=1
self.add("sys message: Disconnected."+'\n')
self.con_button.configure(text="CONNECT!",command=self.connect)
self.entry_field.configure(state=DISABLED)
self.handle_entry.configure(state=NORMAL)
self.closeflag=0
#self.send_button.configure(state=DISABLED)
def socket(self):
def loop0():
while 1:
try:
if self.closeflag==0:
data = self.tcpCliSock.recv(BUFSIZE)
if data: self.add(data)
except:
e = sys.exc_info()[1]
self.add("ERROR: "+str(e)+' disconnecting...'+'\n')
self.con_button.configure(text="CONNECT!",command=self.connect)
self.entry_field.configure(state=DISABLED)
self.handle_entry.configure(state=NORMAL)
self.closeflag=0
thread.exit()
#self.disconnect()
break
if self.closeflag==1 :
self.tcpCliSock.close()
print "ping"
thread.exit()
thread.start_new_thread(loop0, ())
I know this is a lot of code but I am not sure which bit is causing the problems so decided to add all the parts I think could be responsible.when the disconnect function is called, the server doesnt detect a disconnection. And then after the disconnect, the connect function is called, then the client crashes.

Categories

Resources