I need some help to make this simple program run [please, see the source code 
below] as it's probably some basic misunderstanding. I'm trying to lay out the 
principles of an elementary chat server with a fixed number of clients, and 
I've been testing these two programs on my local network -- WiFi, no fire wall.

The client would be an iMac G5 running X.4.6 at 192.168.1.20 while the server 
would be a PowerBook running X.3.9 at 192.168.1.129. That said,  swapping the 
machines doesn't solve anything.

The question is: why can't the client use the socket.connect function here?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The server runs with no glitch and cycles in its while-loop. Meanwhile, I 
launch the client, but it quits on a sys.exit instruction, and the 
socket.connect function doesn't return anything printable -- maybe because it 
executes in a try-block?...

Here's the error I get:

        $ python client.py jf
        ~~~ Name : jf - Host : 192.168.1.129 - Port : 5007
        Connection failed - error : None

Any idea?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# -- source code

# -- server

from socket import *
import threading
import sys

def server (Host, Port):
        global clients
        S = socket (AF_INET, SOCK_STREAM)
        try:
                S.bind ((Host, Port))
        except:
                print "socket connection failed"
                sys.exit ()
        print "Host =", Host, "\nPort =", Port
        print "Accepting new clients..."
        S.listen (MAX)
        while True:
                C, A = S.accept ()
                P = C.recv (10) # pseudonym
                clients.append (C)
                print "New client connected:", P, A
                threading.Thread (target = dialog, args = (P, C)).start ()

def dialog (pseudo, socket):
        help (socket)
        while True:
                CliMsg = socket.recv (BuffSize)
                if (CliMsg == '/quit'):
                        SrvMsg = pseudo + ' = disconnected'
                        break
                if (CliMsg == '/help'):
                        help (socket)
                        continue
                SrvMsg = pseudo + ' = ' + CliMsg
                print SrvMsg
                for c in  range (len (clients)):
                        clients[c].send (SrvMsg)
        print SrvMsg
        for c in range (len (clients)):
                clients[c].send (SrvMsg)
        for c in range (len (clients)):
                if (clients[c] == socket):
                        del (clients[c])
        socket.close ()

def help (socket):
        help_text = "\n"
        help_text += "type /help  to display this again\n"
        help_text += "type /quit  to disconnect\n"
        help_text += "press [quit] to quit\n"
        socket.send (help_text)

clients = []
HOST = 'localhost'
PORT = 5007 # can't bind this for 2 successive runs
BuffSize = 1024
MAX = 5

if len (sys.argv) > 1:
        if len (sys.argv) == 3:
                HOST = sys.argv[1]
                PORT = int (sys.argv[2])
        else:
                print "usage:", sys.argv[0], "<host> <port>"
                sys.exit ()

threading.Thread (target = server, args = (HOST, PORT)).start ()

# -- client
import threading
import Tkinter
import sys
import tkSimpleDialog
import socket

def send_handler (event):
        global NT
        try:
                CliMsg = NT.get ()
                NT.delete (0, END)
                socket.send (CliMsg)
        except:
                socket.close ()
                sys.exit

def quit ():
        try: socket.send ('/quit')
        except: pass
        root.destroy ()
        root.quit ()

def setup (root, title):
    global TF, NT
    root.title (title)
    TF = Text (root, width = 60, height = 20)
    TF.grid (row = 0, column = 0, columnspan = 3)
    SB = Scrollbar (root, orient = VERTICAL)
    SB.config (command = TF.yview)
    TF.config (yscrollcommand = SB.set)
    SB.grid (row = 0, column = 3, sticky = N + S)
    NT = Entry (root)
    NT.grid (row = 1, column = 0, columnspan = 2, sticky = W + E)
    NT.bind ("<Return>", send_handler)
    NT.focus_set ()
    QB = Button (root, text = 'quit', command = quit)
    QB.grid (row = 1, column = 2)

def connect (Host, Port, P):
        Sk = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
        try:
                Sk.socket.connect ((Host, Port))
        except :
                print "Connection failed..."
                sys.exit ()
        print "Connection successful @ ", Host , Port
        Sk.socket.send (P)
        return Sk

def listening (sock):
        global TF
        while True:
                try:
                        SrvMsg = sock.recv (BuffSize)
                        if (SrvMsg):
                                TF.insert (END, SrvMsg + '\n')
                except:
                        TF.insert (END, "not connected...\n")
                        print "Not connected..."
                        break
        sock.close ()

# Application specific ~~~~~~~~~~~~~~~~~~~~~~~~


HOST = 'localhost'
HOST = '192.168.1.129' # my network only
PORT = 5007
BuffSize = 1024
P = None # pseudonym

if (len (sys.argv) > 1):
        if (len (sys.argv) == 2):
                P = sys.argv[1]
        elif (len (sys.argv) == 3):
                HOST = sys.argv[1]
                PORT = int (sys.argv[2])
        elif (len (sys.argv) == 4):
                HOST = sys.argv[1]
                PORT = int (sys.argv[2])
                P = sys.argv[3]
        else :
                print "usage:", sys.argv[0], " [HOST PORT] [userName]"
                sys.exit ()
while (not P):
        P = tkSimpleDialog.askstring ('P', 'Pseudonym?...')

# remove this one
print "~~~ Client name:", P, "- Host:", HOST, "- Port:", PORT

socket = connect (HOST, PORT, P)
root = Tk ()
setup (root, "PyChAt @ " + P)
threading.Thread (target = listening, args = [socket]).start ()
root.mainloop ()

-- 
Jym Feat -- Paris FR 75018
_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig

Reply via email to