Incorrect number of bytes returned by getsockopt(socket.SOL_SOCKET,socket.TCP_INFO, buflen)

Discussion in 'Python' started by Ashwin Rao, Dec 3, 2009.

  1. Ashwin Rao

    Ashwin Rao Guest

    Hi,

    I need to retrieve the tcp_info from a socket using getsockopt. The
    details of structure tcp_info are available at [http://src.gnu-
    darwin.org/src/sys/netinet/tcp.h.html]. The getsockopt method for a
    socket whose documentation is available at [http://docs.python.org/
    library/socket.html#socket.socket.getsockopt] specifies that - "if
    buflen is present, it specifies the maximum length of the buffer used
    to receive the option in, and this buffer is returned as a string". To
    confirm my calculation, I computed the size of the tcp_info using the
    following program, which returned 104.
    ---
    #include <stdio.h>
    #include <netinet/tcp.h>

    int main()
    {
    printf("%d", sizeof(struct tcp_info));
    }
    ---
    I then added the following lines to my program

    tcp_info = sock.getsockopt(socket.SOL_SOCKET, socket.TCP_INFO, 104)
    print "Return len"+str(len(tcp_info))
    for cnt in range(len(tcp_info)):
    print tcp_info[cnt]


    The output for the length is 4, which is less than 104. I would like
    to know the technique to obtain the tcp_info structure using the
    getsocktopt method. I am currently using python 2.5.2.

    Regards,
    Ashwin
     
    Ashwin Rao, Dec 3, 2009
    #1
    1. Advertisements

  2. Ashwin Rao

    Roy Smith Guest

    Re: Incorrect number of bytes returned by getsockopt(socket.SOL_SOCKET, socket.TCP_INFO, buflen)

    In article
    <>,
    Ashwin Rao <> wrote:

    > I computed the size of the tcp_info using the
    > following program, which returned 104.
    > ---
    > #include <stdio.h>
    > #include <netinet/tcp.h>
    >
    > int main()
    > {
    > printf("%d", sizeof(struct tcp_info));
    > }


    In general, the right way to do stuff like this in Python is to use the
    struct module. In particular, struct.calcsize() will tell you how long the
    string has to be. Try that and see if you come up with the same value.
     
    Roy Smith, Dec 3, 2009
    #2
    1. Advertisements

  3. Re: Incorrect number of bytes returned bygetsockopt(socket.SOL_SOCKET, socket.TCP_INFO, buflen)

    On Thu, 3 Dec 2009 10:29:13 -0800 (PST),
    Ashwin Rao <> wrote:

    > tcp_info = sock.getsockopt(socket.SOL_SOCKET, socket.TCP_INFO, 104)


    In C, to get tcp_info, SOL_SOCKET should be IPPROTO_TCP. I'm assuming
    it's the same in Python, although I've never tried it.

    Martien
    --
    |
    Martien Verbruggen | Never hire a poor lawyer. Never buy
    | from a rich salesperson.
    |
     
    Martien Verbruggen, Dec 3, 2009
    #3
    1. Advertisements

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. Jason Collins
    Replies:
    3
    Views:
    6,350
    Jason Collins
    Feb 18, 2004
  2. Richard Taylor
    Replies:
    4
    Views:
    2,388
    =?UTF-8?B?0LTQsNC80ZjQsNC9INCzLg==?=
    Apr 21, 2004
  3. Fabian Deutsch

    getsockopt

    Fabian Deutsch, Dec 16, 2005, in forum: Python
    Replies:
    0
    Views:
    642
    Fabian Deutsch
    Dec 16, 2005
  4. William Connery
    Replies:
    0
    Views:
    436
    William Connery
    Dec 5, 2006
  5. mrby

    4-bytes or 8-bytes alignment?

    mrby, Nov 2, 2004, in forum: C Programming
    Replies:
    8
    Views:
    741
    Mark McIntyre
    Nov 2, 2004
  6. Replies:
    5
    Views:
    843
    Flash Gordon
    Apr 9, 2006
  7. chris
    Replies:
    0
    Views:
    948
    chris
    May 21, 2010
  8. Ishan Bhalla

    Incorrect data types returned

    Ishan Bhalla, Mar 3, 2008, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    172
    Ishan Bhalla
    Mar 3, 2008
Loading...