On 1/26/2012 4:22 PM, Navneet wrote:
Hi,

I am trying to create a chat program.(Programs are attached)
Please find the code below, where I am having the problem.

def run( self ):
    while 1:
            print "Hello There !!!!"
        print self.descriptors
        # Await an event on a readable socket descriptor
(sread, swrite, sexc) = select.select( self.descriptors, [], [] )
        print sexc
            # Iterate through the tagged read descriptors
        print sread
            print "Hello There 1 !!!!"
        for sock in sread:


For this I am getting the output as below:
bash-3.1$ python Server.py
Enter the Port:...9009
ChatServer started on port 9009
Hello There !!!!
[<socket._socketobject object at 0x0000000002487660>]


But it is not printing the value of sread or "Hello There 1 !!!!!"





Hello All,

One more thing I want to add here is, I am trying to create the GUI based chat server.(Attached the programs.)
The concept is something like this:
I will start a server on a particular port and different Clients can interact with each other using that (more like a chat room ) Now I want to add the GUI part in a different module and want to call that module from client program.
But while executing the client1.py I am getting below error :



bash-3.1$ python Client1.py
Enter the server address:...9009
Traceback (most recent call last):
  File "Client1.py", line 53, in <module>
    c = ClientChat(serverport)
  File "Client1.py", line 24, in __init__
    gui.callGui()
  File "a:\FedEx\Exp\ClientGui.py", line 37, in callGui
sendbutton =Button(f2, width = 5, height = 2, text = "Send", command = C.ClientChat.senddata()) TypeError: unbound method senddata() must be called with ClientChat instance as first argument (got nothing instead)












from Tkinter import *
import tkMessageBox
import socket
import logging
import sys, time
import pickle
import thread
import ClientGui as gui



class ClientChat():
    def __init__(self,serverport):
        self.counter = 0

        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        self.host = 'localhost' # server address
        self.port = serverport # server port 

        # connect to server
        self.s.connect((self.host, self.port))

        gui.callGui()
        

    def senddata(self):
        self.k = gui.callGui.sendmsg.get(1.0, END)
        gui.callGui.sendmsg.delete(1.0, END)
        self.s.send(self.k)
            
        if self.counter == 0:
            thread.start_new_thread(self.recvdata,())
##        if self.k == '':
##            thread.exit()
##            self.s.close()
##            self.chatclient.destroy()

    def exitchat():
        self.s.close()
        self.chatclient.destroy()
        
    def recvdata(self):
        while 1:
            self.v = self.s.recv(1024) 
            gui.callGui.recvmsg.insert(END,self.v)
            
                

if __name__ == '__main__':

    serverport = int(raw_input("Enter the server address:..."))
    c = ClientChat(serverport)
            

from Tkinter import *
import tkMessageBox
import socket
import logging
import sys, time
import pickle
import thread
import Client1 as C


def callGui():
    chatclient = Tk()

    chatclient["padx"] = 200
    chatclient["pady"] = 200

    chatclient.title("Client")

    f1 = Frame(chatclient,width = 100, height = 10)
    f1.pack(side = TOP)

    f2 = Frame(chatclient,width = 100, height = 10)
    f2.pack(side = BOTTOM)


    recvmsg = Text(f1, width = 100, height = 10)
    sb = Scrollbar(f1)
    sb.pack(side = RIGHT, fill = Y)

    sb.config(command = recvmsg.yview)
    recvmsg.config(yscrollcommand=sb.set)
    recvmsg.pack(padx = 10, pady =10)

    sendmsg = Text(f2, width = 100, height = 5)
    sendmsg.pack(padx = 10, pady =20)

    sendbutton =Button(f2, width = 5, height = 2, text = "Send", command = 
C.ClientChat.senddata())
    sendbutton.pack(side = LEFT, padx = 10, pady = 10)

    sendbutton =Button(f2, width = 5, height = 2, text = "Exit", command = 
C.ClientChat.exitchat())
    sendbutton.pack(side = RIGHT, padx = 10, pady = 10)

    chatclient.mainloop()
import socket
import select


class ChatServer:
    def __init__( self, port ):
        self.port = port;
        self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
        self.srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
        self.srvsock.bind( ("localhost", port) )
        self.srvsock.listen( 5 )
        self.descriptors = [self.srvsock]
        print 'ChatServer started on port %s' % port
    def run( self ):
        while 1:
        # Await an event on a readable socket descriptor
            sread, swrite, sexc = select.select( self.descriptors, [], [],5 )
            # Iterate through the tagged read descriptors
            for sock in sread:
                # Received a connect to the server (listening) socket
                if sock == self.srvsock:
                    self.accept_new_connection()
                else:
                    # Received something on a client socket
                    str = sock.recv(100)
                    # Check to see if the peer socket closed
                    if str == '':
                        host,port = sock.getpeername()
                        str = 'Client left %s:%s\r\n' % (host, port)
                        self.broadcast_string( str, sock )
                        sock.close()
                        self.descriptors.remove(sock)
                    else:
                        host,port = sock.getpeername()
                        newstr = '[%s:%s] %s' % (host, port, str)
                        self.broadcast_string( newstr, self.srvsock )    

    def accept_new_connection( self ):
        newsock, (remhost, remport) = self.srvsock.accept()
        self.descriptors.append( newsock )
        newsock.send("You're connected to the Python chatserver\r\n")
        str = 'Client joined %s:%s\r\n' % (remhost, remport)
        self.broadcast_string( str, newsock )

    def broadcast_string( self, str, omit_sock ):
        for sock in self.descriptors:
            if sock != self.srvsock and sock != omit_sock:
                sock.send(str)
                print str,

if __name__ == '__main__':

    portno = int(raw_input("Enter the Port:..."))
    s = ChatServer(portno)
    s.run()

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to