Forgive me but I'm not quite sure where in my code things are going wrong! I'm working on creating a multithreaded tcp server, and am attempting to store strings using a dictionary. The code looks like this:
class Echo : Iprotocol
{
public Dictionary<string, string> dictionary = new Dictionary<string, string>();
private const int BUFFSIZE = 32; //buffer size
private Socket client_sock; //Socket
private Ilogger logger; // logger
public Echo(Socket sock, Ilogger log)
{
this.client_sock = sock;
this.logger = log;
}
public string handlewhois(string inname)
{
ArrayList entry = new ArrayList();
string name = inname;
string message = null;
if (dictionary.ContainsKey(name) == true)
{
entry.Add(System.DateTime.Now + "Dictionary reference found at thread: " + Thread.CurrentThread.GetHashCode());
message = dictionary[name];
}
else
{
entry.Add(System.DateTime.Now + "Dictionary reference not found at thread: " + Thread.CurrentThread.GetHashCode());
message = "ERROR: no entries found";
}
logger.writeEntry(entry);
return message;
}
public string handlewhois(string inname, string inlocation)
{
ArrayList entry = new ArrayList();
string name = inname;
string location = inlocation;
string message = null;
entry.Add(System.DateTime.Now + "Dictionary reference created or updated at thread: " + Thread.CurrentThread.GetHashCode());
dictionary.Add(name, location);
message = "OK";
logger.writeEntry(entry);
return message;
}
}
It runs perfectly fine, however as I step through it in debug, I see the the dictionary entry created, however when it gets to the line: logger.writeEntry(entry);
It suddenly vanishes, with dictionary not containing any values.
I think it may have something to do with the multithreading but honestly I have no idea!