Bonjour tous le monde ,

Excusez moi de vous déranger mais j'ai un petit probleme avec une application de test que je fais.

Globalement, j'ai créé un service pour qu'il détecte l'arrivée de données provenant d'une application.

De chaque coté ses données sont l'association du nom de l'ordinateur codé un systeme md5 et du nom d'une fonction à effectuer.

Par exemple :
j'ai deux boutons activ et desactiv. Lorsque l'utilisateur clique sur activ ou sur desactive, il envoie donc les données suivantes : nomordicodé + "activ" ou nomordicodé + "desactiv".

Ces informations sont ensuites récupérées par le service qui va voir si elles viennent bien de l'ordinateur où il tourne et ensuite identifier si la donnée envoyée correspond à "activ" ou "desactiv". Dans le code ci-dessous je lui fais renvoyer les données (activée ou désactivée) à l'application afin qu'elle affiche "activée" ou "desactivée" dans une messagebox .

Mon probleme est que j'obtiens toujours la valeur "erreur" qui s'affiche dans la messagebox de l'application.

Voici le code de mon service :

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
Imports System.Security.Cryptography
Imports System.text
Imports System.Net.sockets
Imports System
Imports System.Net
Imports System.Threading
Imports Microsoft.VisualBasic
Imports System.ServiceProcess.ServiceBase
Imports Microsoft.Win32
Imports System.Windows.forms
'----------------------------------------------
Public Class EssWitchService
 
    Public reception As New Threading.ManualResetEvent(False)
    Private listener As Net.Sockets.TcpListener
    Const PORT_NUM As Integer = 1039
    Dim EndPoint As Net.IPEndPoint
    Public verif As String = getMd5Hash()
 
 
    Protected Overrides Sub OnStart(ByVal args() As String)
        Dim thread As New System.Threading.Thread(AddressOf General)
        thread.IsBackground = True
        thread.Start()
    End Sub
 
       Public Sub General()
 
        Try
            Dim ipLocale As Long = 16777343
            EndPoint = New Net.IPEndPoint(ipLocale, 1039)
            Dim listener As New TcpListener(EndPoint)
            listener.Start()
            While True
                reception.Reset()
                listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptCallback), listener)
                reception.WaitOne()
            End While
 
 
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub
 
    Public Sub AcceptCallback(ByVal ar As IAsyncResult)
        Try
            Dim bit(1024) As Byte
            Dim biit(1024) As Byte
            Dim listener As TcpListener = CType(ar.AsyncState, TcpListener)
            Dim client As TcpClient = listener.EndAcceptTcpClient(ar)
            Dim stream As NetworkStream = client.GetStream()
            stream.Read(bit, 0, bit.Length)
            Dim valeurrecue As String = System.Text.Encoding.ASCII.GetString(bit, 0, bit.Length)
 
            Dim cle As String = getMd5Hash()
 
            Dim activ As String = cle + "activ"
            Dim desactiv As String = cle + "desactiv"
            Dim retour As String = "erreur"
 
            If (valeurrecue = activ) Then
                retour = "activee"
            End If
            If (valeurrecue = desactiv) Then
                retour = "desactivee"
            End If
 
            biit = System.Text.Encoding.ASCII.GetBytes(retour.ToCharArray)
            stream.BeginWrite(biit, 0, biit.Length, New AsyncCallback(AddressOf WriteCallback), stream)
 
        Catch ex As Exception
 
        End Try
 
    End Sub
 
    Public Sub WriteCallback(ByVal ar As IAsyncResult)
 
        Dim Stream As NetworkStream = CType(ar.AsyncState, NetworkStream)
        Stream.EndWrite(ar)
        reception.Set()
 
    End Sub
 
    Function getMd5Hash() As String
 
        Dim ordi As String = System.Environment.MachineName
        Dim acrypt As String = ordi
        Dim md5Hasher As MD5 = MD5.Create()
 
        Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(acrypt))
 
        Dim sBuilder As New StringBuilder()
 
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i
 
        Return sBuilder.ToString()
 
    End Function
 
   Protected Overrides Sub OnStop()
        listener.Stop()
    End Sub
 
End Class
Et l'application :
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
 
Imports System.Security.Cryptography
Imports System.Text
 
Public Class Form1
 
    Public cle As String = getMd5Hash()
    Public tab1(1024) As Byte
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Try
            Dim client As New Net.Sockets.TcpClient()
 
            'Le client va lancer la fonction SendCallBack'
            client.BeginConnect("127.0.0.1", 1039, New AsyncCallback(AddressOf SendActivCallback), client)
 
 
        Catch ex As Exception
            MessageBox.Show(ex.Message)
 
        End Try
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim client As New Net.Sockets.TcpClient()
 
            'Le client va lancer la fonction SendCallBack'
            client.BeginConnect("127.0.0.1", 1039, New AsyncCallback(AddressOf SendDesactivCallback), client)
 
 
        Catch ex As Exception
            MessageBox.Show(ex.Message)
 
        End Try
    End Sub
 
 
 
    Public Sub SendActivCallback(ByVal ar As IAsyncResult)
        Try
            Dim client As Net.Sockets.TcpClient = CType(ar.AsyncState, Net.Sockets.TcpClient)
 
            client.EndConnect(ar)
            Dim envoie As String = cle + "activ"
            Dim content(1024) As Byte
 
            content = System.Text.Encoding.ASCII.GetBytes(envoie.ToCharArray)
 
            Dim flux As Net.Sockets.NetworkStream = client.GetStream()
            flux.Write(content, 0, content.Length)
            flux.BeginRead(tab1, 0, tab1.Length, New AsyncCallback(AddressOf ReadCallback), flux)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
 
    End Sub
 
    Public Sub SendDesactivCallback(ByVal ar As IAsyncResult)
 
        Try
 
            Dim client As Net.Sockets.TcpClient = CType(ar.AsyncState, Net.Sockets.TcpClient)
            client.EndConnect(ar)
            Dim envoie As String = cle + "desactiv"
            Dim content(1024) As Byte
            content = System.Text.Encoding.ASCII.GetBytes(envoie.ToCharArray())
 
            Dim flux As Net.Sockets.NetworkStream = client.GetStream()
            flux.Write(content, 0, content.Length)
            flux.BeginRead(tab1, 0, tab1.Length, New AsyncCallback(AddressOf ReadCallback), flux)
        Catch ex As Exception
 
            MessageBox.Show(ex.Message)
 
        End Try
 
    End Sub
 
    Public Sub ReadCallback(ByVal ar As IAsyncResult)
 
        Dim flux As Net.Sockets.NetworkStream = CType(ar.AsyncState, Net.Sockets.NetworkStream)
        Dim nbResultat As Integer = flux.EndRead(ar)
        Dim nbRecup3 As String = System.Text.Encoding.ASCII.GetString(tab1)
        MessageBox.Show(nbRecup3)
    End Sub
 
 
    Function getMd5Hash() As String
 
        Dim ordi As String = System.Environment.MachineName
        Dim acrypt As String = ordi
        Dim md5Hasher As MD5 = MD5.Create()
 
        Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(acrypt))
 
        Dim sBuilder As New StringBuilder()
 
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i
 
        Return sBuilder.ToString()
 
    End Function
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = cle
    End Sub
End Class
(note, ds l'appli j'affiche au demarrage la cle obtenue avec le codage md5 ds une textbox)

En tout cas, je vous remercie d'avance pour votre aide.

Vinou