日期:2013-01-11  浏览次数:20495 次

Chat Server with Client implemented in C#

Submitted By Date of Submission
Nanu  01/19/2000

  
Download: WorkingServer.zip 11 KB
The Main Heart of the program is taken from the sample program of Gopalan Suresh Raj  modified as per requirement & presented in front of you.... Hope it would be helpful for somebody

How to Run the Chat Server and the Client?

(1) First start the server.exe
(2) Next start formclient.exe
(3) Give you nick name "xyz" & click the connect button .... you will be connected
to the server & a message will be displaed in the yellow box
(4) Run formclient.exe again with start-->run-->formclient.exe & give another
nick name for trial & error.








The Server Code: The server code is implemented in newServer.cs.

/**
* Article: Channels and .NET Remoting
* Copyright (c) 2000, Gopalan Suresh Raj. All Rights Reserved.
* File: NewServer.cs
* ... Thanks Saurabh for great help to make it lively Your Knowledge Rules ....
* ... Thanks Mahesh for Loading it on Site .... It Rocks
* ... Thanks to You for Reading it You know better who you are :)
  
* ... The Main Heart of the program is taken from the sample program of Gopalan Suresh Raj
* ... modified as per requirement & presented in front of you.... Hope it would be helpful for somebody
*/
using System;
using System.IO;
using System.Collections;
using System.Runtime.Remoting;
using System.WinForms ;
using System.Runtime.Remoting.Channels.HTTP;
namespace com.icommware.PublishSubscribe {
  
public class Topic : MarshalByRefObject {
  
  
ArrayList userlist = new ArrayList() ;
ArrayList textboxlist = new ArrayList() ;
ArrayList listboxlist = new ArrayList() ;
///<summary>
/// Input : string username (by value) and RichTextBox , ListBox (by refrence)
/// Output: bool 'true' if username is unique else 'false '
///</summary>
public bool addUser(string user, ref RichTextBox textBox1, ref ListBox listBox1)
{
//check is username is present in the userlist 'ArrayList'
bool test=userlist.Contains(user) ;
if(test)
{
//if present then Give the message to use and return 'false'
textBox1.Text+="This Nick has already been taken, try changing your Nick \n" ;
return false ;
}
else{
//user is unique hence add him to the userlist
  
userlist.Add(user) ;
//add the RichTextBox reference to textboxlist
textboxlist.Add(textBox1);
//add to existing users list
foreach(ListBox lb in listboxlist)
{
lb.Items.Add(user) ;
}
//add the ListBox reference to listboxlist
listboxlist.Add(listBox1) ;
//Send to message only to the client connected
textBox1.Text+="Connected to server... \n" ;
//send message to everyone .
sendMessage(user+" has Joined Chat") ;
//add all the usernames in the ListBox of the client
foreach(string users in userlist)
{
listBox1.Items.Add(users) ;
}
  
return true ;
}
  
}
  
///<summary>
/// Input: string username
/// It is called when the user quits chat
///</summary>
public void removeUser(string user)
{
//check is the user is present in the userlist
try{
if(userlist.Contains(user))
{
//get the position of user in userlist
int i=userlist.IndexOf(user) ;
//remove user from userlist
userlist.Remove(user) ;
//remove user's RichTextBox reference from textboxarray
textboxlist.RemoveAt(i) ;
//remove user's ListBox refrence from listboxarray
listboxlist.RemoveAt(i) ;