[Tutor] Socket connection refused

Vianus le Fus deadviannou at caramail.com
Mon Feb 23 14:09:13 EST 2004

I'm a newbie to python so please don't be too hard with me :)
I'm encountering problems using socket connections in my programs. My goal is to create a small chat program 
so I begin with the beginning : two really small apps, one for the server and one for the client.
The server is here to listen to one port and to resend its own data to the client that's connected to the port.

I have found two ways to test this : I compile the server with py2exe and launch it, then I can run the client 
either running the module under IDLE or compiling it with py2exe.
And there's my problem : it works under IDLE (connection is set and the client receives its own data) but 
connection fails when using the exe client.... it says "error 10061 : Connection refused". I don't have any 
firewall nor other running programs that could block the socket, I really don't understand what's the difference 
using py2exe or not !!

Please does someone know why the first method works and not the other ?

import socket

HOST = ''
PORT = 50007

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break

import errno
import socket
from Tkinter import *
import tkMessageBox

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connected = 0
        self.data = ''

    def createWidgets(self):
        self.btn_quit = Button(self)
        self.btn_quit["text"] = "QUIT"
        self.btn_quit["fg"]   = "red"
        self.btn_quit["command"] =  self.end

        self.btn_quit.pack({"side": "left"})

        self.btn_connect = Button(self)
        self.btn_connect["text"] = "Connect",
        self.btn_connect["command"] = self.connect

        self.btn_connect.pack({"side": "right"})

    def connect(self) :
            self.s.connect(('', 50007))
            self.connected = 1
        except socket.error, msg:
            tkMessageBox.showinfo(title='Connexion error', message='Can\'t connect \
to the server' + '\n' + str(errno.errorcode[msg[0]]))

        if self.connected == 1 :
            self.s.send('Hello, world')
            self.data = self.s.recv(1024)
            print 'Received', str(self.data)
            tkMessageBox.showinfo(title='Data received !!', message=str(self.data))

    def end(self) :

app = Application()

Plus simple, plus fiable, plus rapide : découvrez le nouveau Caramail - http://www.caramail.lycos.fr

More information about the Tutor mailing list