.NET Remoting, connection au serveur
Bonjour,
J'essaye de créer une petite appli client-serveur.
je viens de faire le tuto .NET Remoting et jusque la tout vas bien.
J'aimerais modifier un peu l'exemple afin de pouvoir entrer à la main dans une text-box l'adresse de mon serveur pour ne pas l'avoir codée en dur dans mon programme.
Je récupère donc la valeur par défaut de ma textbox pour me connecter au début du programme et la ca marche.
En revanche j'ai créer un bouton pour me connecter une fois l'adresse tapée et je demande la connection au clic et la ca ne marche plus.
Quelqu'un saurait-il ma dire pourquoi?
Pour être plus clair voici le code qui fonctionne :
Code:
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
| Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Public Class Form1
Dim remoteOperation As RemotingInterfaces.IRemoteOperation
Public Sub New()
InitializeComponent()
Connect()
End Sub 'New
Public Function Connect()
Try
Dim channel As TcpChannel = New TcpChannel
Dim ensureSecurity As Boolean = True
ChannelServices.RegisterChannel(channel, ensureSecurity)
remoteOperation = CType(Activator.GetObject(GetType(RemotingInterfaces.IRemoteOperation), _
TXT_Addr_Srv.Text), RemotingInterfaces.IRemoteOperation)
Catch
MessageBox.Show("Erreur de connexion au serveur")
End Try
Return Nothing
End Function
Private Sub buttonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonAdd.Click
Try
If (Not (remoteOperation) Is Nothing) Then
Dim a As Integer = Int32.Parse(textBoxA.Text)
Dim b As Integer = Int32.Parse(textBoxB.Text)
textBoxResAdd.Text = remoteOperation.Addition(a, b).ToString
End If
Catch
MessageBox.Show("Erreur !")
End Try
End Sub
Private Sub buttonInc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonInc.Click
Try
If (Not (remoteOperation) Is Nothing) Then
Dim valeur As Integer = Int32.Parse(textBoxValeur.Text)
textBoxResInc.Text = remoteOperation.Incrementation(valeur).ToString
End If
Catch
MessageBox.Show("Erreur !")
End Try
End Sub
End Class |
Et celui qui ne fonctionne pas :
Code:
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
| Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Public Class Form1
Dim remoteOperation As RemotingInterfaces.IRemoteOperation
Public Sub New()
InitializeComponent()
End Sub 'New
Public Function Connect()
Try
Dim channel As TcpChannel = New TcpChannel
Dim ensureSecurity As Boolean = True
ChannelServices.RegisterChannel(channel, ensureSecurity)
remoteOperation = CType(Activator.GetObject(GetType(RemotingInterfaces.IRemoteOperation), _
TXT_Addr_Srv.Text), RemotingInterfaces.IRemoteOperation)
Catch
MessageBox.Show("Erreur de connexion au serveur")
End Try
Return Nothing
End Function
Private Sub buttonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonAdd.Click
Try
If (Not (remoteOperation) Is Nothing) Then
Dim a As Integer = Int32.Parse(textBoxA.Text)
Dim b As Integer = Int32.Parse(textBoxB.Text)
textBoxResAdd.Text = remoteOperation.Addition(a, b).ToString
End If
Catch
MessageBox.Show("Erreur !")
End Try
End Sub
Private Sub buttonInc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonInc.Click
Try
If (Not (remoteOperation) Is Nothing) Then
Dim valeur As Integer = Int32.Parse(textBoxValeur.Text)
textBoxResInc.Text = remoteOperation.Incrementation(valeur).ToString
End If
Catch
MessageBox.Show("Erreur !")
End Try
End Sub
Private Sub Bt_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Connect.Click
Connect()
End Sub
End Class |
Je précise que je suis débutant en .net et que l'erreur est probablement bête.
Je suppose que dans mon "public sub new" il me faut une instruction qui attende le clic ou quelque chose comme ca mais je ne trouve pas quoi mettre
Merci d'avance