Python and UDP sockets

Werner Schiendl n17999950.temp.werner at neverbox.com
Tue Oct 21 06:34:46 EDT 2003


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:

> 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
> 





More information about the Python-list mailing list