IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Recupérer adresse ip [Débutant]


Sujet :

C#

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2011
    Messages
    905
    Détails du profil
    Informations personnelles :
    Localisation : France, Vienne (Poitou Charente)

    Informations forums :
    Inscription : Mai 2011
    Messages : 905
    Points : 85
    Points
    85
    Par défaut Recupérer adresse ip
    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 :
    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();
        }
    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
     
     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...");
                }
            }
        }
    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)
            {
     
            }
        }
    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
    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();
                }
            }
        }
    Ç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.

    Les sockets c'était plus simple et on avait la possibilité de récupérer l'adresse IP du client.
    Voilà.

    Merci

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2011
    Messages
    905
    Détails du profil
    Informations personnelles :
    Localisation : France, Vienne (Poitou Charente)

    Informations forums :
    Inscription : Mai 2011
    Messages : 905
    Points : 85
    Points
    85
    Par défaut
    J'ai résolu le problème grâce à ce site :
    https://stackoverflow.com/questions/...ing-invocation

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Recupérer adresse E-mail
    Par redmonster dans le forum Android
    Réponses: 5
    Dernier message: 31/05/2012, 18h34
  2. [Inno Setup] Recupération adresse IP
    Par Mimie37 dans le forum Outils
    Réponses: 1
    Dernier message: 29/05/2012, 11h37
  3. [WD10] Recuprer adresse MAC
    Par nuFox dans le forum WinDev
    Réponses: 6
    Dernier message: 08/04/2009, 08h51
  4. recuprer adresse IP et local host name
    Par einsteineuzzz dans le forum C++
    Réponses: 2
    Dernier message: 08/01/2007, 15h55
  5. Recupérer l'adresse du site web
    Par jeyce dans le forum Général JavaScript
    Réponses: 7
    Dernier message: 16/03/2006, 12h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo