[Tutor] Connections with socket, Tkinter

Jeremiah Rushton Jeremiah.Rushton at gmail.com
Fri Oct 22 01:03:42 CEST 2004


I'm using threading, tkinter, and socket to make an 'instant
messaging' program.  Here  is the code for the client...
------------------------------------------------------------------------------------------
from Tkinter import *
from socket import *
from threading import *

class Receive(Thread):
    
    def __init__(self,client):
        Thread.__init__(self)
        while 1:
            try:
                text = client.recv(1024)
                App.__init__.gettext.configure(state='normal')
                App.__init__.gettext.insert(END,'%s\n'%text)
                App.__init__.gettext.configure(state='disabled')
            except:
                pass

class App:
    client = socket(AF_INET,SOCK_STREAM)
    ip = 'localhost'
    server = (ip, 285)
    client.connect(server)
    
    def __init__(self, master):
        frame = Frame(master)
        frame.grid(row=2,column=3)
        
        self.quitbutton = Button(frame, text="QUIT", fg='red',
command=frame.quit)
        self.quitbutton.grid(row=1,column=2)
        
        self.button = Button(frame, text="SEND", command=self.Send)
        self.button.grid(row=1,column=1)
        
        self.sendtext = Entry(frame,width=60)
        self.sendtext.grid(row=1,column=0)
        
        gettext = Text(frame,height=10,width=80,wrap=WORD)
        self.gettext = gettext
        gettext.grid(row=0,columnspan=3)
        gettext.insert(END,'Welcome to my Instant Messaging Program\n')
        gettext.configure(state='disabled')
        
    def Send(self):
        self.gettext.configure(state='normal')
        text = self.sendtext.get()
        self.gettext.insert(END,'%s\n'%text)
        self.sendtext.delete(0,END)
        self.client.send(text)
        self.gettext.configure(state='disabled')
        

root = Tk()
root.title('Client')
app = App(root)
root.mainloop()
------------------------------------------------------------------------------------------

And here is the code for the server side...

------------------------------------------------------------------------------------------
from Tkinter import *
from socket import *
from threading import *

class Receive(Thread):
    
    def __init__(self, server, gettext):
        Thread.__init__(self)
        self.server = server
        self.gettext = gettext

    def run(self):
        while 1:
            try:
                text = self.server.recv(1024)
                if not text: break
                self.gettext.configure(state='normal')
                self.gettext.insert(END,'%s\n'%text)
                self.gettext.configure(state='disabled')
            except:
                break

class App(Thread):
    server = socket(AF_INET,SOCK_STREAM)
    server.bind(('', 285))
    server.listen(5)
    client,addr = server.accept()
    
    def __init__(self, master):
        Thread.__init__(self)
        
        frame = Frame(master)
        frame.grid(row=2,column=3)
        
        self.quitbutton = Button(frame, text="QUIT", fg='red',
command=frame.quit)
        self.quitbutton.grid(row=1,column=2)
        
        self.button = Button(frame, text="SEND", command=self.Send)
        self.button.grid(row=1,column=1)
        
        self.sendtext = Entry(frame,width=60)
        self.sendtext.grid(row=1,column=0)
        
        self.gettext = Text(frame,height=10,width=80,wrap=WORD)
        self.gettext.grid(row=0,columnspan=3)
        self.gettext.insert(END,'Welcome to my Instant Messaging Program\n')
        self.gettext.configure(state='disabled')
        
    def Send(self):
        self.gettext.configure(state='normal')
        text = self.sendtext.get()
        self.gettext.insert(END,'%s \n'%text)
        self.sendtext.delete(0,END)
        self.server.send(text)
        self.gettext.configure(state='disabled')

    def run(self):
        Receive(self.server, self.gettext).start()

       
root = Tk()
root.title('Server')
app = App(root).start()
root.mainloop()
------------------------------------------------------------------------------------------

Whenever I run them both and I try to send from either one, it doesn't
work.  If I try to send from the server it tells me that there the
socket is not connected.  When I run the client with this server
program it works though...

------------------------------------------------------------------------------------------
from socket import *
from time import sleep

def Server():
    ss = socket(AF_INET,SOCK_STREAM);
    ss.bind(("",285));
    ss.listen(5);
    n = 0;
    while n != 1:
        client,addr = ss.accept();
        n += 1;
        if n == 1:
            print "Got a connection from ", addr
            client.send("Welcome to JRAM Instant Messaging");
            while 1:
                text = raw_input('>  ')
                client.send(text)

def Server()
------------------------------------------------------------------------------------------

I have no idea what I am doing wrong, any help would be appreciated.  Thankyou.


More information about the Tutor mailing list