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

Silverlight Discussion :

Communication Java - Silverlight via Socket


Sujet :

Silverlight

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Novembre 2009
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2
    Points : 1
    Points
    1
    Par défaut Communication Java - Silverlight via Socket
    Bonjour,

    Je précise que j'ai déjà cherché des solutions sur de nombreux forums... Sans succès, évidemment !

    Dans le cadre d'un projet de fin d'études, je suis amené à développer une application dont l'interface doit être réalisée en Silverlight. Cette interface doit créer, envoyer et recevoir des données via des sockets vers un serveur local développé en java.

    Le serveur est opérationnel, nous avons réalisé un programme client de test qui prouve que le serveur java fonctionne correctement.

    Voici donc mon problème. J'ai besoin de créer un socket, d'y affecter des données et de les envoyer vers le serveur java.
    Logiciel utilisé : Microsoft Blend 3, Windows XP
    Voici le code source de mon c# silverlight :
    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
     
    Code C# :
                    public void loadData()
    		{
    			IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("localhost"), 1050);
    			HtmlPage.Window.Alert("IP : " + endPoint.ToString());
    			Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     
    			SocketAsyncEventArgs args = new SocketAsyncEventArgs();
    			args.UserToken = socket;
    			args.RemoteEndPoint = endPoint;
    			args.Completed += new EventHandler<SocketAsyncEventArgs>(OnSocketConnectCompleted);
    			socket.ConnectAsync(args);
    		}
     
    		private void OnSocketConnectCompleted(object sender, SocketAsyncEventArgs e)
    		{
    			HtmlPage.Window.Alert("Dans on completed");
    			byte[] response = new byte[1024];
    			response[0] = 255;
    			response[1] = 254;
     
    			e.SetBuffer(response, 0, response.Length);
    			e.Completed -= new EventHandler<SocketAsyncEventArgs>(OnSocketConnectCompleted);
    			e.Completed += new EventHandler<SocketAsyncEventArgs>(OnSocketReceive);
    			Socket socket = (Socket)e.UserToken;
    			socket.ReceiveAsync(e);
    		}
     
    		private void OnSocketReceive(object sender, SocketAsyncEventArgs e)
    		{
    			HtmlPage.Window.Alert("Dans on receive");
    		}
    Voici ce que j'essaye de faire : je veux établir la connexion puis envoyer des données au serveur java. Or, je n'ai même pas l'affichage de mon message "Dans on completed", ce qui signifie que la connexion a échouée. De plus, le serveur ne fait aucune trace indiquant une connexion.

    Vous pouvez télécharger le serveur java et le client java qui test le serveur en cliquant ici. Le port du serveur et du client par défaut est 1050. Vous pouvez les changer en ajoutant un argument supplémentaire de votre choix.

    Pouvez-vous m'aider à résoudre ce problème.

    Merci d'avance.

  2. #2
    Expert éminent sénior
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Points : 13 380
    Points
    13 380
    Par défaut
    Plusieurs choses :

    En silverlight tu ne peux te connecter que sur des ports compris entre 4502 et 4534.

    Deuxième chose, avant d'établir la connexion, Silverlight réclame le fichier clientaccesspolicy.xml sur le port 943.

    Regarde sur cette page de la msdn tout en bas il y a un exemple de serveur pour la demande du fichier xml.
    http://msdn.microsoft.com/en-us/libr...8VS.95%29.aspx
    Introduction à Silverlight 4 (new) ; Localisation d'une application Silverlight (new) ;
    Mon espace perso[/B]

    La connaissance s’acquiert par l’expérience, tout le reste n’est que de l’information. Albert Einstein[/SIZE]

  3. #3
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Ne pourrais-tu point passer par un socket dans ton serveur Silverlight pour faire transiter les messages entre client Silverlight et serveur Java ?
    " Le croquemitaine ! Aaaaaah ! Où ça ? " ©Homer Simpson

  4. #4
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Novembre 2009
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2
    Points : 1
    Points
    1
    Par défaut
    Bonjour à vous,

    Tout d'abord, désolé du retard dans la réponse mais je ne travaille sur ce projet que deux jours par semaine.

    J'ai tenu compte de vos remarques et voici ce que j'ai fait :
    J'ai créé un serveur en C# qui me retourne le clientaccesspolicy.xml avec succès. Ainsi, maintenant lorsque je lance mon application Silverlight, je vois bien que mon application crée un socket puisque mon serveur java affiche "Connexion détectée".

    Cependant, je suis toujours dans l'incapacité d'effectuer des échanges de sockets entre Silverlight et Java.

    Voici mon code Silverlight :
    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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
     
    private static AutoResetEvent autoConnectEvent = new AutoResetEvent(false);
    		private Boolean connected = false;
    		private Socket clientSocket;
    		private IPEndPoint endPoint;
    		private const Int32 ReceiveOperation = 1, SendOperation = 0;
    		private static AutoResetEvent[]  autoSendReceiveEvents = new AutoResetEvent[]
            {
                new AutoResetEvent(false),
                new AutoResetEvent(false)
            };
     
    public void loadData()
    		{
    			endPoint = new IPEndPoint(IPAddress.Parse("192.168.5.195"), 4503);
    			clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     
    			SocketAsyncEventArgs args = new SocketAsyncEventArgs();
    			args.UserToken = clientSocket;
    			args.RemoteEndPoint = endPoint;
    			args.Completed += new EventHandler<SocketAsyncEventArgs>(OnConnect);
    			if (!clientSocket.ConnectAsync(args))
    			{
    				if (args.SocketError != SocketError.Success)
    				{
    						HtmlPage.Window.Alert("Echec de la connexion du socket au servuer ");
    				}
    			}
    		}
     
    		// Calback for connect operation
            private void OnConnect(object sender, SocketAsyncEventArgs e)
            {
    			HtmlPage.Window.Alert("Début onConnect");
     
                // Signals the end of connection.
                autoConnectEvent.Set();
     
    			HtmlPage.Window.Alert("Socket Error : " + e.SocketError);
                // Set the flag for socket connected.
                connected = (e.SocketError == SocketError.Success);
            }
     
    		// Exchange a message with the host.
            internal String SendReceive(String message)
            {
    			HtmlPage.Window.Alert("Début send Receive");
                if (connected)
                {
    				HtmlPage.Window.Alert("je suis bien connecté !");
     
                    // Create a buffer to send.
                    Byte[] sendBuffer = Encoding.Unicode.GetBytes(message);
     
                    // Prepare arguments for send/receive operation.
                    SocketAsyncEventArgs completeArgs = new SocketAsyncEventArgs();
                    completeArgs.SetBuffer(sendBuffer, 0, sendBuffer.Length);
                    completeArgs.UserToken = clientSocket;
                    completeArgs.RemoteEndPoint = endPoint;
                    completeArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnSend);
     
                    // Start sending asyncronally.
                    clientSocket.SendAsync(completeArgs);
     
                    // Wait for the send/receive completed.
                    AutoResetEvent.WaitAll(autoSendReceiveEvents);
     
                    // Return data from SocketAsyncEventArgs buffer.
                    return Encoding.Unicode.GetString(completeArgs.Buffer,  completeArgs.Offset, completeArgs.BytesTransferred);
                }
                else
                {
    				HtmlPage.Window.Alert("Je ne suis pas connecté !");
                    //throw new SocketException((Int32)SocketError.NotConnected);
    				return "";
                }
            }
     
    		private void OnSend(object sender, SocketAsyncEventArgs e)
            {
                // Signals the end of send.
                autoSendReceiveEvents[ReceiveOperation].Set();
     
                if (e.SocketError == SocketError.Success)
                {
                    if (e.LastOperation == SocketAsyncOperation.Send)
                    {
                        // Prepare receiving.
                        Socket s = e.UserToken as Socket;
     
                        byte[] receiveBuffer = new byte[255];
                        e.SetBuffer(receiveBuffer, 0, receiveBuffer.Length);
                        e.Completed += 
                          new EventHandler<SocketAsyncEventArgs>(OnReceive);
                        s.ReceiveAsync(e);
                    }
                }
                else
                {
                    ProcessError(e);
                }
            }
     
    		private void ProcessError(SocketAsyncEventArgs e)
            {
    			HtmlPage.Window.Alert("Dans process Error");
     
                Socket s = e.UserToken as Socket;
                if (s.Connected)
                {
                    // close the socket associated with the client
                    try
                    {
                        s.Shutdown(SocketShutdown.Both);
                    }
                    catch (Exception)
                    {
                        // throws if client process has already closed
                    }
                    finally
                    {
                        if (s.Connected)
                        {
                            s.Close();
                        }
                    }
                }
     
                // Throw the SocketException
                throw new SocketException((Int32)e.SocketError);
            }
     
    		private void OnReceive(object sender, SocketAsyncEventArgs e)
            {
                // Signals the end of receive.
                autoSendReceiveEvents[SendOperation].Set();
            }
    Lors du lancement de l'application, je ne rentre jamais dans la méthode "OnConnect". Pourtant, mon serveur java me dit bien "Connexion détectée" et je n'ai pas l'affichage du message d'erreur de connexion en Silverlight (voir code ci-dessus).

    Avez-vous une autre idée qui pourrait me permettre d'avancer sur mon projet ?

    Merci d'avance.

  5. #5
    Expert éminent sénior
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Points : 13 380
    Points
    13 380
    Par défaut
    Ah ben là c'est pas facile sans tester.

    Essaye de mettre un breakpoint dans OnConnect pour voir si tu rentres vraiment jamais dedans.
    Introduction à Silverlight 4 (new) ; Localisation d'une application Silverlight (new) ;
    Mon espace perso[/B]

    La connaissance s’acquiert par l’expérience, tout le reste n’est que de l’information. Albert Einstein[/SIZE]

Discussions similaires

  1. communication Java / player Flash via XMLSocket
    Par Sylvain__A_ dans le forum Langage
    Réponses: 4
    Dernier message: 05/02/2010, 15h45
  2. Communication via Socket TCP
    Par onet dans le forum Bibliothèques
    Réponses: 28
    Dernier message: 09/10/2009, 16h11
  3. Communication page web (AJAX/CGI) et Programme en C++ via sockets
    Par sagopa dans le forum Serveurs (Apache, IIS,...)
    Réponses: 2
    Dernier message: 01/10/2008, 12h07
  4. Connexion via Socket JAVA
    Par jihene dans le forum API standards et tierces
    Réponses: 2
    Dernier message: 08/06/2006, 18h50
  5. python & flash : communication via socket -> Null byt
    Par arcane14 dans le forum Réseau/Web
    Réponses: 3
    Dernier message: 30/01/2006, 21h19

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