public void Disconnect() { // Create delegate to remote method Action <String> RemoteDel = new Action <String>(conversation.UnregisterClient); // Create delegate to local callback AsyncCallback RemoteCallback = new AsyncCallback(Client.UnregisterClientCallBack); // Call remote method IAsyncResult RemAr = RemoteDel.BeginInvoke(nickname, RemoteCallback, null); conversation = null; ClientRemoteObject remoteObject = (ClientRemoteObject)RemotingServices.Unmarshal(conversationReference); RemotingServices.Disconnect(remoteObject); ChannelServices.UnregisterChannel(channel); }
public bool Connect(String nickname, String port) { if (nickname.Equals("")) { printOnWarningLabel("Warning: The client cannot connect to the chat using an empty nickname."); return(false); } int portNumber; if (!int.TryParse(port, out portNumber) || portNumber < MINPORT || portNumber > MAXPORT) { printOnWarningLabel("Warning: The client can only connect through ports between " + MINPORT + " and " + MAXPORT + "."); return(false); } EventHandler <UpdateChatEventArgs> updateChatHandler = new EventHandler <UpdateChatEventArgs>(UpdateChat); channel = new TcpChannel(portNumber); ChannelServices.RegisterChannel(channel, true); ClientRemoteObject remoteObject = new ClientRemoteObject(updateChatHandler); conversation = (IServerRemoteObject)Activator.GetObject( typeof(IServerRemoteObject), "tcp://localhost:8086/Conversation"); conversationReference = RemotingServices.Marshal( remoteObject, "Conversation", typeof(ClientRemoteObject)); try { // Create delegate to remote method Action <String, int> RemoteDel = new Action <String, int>(conversation.RegisterClient); // Create delegate to local callback AsyncCallback RemoteCallback = new AsyncCallback(Client.RegisterClientCallBack); // Call remote method IAsyncResult RemAr = RemoteDel.BeginInvoke(nickname, portNumber, RemoteCallback, null); } catch (SocketException) { printOnChatBox("Could not locate server"); return(false); } this.nickname = nickname; return(true); }