Re: Newbie: Keep TCP socket open

Discussion in 'Python' started by Irmen de Jong, May 19, 2008.

  1. Alan Wright wrote:

    > while (num1<=10) :
    >
    > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    > s.settimeout(10.0)
    > s.connect(("10.1.1.69", 50008)) # SMTP
    > print s.recv(1024) + '\n',
    > num1=num1+1
    > #s.close()
    >
    >
    > sys.exit(1)


    I think the following is happening:
    Reusing the 's' object for every new socket will make Python to garbage
    collect the previous ones. Garbage collecting a socket will likely close() it.
    Also after creating all sockets your program exits. I guess either Python or the
    operating system itself will go close all the sockets.


    Try putting every new socket you make into a big list instead, so that Python can't
    garbage collect it. And put your program to sleep at the end.

    import time
    allsockets=[]

    while (...):
    s=socket.socket(...
    allsockets.append(s)
    s.settimeout(...
    ...

    time.sleep(99999)



    --irmen
    Irmen de Jong, May 19, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pwilkins
    Replies:
    7
    Views:
    437
    Grant Edwards
    Jun 25, 2005
  2. Tiger
    Replies:
    5
    Views:
    952
    Dave Thompson
    May 1, 2006
  3. TCP Socket login handling. Newbie

    , Jul 15, 2006, in forum: C Programming
    Replies:
    0
    Views:
    366
  4. Replies:
    5
    Views:
    626
    Roy Smith
    May 21, 2008
  5. hisan
    Replies:
    1
    Views:
    1,296
    Dan Stromberg
    Jun 25, 2012
Loading...

Share This Page