0
votes

I have a c++ program which creates a local socket bound to "/tmp/.mysocket" and waits to receive data from that socket. The way it is set up, raw binary data would be sent to the socket and loaded into the following C++ structure:

struct StateVariable
{
    char Name[64];
    int E[8];
};

The C++ program listens for input using recvfrom:

int nBytes = recvfrom(SD,&DataReceived,sizeof(StateVariable)/sizeof(char),0,(sockaddr*)&SentFrom,&size);

The server is just using the standard AF_UNIX and SOCK_DGRAM to create the socket.

My questions is relating to python: How do I send data to the local socket bound to /tmp/.mysocket using python? I am not using AF_INET or opening a specific port for this.

I can use python's socket library to describe the socket, but I can't find any resource that discusses binding a socket to a file in python and sending data to that socket. The documentation for the socket library only discusses using AF_INET and SOCK_DGRAM for local sockets bound to a port number at 127.0.0.1, but I'm not doing that.

How do I get python to send data to a socket bound to a file? Is there an example python program that does just that (maybe a client/server pair that demonstrates this functionality)? As long as I can get python to send data to a local file socket, I can figure out the rest.

2
Remote procedure call mechanism was programmed 10000 times. Google for already existing method.Kirill Kobelev

2 Answers

2
votes

The Python socket library is a fairly thin wrapper around the socket interface you're familiar with from C++. See socket documentation.

It's going to look something like this:

import socket
import struct 

s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
s.connect('/tmp/...')
s.send(struct.pack('64s8i', ...))
0
votes

You just change AF_INET to AF_UNIX and then .connect(address) and/or .bind(address) methods accept path to a file (instead of (host, port) pair). Other then that everything else works pretty much the same.