Python and UDP sockets

Z

Zunbeltz Izaola

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(SOCKET iSocket,int port,char *host, instruccion *Instruccion)
{
struct sockaddr_in siSockAddr;
int iRc;
CodigoMensaje resultado;

if (iSocket != INVALID_SOCKET)
{
memset((char *)&siSockAddr,0,sizeof(siSockAddr));
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,sizeof(instruccion),0,(struct sockaddr FAR *)(&siSockAddr),sizeof(siSockAddr));
}
return iRc;
}

Can someone help me?
Thanks in advance

Zunbeltz Izaola
 
W

Werner Schiendl

Hi,

look at the struct module from the python standard library.

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

hth
Werner
 
Z

Zunbeltz Izaola

Hi

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

Zunbeltz
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 said:
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(SOCKET iSocket,int port,char *host, instruccion
*Instruccion)
{
struct sockaddr_in siSockAddr;
int iRc;
CodigoMensaje resultado;
if (iSocket != INVALID_SOCKET)
{
memset((char *)&siSockAddr,0,sizeof(siSockAddr));
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,sizeof(instruccion),0,(struct sockaddr FAR *)(&siSockAddr),sizeof(siSockAddr));
}
return iRc;
}
Can someone help me?
Thanks in advance
Zunbeltz Izaola
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top