public static void Main(string[] args) { bool isServer; int port; string hostname = "127.0.0.1"; //IPAddress ipaddr = IPAddress.Parse ("127.0.0.1"); if (args.Length == 2 && args[0] == "server") { isServer = true; port = Int32.Parse(args[1]); } else if (args.Length == 3 && args[0] == "client") { isServer = false; hostname = args[1]; port = Int32.Parse(args[2]); } else { Console.Error.WriteLine("Usage: test-server-tcp [server PORT|client HOSTNAME PORT]"); return; } Connection conn; ObjectPath myOpath = new ObjectPath("/org/ndesk/test"); string myNameReq = "org.ndesk.test"; if (!isServer) { SocketTransport transport = new SocketTransport(); transport.Open(hostname, port); conn = new Connection(transport); DemoObject demo = conn.GetObject <DemoObject> (myNameReq, myOpath); demo.GiveNoReply(); //float ret = demo.Hello ("hi from test client", 21); float ret = 200; while (ret > 5) { ret = demo.Hello("hi from test client", (int)ret); Console.WriteLine("Returned float: " + ret); System.Threading.Thread.Sleep(1000); } } else { TcpListener server = new TcpListener(IPAddress.Any, port); server.Start(); while (true) { Console.WriteLine("Waiting for client on " + port); TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Client accepted"); //TODO: use the right abstraction here, probably using the Server class SocketTransport transport = new SocketTransport(); transport.Stream = client.GetStream(); conn = new Connection(transport); //conn.SocketHandle = (long)clientSocket.Handle; //ConnectionHandler.Handle (conn); //in reality a thread per connection is of course too expensive ConnectionHandler hnd = new ConnectionHandler(conn); new Thread(new ThreadStart(hnd.Handle)).Start(); Console.WriteLine(); } } }
//TODO: complete this test daemon/server example, and a client //TODO: maybe generalise it and integrate it into the core public static void Main(string[] args) { bool isServer; if (args.Length == 1 && args[0] == "server") { isServer = true; } else if (args.Length == 1 && args[0] == "client") { isServer = false; } else { Console.Error.WriteLine("Usage: test-server [server|client]"); return; } //string addr = "unix:abstract=/tmp/dbus-ABCDEFGHIJ"; string addr = "unix:path=/tmp/dbus-ABCDEFGHIJ"; Connection conn; ObjectPath myOpath = new ObjectPath("/org/ndesk/test"); string myNameReq = "org.ndesk.test"; if (!isServer) { conn = new Connection(Transport.Create(AddressEntry.Parse(addr))); DemoObject demo = conn.GetObject <DemoObject> (myNameReq, myOpath); demo.GiveNoReply(); //float ret = demo.Hello ("hi from test client", 21); float ret = 200; while (ret > 5) { ret = demo.Hello("hi from test client", (int)ret); Console.WriteLine("Returned float: " + ret); System.Threading.Thread.Sleep(1000); } } else { string path; bool abstr; AddressEntry entry = AddressEntry.Parse(addr); path = entry.Properties["path"]; UnixSocket server = new UnixSocket(); byte[] p = Encoding.Default.GetBytes(path); byte[] sa = new byte[2 + p.Length + 1]; //we use BitConverter to stay endian-safe byte[] afData = BitConverter.GetBytes(UnixSocket.AF_UNIX); sa[0] = afData[0]; sa[1] = afData[1]; for (int i = 0; i != p.Length; i++) { sa[2 + i] = p[i]; } sa[2 + p.Length] = 0; //null suffix for domain socket addresses, see unix(7) server.Bind(sa); //server.Listen (1); server.Listen(5); while (true) { Console.WriteLine("Waiting for client on " + addr); UnixSocket client = server.Accept(); Console.WriteLine("Client accepted"); //client.Blocking = true; //PeerCred pc = new PeerCred (client); //Console.WriteLine ("PeerCred: pid={0}, uid={1}, gid={2}", pc.ProcessID, pc.UserID, pc.GroupID); UnixNativeTransport transport = new UnixNativeTransport(); transport.Stream = new UnixStream(client.Handle); conn = new Connection(transport); //ConnectionHandler.Handle (conn); //in reality a thread per connection is of course too expensive ConnectionHandler hnd = new ConnectionHandler(conn); new Thread(new ThreadStart(hnd.Handle)).Start(); Console.WriteLine(); } } }
//TODO: complete this test daemon/server example, and a client //TODO: maybe generalise it and integrate it into the core public static void Main(string[] args) { bool isServer; if (args.Length == 1 && args[0] == "server") { isServer = true; } else if (args.Length == 1 && args[0] == "client") { isServer = false; } else { Console.Error.WriteLine("Usage: test-server [server|client]"); return; } string addr = "unix:abstract=/tmp/dbus-ABCDEFGHIJ"; Connection conn; ObjectPath myOpath = new ObjectPath("/org/ndesk/test"); string myNameReq = "org.ndesk.test"; if (!isServer) { conn = new Connection(addr); DemoObject demo = conn.GetObject <DemoObject> (myNameReq, myOpath); demo.GiveNoReply(); //float ret = demo.Hello ("hi from test client", 21); float ret = 200; while (ret > 5) { ret = demo.Hello("hi from test client", (int)ret); Console.WriteLine("Returned float: " + ret); System.Threading.Thread.Sleep(1000); } } else { string path; bool abstr; Address.Parse(addr, out path, out abstr); AbstractUnixEndPoint ep = new AbstractUnixEndPoint(path); Socket server = new Socket(AddressFamily.Unix, SocketType.Stream, 0); server.Bind(ep); //server.Listen (1); server.Listen(5); while (true) { Console.WriteLine("Waiting for client on " + addr); Socket client = server.Accept(); Console.WriteLine("Client accepted"); client.Blocking = true; PeerCred pc = new PeerCred(client); Console.WriteLine("PeerCred: pid={0}, uid={1}, gid={2}", pc.ProcessID, pc.UserID, pc.GroupID); conn = new Connection(null); conn.ns = new NetworkStream(client); conn.SocketHandle = (long)client.Handle; //ConnectionHandler.Handle (conn); //in reality a thread per connection is of course too expensive ConnectionHandler hnd = new ConnectionHandler(conn); new Thread(new ThreadStart(hnd.Handle)).Start(); Console.WriteLine(); } } }