[Tutor] Socket connection refused
Nick Lunt
nick at javacat.f2s.com
Mon Feb 23 14:14:13 EST 2004
Oops, made an error there, I get the TkDialog error message when I've
clicked on 'connect' 2 or more times.
A quick google on EISCONN showed me that it is because the socket has to
be closed again before another connection can be made to it.
See here http://www.wlug.org.nz/EISCONN .
I think you can change that behaviour with 'socketObject.listen(10)' for
example, but don't take my word for that cos I tried it and while I
could connect to it multiple times using telnet, only the first telnet
session echoed back what I typed in.
Hope that helps a bit
Nick.
On Mon, 23 Feb 2004 19:00:49 +0000 Nick Lunt <nick at javacat.f2s.com>
wrote:
> Hi there,
>
> I'm also new at python but I copied/pasted your source code and it ran
> fine after I'd sorted out the formatting, but I suspect that was
> because of my email client.
>
> Anyway it runs on linux ok, the only issue I found was that when
> connecting, then clicking on the OK button on the 'Hello World'
> message I get the following TkDialog up "Can't connect to the server
> 127.0.0.1 EISCONN"
>
> Sorry I can't be of anymore help.
>
> Cheers
> Nick.
>
>
> On Mon, 23 Feb 2004 19:09:13 GMT "Vianus le Fus "
> <deadviannou at caramail.com> wrote:
>
> > Hello,
> > 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?
> >
> > -----------------------------------------------------
> > Serveur.py
> > -----------------------------------------------------
> > import socket
> >
> > HOST = '127.0.0.1'
> > PORT = 50007
> >
> > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> > s.bind((HOST, PORT))
> > s.listen(1)
> > conn, addr = s.accept()
> > print 'Connected by', addr
> > while 1:
> > data = conn.recv(1024)
> > if not data: break
> > conn.send(data)
> > conn.close()
> >
> >
> >
> > -----------------------------------------------------
> > Client.py
> > -----------------------------------------------------
> > 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 = ''
> >
> > self.pack()
> > self.createWidgets()
> >
> >
> > 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) :
> > try:
> > self.s.connect(('127.0.0.1', 50007))
> > self.connected = 1
> >
> > except socket.error, msg:
> > tkMessageBox.showinfo(title='Connexion error',
> > message='Can\'t connect \
> > to the server 127.0.0.1' + '\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) :
> > self.s.close()
> > self.quit()
> >
> >
> > # MAIN
> > app = Application()
> > app.mainloop()
> > -----------------------------------------------------
> >
> > Plus simple, plus fiable, plus rapide : découvrez le nouveau
> > Caramail- http://www.caramail.lycos.fr
> >
> >
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list