Python and UDP sockets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zunbeltz Izaola

    Python and UDP sockets


    Hi!

    I want to port a small socket aplication (in C) to Python.
    The server is writen in C, and it expected to recibe and C struct
    like this


    struct tag_instruccion
    {
    unsigned int Handle __attribute__ ((aligned(1)));
    int Codigo __attribute__ ((aligned(1)));
    double Param[10] __attribute__ ((aligned(1)));
    char Comando[_MAX_PATH] __attribute__ ((aligned(1))) ;
    char Instruc[_MAX_PATH] __attribute__ ((aligned(1))) ;
    };

    I know that the functions for sockets are essentialy those that are in
    C (BSD sockets), but i don't know how to send this kind of struct
    directly.

    One of the function to send a socket is this

    EnviaMensaje(SO CKET iSocket,int port,char *host, instruccion *Instruccion)
    {
    struct sockaddr_in siSockAddr;
    int iRc;
    CodigoMensaje resultado;

    if (iSocket != INVALID_SOCKET)
    {
    memset((char *)&siSockAddr,0 ,sizeof(siSockA ddr));
    siSockAddr.sin_ family = AF_INET;
    siSockAddr.sin_ port = htons(port);
    siSockAddr.sin_ addr.s_addr = inet_addr(host) ;
    iRc = sendto(iSocket, (char const FAR *)Instruccion,s izeof(instrucci on),0,(struct sockaddr FAR *)(&siSockAddr) ,sizeof(siSockA ddr));
    }
    return iRc;
    }

    Can someone help me?
    Thanks in advance

    Zunbeltz Izaola

    --
    Remove XXX from email: zunbeltz@wm.lc. ehu.esXXX
  • Alex Martelli

    #2
    Re: Python and UDP sockets

    Zunbeltz Izaola wrote:
    ...[color=blue]
    > C (BSD sockets), but i don't know how to send this kind of struct
    > directly.[/color]

    You transform the tuple of field values into a string with struct.pack
    and send the string. And vv in receiving. See:




    Alex

    Comment

    • Werner Schiendl

      #3
      Re: Python and UDP sockets

      Hi,

      look at the struct module from the python standard library.

      in particular, struct.pack allows to format your data as required.

      hth
      Werner


      Zunbeltz Izaola wrote:
      [color=blue]
      > Hi!
      >
      > I want to port a small socket aplication (in C) to Python.
      > The server is writen in C, and it expected to recibe and C struct
      > like this
      >
      >
      > struct tag_instruccion
      > {
      > unsigned int Handle __attribute__ ((aligned(1)));
      > int Codigo __attribute__ ((aligned(1)));
      > double Param[10] __attribute__ ((aligned(1)));
      > char Comando[_MAX_PATH] __attribute__ ((aligned(1))) ;
      > char Instruc[_MAX_PATH] __attribute__ ((aligned(1))) ;
      > };
      >
      > I know that the functions for sockets are essentialy those that are in
      > C (BSD sockets), but i don't know how to send this kind of struct
      > directly.
      >
      > One of the function to send a socket is this
      >
      > EnviaMensaje(SO CKET iSocket,int port,char *host, instruccion *Instruccion)
      > {
      > struct sockaddr_in siSockAddr;
      > int iRc;
      > CodigoMensaje resultado;
      >
      > if (iSocket != INVALID_SOCKET)
      > {
      > memset((char *)&siSockAddr,0 ,sizeof(siSockA ddr));
      > siSockAddr.sin_ family = AF_INET;
      > siSockAddr.sin_ port = htons(port);
      > siSockAddr.sin_ addr.s_addr = inet_addr(host) ;
      > iRc = sendto(iSocket, (char const FAR *)Instruccion,s izeof(instrucci on),0,(struct sockaddr FAR *)(&siSockAddr) ,sizeof(siSockA ddr));
      > }
      > return iRc;
      > }
      >
      > Can someone help me?
      > Thanks in advance
      >
      > Zunbeltz Izaola
      >[/color]

      Comment

      • Zunbeltz Izaola

        #4
        Re: Python and UDP sockets

        Werner Schiendl <n17999950.temp .werner@neverbo x.com> writes:

        Hi

        Thanks for the point. I'm using struct and it is working well.
        Thanks again

        Zunbeltz
        [color=blue]
        > Hi,
        >
        > look at the struct module from the python standard library.
        >
        > in particular, struct.pack allows to format your data as required.
        >
        > hth
        > Werner
        >
        >
        > Zunbeltz Izaola wrote:
        >[color=green]
        > > Hi!
        > > I want to port a small socket aplication (in C) to Python.
        > > The server is writen in C, and it expected to recibe and C struct
        > > like this struct tag_instruccion
        > > {
        > > unsigned int Handle __attribute__ ((aligned(1)));
        > > int Codigo __attribute__ ((aligned(1)));
        > > double Param[10] __attribute__ ((aligned(1)));
        > > char Comando[_MAX_PATH] __attribute__ ((aligned(1))) ;
        > > char Instruc[_MAX_PATH] __attribute__ ((aligned(1))) ;
        > > };
        > > I know that the functions for sockets are essentialy those that are
        > > in
        > > C (BSD sockets), but i don't know how to send this kind of struct
        > > directly.
        > > One of the function to send a socket is this
        > > EnviaMensaje(SO CKET iSocket,int port,char *host, instruccion
        > > *Instruccion)
        > > {
        > > struct sockaddr_in siSockAddr;
        > > int iRc;
        > > CodigoMensaje resultado;
        > > if (iSocket != INVALID_SOCKET)
        > > {
        > > memset((char *)&siSockAddr,0 ,sizeof(siSockA ddr));
        > > siSockAddr.sin_ family = AF_INET;
        > > siSockAddr.sin_ port = htons(port);
        > > siSockAddr.sin_ addr.s_addr = inet_addr(host) ;
        > > iRc = sendto(iSocket, (char const FAR *)Instruccion,s izeof(instrucci on),0,(struct sockaddr FAR *)(&siSockAddr) ,sizeof(siSockA ddr));
        > > }
        > > return iRc;
        > > }
        > > Can someone help me?
        > > Thanks in advance
        > > Zunbeltz Izaola
        > >[/color]
        >[/color]

        --
        Remove XXX from email: zunbeltz@wm.lc. ehu.esXXX

        Comment

        Working...