Problem with socket

Discussion in 'Python' started by =?ISO-8859-1?Q?Thomas_Herv=E9?=, Apr 28, 2004.

  1. My problem is not really python specific but as I do my implementation
    in python I hope someone here can help me.

    I have two programs that talk through a socket. Here is the code :

    <server>
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    sock.settimeout(5.0)

    sock.bind(("", port)
    # We only handle one connection
    sock.listen(1)

    while 1:
    newsock, address = sock.accept()
    handle(newsock, address)

    def handle(sock, address) :
    print "Connection from", address
    dataReceived = newsock.recv(1024)
    while 1:
    try:
    newsock.send(data)
    except socket.timeout, err:
    print "Connection timeout %s" % err
    break
    except socket.error, err:
    print "Connection broken %s" % err
    break

    </server>

    <client>
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(5.0)
    sock.connect((host, port))
    sock.send("Gimme a piece of information, please\r\n")

    while 1:
    r, w, e = select.select(, [], [], 1.0)
    if r != []:
    handle_connection(s)


    def handle_connection(sock):
    try:
    response_data = sock.recv(1024)
    except socket.timeout:
    print "Socket timeout"
    return
    manage(response_data)

    </client>

    Ok I hope it's clear. My problem is that "select" only tests if there's
    data on the socket, and not the state of the socket. I want to be able
    to know if socket is "alive" or something like that. But I don't want
    to make "send" in the client (stay passive). Then, if I know that my
    socket is closed or my link down, I can try to reconnect periodically.

    I would rewrite the while in the client like that :
    <client>
    while 1:
    if not test_connect(s) :
    reconnect(s)
    # else continue normally
    r, w, e = select.select(, [], [], 1.0)
    if r != []:
    handle_connection(s)
    </client

    My env: python2.3, linux/win32.

    Thanks in advance,

    --
    Thomas Herve <sorry for my english>
    =?ISO-8859-1?Q?Thomas_Herv=E9?=, Apr 28, 2004
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Thomas_Herv=E9?=

    Donn Cave Guest

    In article <408f7917$0$27016$>,
    Thomas Herve <> wrote:
    ....
    > Ok I hope it's clear. My problem is that "select" only tests if there's
    > data on the socket, and not the state of the socket. I want to be able
    > to know if socket is "alive" or something like that. But I don't want
    > to make "send" in the client (stay passive). Then, if I know that my
    > socket is closed or my link down, I can try to reconnect periodically.


    Try a SO_KEEPALIVE option on the socket (setsockopt). That
    should (I would sort of expect anyway) raise an exception when
    the keepalive negotiation fails. The reaction won't be immediate,
    it may take hours to notice a dead connection.

    Donn Cave,
    Donn Cave, Apr 28, 2004
    #2
    1. Advertising

  3. On 2004-04-28, Thomas Hervé <> wrote:

    > Ok I hope it's clear. My problem is that "select" only tests
    > if there's data on the socket, and not the state of the
    > socket.


    That's not true. If the socket is closed, it will return from
    select as readable.

    > I want to be able to know if socket is "alive" or something
    > like that. But I don't want to make "send" in the client (stay
    > passive). Then, if I know that my socket is closed or my link
    > down, I can try to reconnect periodically.


    You'll know if the socket is closed because select will mark it
    as readable, and you'll get 0 bytes when you read it.

    If you want to know if the network or the other host has "gone
    away", set the SO_KEEPALIVE option on the socket. That will
    generate an error if the link is down. IIRC, it takes 75
    minutes (or is it 150?) to time out after the other end goes
    away.

    --
    Grant Edwards grante Yow! I wonder if there's
    at anything GOOD on tonight?
    visi.com
    Grant Edwards, Apr 28, 2004
    #3
  4. Grant Edwards wrote:
    > On 2004-04-28, Thomas Hervé <> wrote:
    >
    >
    >>Ok I hope it's clear. My problem is that "select" only tests
    >>if there's data on the socket, and not the state of the
    >>socket.

    >
    > That's not true. If the socket is closed, it will return from
    > select as readable.


    Exact.

    >>I want to be able to know if socket is "alive" or something
    >>like that. But I don't want to make "send" in the client (stay
    >>passive). Then, if I know that my socket is closed or my link
    >>down, I can try to reconnect periodically.

    >
    > You'll know if the socket is closed because select will mark it
    > as readable, and you'll get 0 bytes when you read it.


    It seems that it's not true : when I read it I have a "Connection reset
    by peer" error.

    > If you want to know if the network or the other host has "gone
    > away", set the SO_KEEPALIVE option on the socket. That will
    > generate an error if the link is down. IIRC, it takes 75
    > minutes (or is it 150?) to time out after the other end goes
    > away.


    Yes, I've seen this option, but the delay is a bit too large (2 hours ?
    depending on OS it seems) and not configurable. So I made it by send
    some hearbeat regulary, even if I didn't want to I haven't found another
    way. And it's better for server side, from which I can make some recv
    that make the break found earlier.

    --
    Thomas Herve
    =?ISO-8859-1?Q?Thomas_Herv=E9?=, Apr 29, 2004
    #4
    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. Laszlo Nagy
    Replies:
    1
    Views:
    4,822
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    966
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    545
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    662
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    715
Loading...

Share This Page