如何连接到C#中的本地套接字?

我正在尝试修改我发现的用于连接到Dropbox守护进程的Python代码:

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 

这是我到目前为止:

 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's 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; } 

这编译好,但当我尝试运行它,我得到System.Net.Sockets.SocketException: No such host is known 。 我认为这是因为cmdSocketifaceSocket是path,而不是IP地址。 Python似乎自动处理这个,我怎么在C#中做到这一点? 这是我第一次进入套接字编程,所以请指出任何明显的错误。

您需要使用Mono.Unix.UnixEndPoint的Mono.Unix.UnixEndPoint而不是IPEndPoint。 其他一切都是一样的。 查看XSP如何在此使用它的示例。