Bonjour , je voudrais recupérer au serveur l'adresse IP et le localhost ,
je recopie bêtement des samples internet car je n'y comprend rien.
Dejà rien en lui passant "RServerTransportSink" le serveur est innopérationnel !
Comprend rien !
C'était plus simple en natif .
Voici le code :
ET SERVEUR :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 public class ProductServer : MarshalByRefObject, IProduct { public Product Find() { return new Product() { Id = "P01", Name = "Product_1", Price=100 }; } public List<Product> FindAll() { return new List <Product> { new Product() { Id = "P01", Name = "Product_1", Price = 100 }, new Product() { Id = "P02", Name = "Product_2", Price = 200 }, new Product() { Id = "P03", Name = "Product_3", Price = 300 }, }; } } public interface IProduct { Product Find(); List<Product> FindAll(); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 public class ProductServer : MarshalByRefObject, IProduct { public Product Find() { return new Product() { Id = "P01", Name = "Product_1", Price=100 }; } public List<Product> FindAll() { return new List <Product> { new Product() { Id = "P01", Name = "Product_1", Price = 100 }, new Product() { Id = "P02", Name = "Product_2", Price = 200 }, new Product() { Id = "P03", Name = "Product_3", Price = 300 }, }; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 class Program { static void Main(string[] args) { try { RServerTransportSink rp = new RServerTransportSink(); IDictionary properties = new Hashtable(); properties.Add("port", 9999); properties.Add("secure", true); properties.Add("impersonate", true); TcpServerChannel channel = new TcpServerChannel(properties,rp); ChannelServices.RegisterChannel(channel, true); RemotingConfiguration.RegisterWellKnownServiceType(typeof(ProductServer), "IProduct", WellKnownObjectMode.SingleCall); Console.WriteLine("Server is running..."); Console.ReadKey(); } catch (Exception) { Console.WriteLine("Can't start server..."); } } }ET LE CLIENT :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 internal class RServerTransportSink : IServerChannelSinkProvider { IServerChannelSinkProvider nextProvider; public IServerChannelSinkProvider Next { get { return this.nextProvider; } set { this.nextProvider = value; } } //IServerChannelSinkProvider IServerChannelSinkProvider.Next { get => _epsl; set => _epsl = value; } IServerChannelSink IServerChannelSinkProvider.CreateSink(IChannelReceiver channel) { return nextProvider.CreateSink(channel); } void IServerChannelSinkProvider.GetChannelData(IChannelDataStore channelData) { } }
Ça ne se connecte même pas, sauf si je passe nulle à new TcpServerChannel à la place de 'rp', j'en ai besoin pour récupérer l'adresse ip du client.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 class Program { static void Main(string[] args) { try { IDictionary properties = new Hashtable(); properties.Add("secure", true); //properties.Add("connectionTimeout", 5000); properties.Add("tokenImpersonationLevel", "Impersonation"); TcpClientChannel clientChannel = new TcpClientChannel(properties, null); ChannelServices.RegisterChannel(clientChannel,true); //ChannelServices.RegisterChannel(new TcpChannel(), false); IProduct iproduct = (IProduct)(Activator.GetObject(typeof(IProduct), "tcp://localhost:9999/IProduct")); Console.WriteLine("Product Information:"); Product product = iproduct.Find(); Console.WriteLine($"\nProduct Id: {product.Id}\nProduct Name: {product.Name}\nProduct Price: {product.Price}"); foreach (var item in iproduct.FindAll()) { Console.WriteLine($"\nProduct Id: {item.Id}\nProduct Name: {item.Name}\nProduct Price: {item.Price}"); Console.WriteLine("========================================="); } Console.ReadKey(); } catch (Exception) { Console.WriteLine("Can't connect to the server..."); Console.ReadKey(); } } }
Les sockets c'était plus simple et on avait la possibilité de récupérer l'adresse IP du client.
Voilà.
Merci
Partager