Welcome back.
The hosting server looks like this, remember to add a reference to system.runtime.remoting and to your own server side objects.
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Serialization
Public Class HostingServer
Shared Sub main()
Dim _Server As New BinaryServerFormatterSinkProvider
Dim _Client As New BinaryClientFormatterSinkProvider
Dim _props As IDictionary = New Hashtable
Dim _channel As TcpChannel
_Server.TypeFilterLevel = _
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full
_props("port") = 8080
_channel = New TcpChannel(_props, _Client, _Server)
ChannelServices.RegisterChannel(_channel, True)
RemotingConfiguration.RegisterWellKnownServiceType _
(GetType(Server_Objects.Server), _
"Serv", WellKnownObjectMode.Singleton)
Console.WriteLine("Press any key to quit.")
Console.ReadLine()
ChannelServices.UnregisterChannel(_channel)
End Sub
End Class
The first part sets the security settings no need to bog down in that. Then we instanciate the channel with the security settings and set it to listen to the port 8080. You can use any port you like that is availible. Register the channel so that the app listens for requests.
Now make the server object "reachable" and as a singleton. This is so that all clients will connect to the same server object.
Now create a new project within the same solution or a new one.
Public Class Server
Inherits MarshalByRefObject
Implements Interfaces.IServer
Private arr As New ArrayList
Public Sub SendToAll(ByVal str As String) Implements Interfaces.IServer.SendToAll
For Each obj As Interfaces.IRemoteClient In arr
obj.Data(str)
Next
End Sub
Public Sub Attach(ByVal irc As Interfaces.IRemoteClient) _
Implements Interfaces.IServer.Attach
arr.Add(irc)
Console.WriteLine(irc.Name & " has entered the chat.")
End Sub
Public Sub Detach(ByVal irc As Interfaces.IRemoteClient) _
Implements Interfaces.IServer.Detach
arr.Remove(irc)
Console.WriteLine(irc.Name & " has left the chat.")
End Sub
Public Function NumOfClients() As Integer _
Implements Interfaces.IServer.NumOfClients
Return arr.Count
End Function
End Class
Now when i´m trying to explain the code I realize that we probably should have started with the Interfaces. Well, well... Just paste the code into your project, as you see its straight forward. The server object implements tha Iserver interface and inherits the Marshalbyrefobject. This is the object that is made public by the hostingserver. All it does is attaching client interfaces to an arraylist, detaches them and counting them and the most importing thing, sending messages to all clients
Next up the mixed files.
Monday, February 25, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment