Python <-> C via sockets

Eric Brunel eric.brunel at pragmadev.N0SP4M.com
Wed Sep 24 11:52:10 EDT 2003


Nick Keighley wrote:
> Eric Brunel <eric.brunel at pragmadev.N0SP4M.com> wrote in message news:<bkroq8$8ud$1 at news-reader1.wanadoo.fr>...
> 
>>Nick Keighley wrote:
> 
> 
>>>I'm probably missing something rather obvious, but is there a known 
>>>problem with getting a Python based socket program to communicate with
>>>a C based socket program? A simple echo server written in Python
>>>(the example from Python in a Nutshell actually) will happily talk
>>>to a Python based client. If a C based client is substitued the connection
>>>is refused (or so the C client reports). The C client will talk to
>>>a C server.
>>
>>Communications between C and Python via sockets definetly work: we do that 
>>every  day (no kidding ;-)
>>
>>Can you post an example of your code?
> 
> 
> well I assumed sockets actually worked I was guessing the slight
> differences in the interfaces caused me to set the two ends up
> slightly
> differently.
> 
> Python Echo Server
> ------------------
> import socket
> 
> DEFAULT_PROTOCOL = 0
> PORT = 8702
> 
> sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM,
> DEFAULT_PROTOCOL)
> sock.bind (('', PORT))
> sock.listen (5)
> 
> # wait for a connection
> try:
>     while True:
>         newSocket, address = sock.accept ()
>         print "connected from", address
>         while True:
>             receivedData = newSocket.recv (8192)
>             if not receivedData:
>                 break
>             print "received: ", receivedData
>             newSocket.sendall (receivedData)
>         newSocket.close()
>         print "disconnected from", address
> finally:
>     sock.close ()
> ---------------------------------------------------------------------
> C echo client (includes omitted)
> -------------------------------
> #define DEFAULT_PROTOCOL   0
> #define PORT 8702
> #define HOST "localhost"
> /* #define HOST "127.0.0.1"    */
> /* #define HOST "cmopc018"  */
> #define TYPE SOCK_STREAM
> 
> 
> int main (void)
> {  
>     struct hostent  *phe;
>     struct sockaddr_in sin;
>     int s;
> 
>     s = socket (PF_INET, TYPE, DEFAULT_PROTOCOL);
>     if (s < 0)
>         raise_report (LEVEL_FATAL, "tiny_echo", "can't create socket:
> %s", strerror (errno));
> 
> 
>     /*
>      * connect to the socket 
>      */
> 
>     memset (&sin, 0, sizeof sin);
>     sin.sin_family = AF_INET;
>     sin.sin_port = PORT;
>     if ((phe = gethostbyname (HOST)))
>     {
>         memcpy (&sin.sin_addr, phe->h_addr_list[0], phe->h_length);
>     	raise_report (LEVEL_INFO, "tiny_echo", "addr is %X",
> sin.sin_addr);
>      }
>     else
>         if ((sin.sin_addr.s_addr = inet_addr (HOST)) == INADDR_NONE)
>             raise_report (LEVEL_FATAL, "tiny_echo", "can't get \"%s\"
> host entry", HOST);
> 
>     if (connect (s, (struct sockaddr *)&sin, sizeof sin) < 0)
>         raise_report (LEVEL_FATAL, "tiny_echo", "can't connect to
> %s.%d: %s", HOST, sin.sin_port, strerror (errno));
> 
>     raise_report (LEVEL_INFO, "tiny_echo", "CONNECTED");
> 
>     return 0;
> }
> ------------------------------------------------------------------------------

After adding the missing htons and replacing the raise_report's by printf's, 
works like a charm here (Linux Mandrake 8.0 with Python 2.1).

What is exactly the error you get?
-- 
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com





More information about the Python-list mailing list