How to connect to local socket in c #?

I am trying to adapt this python code that I found to connect to the Dropbox daemon:

def connect(self, cmd_socket="~/.dropbox/command_socket", iface_socket="~/.dropbox/iface_socket"): "Connects to the Dropbox command_socket, returns True if it was successfull." self.iface_sck = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.sck = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: self.sck.connect(os.path.expanduser(cmd_socket)) # try to connect self.iface_sck.connect(os.path.expanduser(iface_socket)) except: self.connected = False return False else: # went smooth self.connected = True return True 

Here is what I still have:

 public bool Connect (int port) { return Connect ("~/.dropbox/command_socket", "~/.dropbox/iface_socket", port); } public bool Connect (string cmdSocket, string ifaceSocket, int port) { IfaceSocket = new Socket (AddressFamily.Unix, SocketType.Stream, ProtocolType.IP); CmdSocket = new Socket (AddressFamily.Unix, SocketType.Stream, ProtocolType.IP); try { // ExpandUser replaces a leading "/~" with the user home directory IPAddress [] CmdIPs = Dns.GetHostAddresses (ExpandUser (cmdSocket)); CmdSocket.Connect (CmdIPs [0], port); IPAddress [] IfaceIPs = Dns.GetHostAddresses (ExpandUser (ifaceSocket)); IfaceSocket.Connect (IfaceIPs [0], port); } catch (Exception e) { // Debug Console.WriteLine (e); Connected = false; return false; } Connected = true; return true; } 

This compiles fine, but when I try to run it, I get a System.Net.Sockets.SocketException: No such host is known . I assume this is because cmdSocket and ifaceSocket are paths, not IP addresses. Python seems to handle this automatically, how do I do this in C #? This is my first foray into socket programming, so please indicate any obvious errors.

+4
source share
1 answer

You need to use Mono.Unix.UnixEndPoint from Mono.Posix.dll instead of IPEndPoint. Everything else is the same. See an example of how XSP uses it here .

+4
source

All Articles