Код библиотека классов
- Код: Выделить всё
Public Class clsTemperature
Inherits MarshalByRefObject
Public Function CtoF(ByVal CelsuisTemp As Single) As Single
'Converts Celsius temperature to Fahrenheit
Return CelsuisTemp * 9 / 5 + 32
End Function
Public Function FtoC(ByVal FahrenheitTemp As Single) As Single
'Converts Fahrenheit temperature to Celsius
Return (FahrenheitTemp - 32) * 5 / 9
End Function
End Class
Код Host приложения
- Код: Выделить всё
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim MyChannel As TcpChannel
Dim MyPort As Integer = 12345
Dim MyEndPoint As String = "TempConverter"
Dim MyObjType As System.Type
Dim ensureSecurity As Boolean
'Register the TCP Channel
MyChannel = New TcpChannel(MyPort)
ChannelServices.RegisterChannel(MyChannel, ensureSecurity)
'Register the class
MyObjType = GetType(TempConverter.clsTemperature)
RemotingConfiguration.RegisterWellKnownServiceType _
(MyObjType, MyEndPoint, WellKnownObjectMode.SingleCall)
MessageBox.Show("Server Started!")
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Application.Exit()
End Sub
End Class
Код клиентский формы
- Код: Выделить всё
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Public Class Form1
Inherits System.Windows.Forms.Form
Private Function PerformConversion(ByVal ConversionType As String, ByVal InputTemp As Single) As Single
'Build URI string
Dim MyPort As Integer = 12345
Dim MyMachine As String = "bsw2k"
Dim MyEndPoint As String = "TempConverter"
Dim URIString As String
URIString = "tcp://" & MyMachine & ":" & MyPort & "/" & MyEndPoint
'Create the remote object instance
Dim MyRemoteObject As TempConverter.clsTemperature
Dim MyObjType As System.Type = GetType(TempConverter.clsTemperature)
MyRemoteObject = CType(Activator.GetObject(MyObjType, URIString), TempConverter.clsTemperature)
'Call a method on the remote object
If MyRemoteObject Is Nothing Then
MessageBox.Show("Error!")
Else
If ConversionType.ToUpper = "CTOF" Then
Return MyRemoteObject.CtoF(InputTemp)
Else
Return MyRemoteObject.FtoC(InputTemp)
End If
End If
End Function
Private Sub btnConvertToC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvertToC.Click
txtTempC.Text = PerformConversion("FTOC", Convert.ToSingle(txtTempF.Text))
End Sub
Private Sub btnConvertToF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvertToF.Click
txtTempF.Text = PerformConversion("CTOF", Convert.ToSingle(txtTempC.Text))
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Вроде бы все нормально но запускаю программу дает ошибку:
“Этот хост неизвестен”
[/code]