I am working on Lan based interoffice messaging system.
The steps in my applications are :
- Server starts and listen for clients
- Clients on connection gets the view of all the other connected clients.
- On connection to the server i do check in the database that if that client is authorize or not.
- if i dont check in the database on client connection to the server then the application does work normally other wise the client if not exists in the database the the client application got closed.
the related code is here :
public void CheckUserName(string userName)
{
if (userName != "Usman") // checking in database( a static name)
{
//Check if the username is registered
Send("sorry@Invalid Username, try another Username!!");
Disconnect();
return;
}
else
{
//If name is not duplicate then the client is connected
this.connected =true;
this.userName =userName;
//Build the Usernames list and send it to the client
StringBuilder userList = new StringBuilder();
userList.Append(this.clientID);
Hashtable clientTable =ClientList.GetList;
foreach(DictionaryEntry d in clientTable)
{
//Seperate the usernames by a '@'
userList.Append("@");
userList.Append(d.Value.ToString());
}
//Start the llistening
lock(myClient.GetStream())
{
AsyncCallback GetStreamMsgCallback = new AsyncCallback(GetStreamMsg);
myClient.GetStream().BeginRead(recByte,0,1024,GetStreamMsgCallback,null);
}
//Send the Userlist
Send(userList.ToString());
//Raise the Connected Event
if(Connected!=null)
{
EventArgs e = new EventArgs();
Connected(this, e);
}
}
}
Do anyone could suggest that what to do to get rid of this bad thing?
Have you considered initially querying the database for all enabled users when the server starts then caching those results? this way you can avoid having to query on every user connection. You can store the entitled users in a Dictionary or some other style of lookup table.