i have got an exception, i dont understand why? basically, below there is the bottom part of my server code that i try to use.
In short, i built 2 Dictionaries of streamWriter and StreamReader and one List of names.
Exception is thrown there:
Some of the collections that i use
Dictionary<string, StreamWriter> dicWrite = new Dictionary<string, StreamWriter>();
Dictionary<string, StreamReader> dicRead = new Dictionary<string, StreamReader>();
Dictionary<string, string> pmChatters = new Dictionary<string, string>();
StreamReader[] sr1 = new StreamReader[100];
StreamWriter[] sw1 = new StreamWriter[100];
List<string> names = new List<string>();
//The server thread function that reads and writes to the clients
public void ProssecClient(object o)
{
TcpClient connection = o as TcpClient;
if (connection == null)
{
return;
}
// here i try to obtain every stream and put it into an array of streams
// a and m are ints, they go up in number , each time a user logs in and receives a TCP reference..
sr1[a++] = new StreamReader(connection.GetStream());
sw1[m++] = new StreamWriter(connection.GetStream());
int i = 0;
try
{
while (true)
{
i = 0;
inputObtained = sr.ReadLine();
// Obtains a name (this function seperates what was sent with the name attached). dont worry about this. it works, it returns the name of the client
currentSender = checkForName(inputObtained);
//Adds names that dont exist, in the server , (it regeistered a new user)
if (!names.Contains(currentSender))
{
names.Add(currentSender);
dicWrite.Add(names[i], sw1[m]);
}
for ( i = 0; i <= m; i++)
{
if (names[i]!=null)
{
//If the Chatter is not in pm
if (pmChatters != null && !pmChatters.ContainsKey(names[i]))
{
dicWrite[names[i]].WriteLine(inputObtained);//The exception, is constatnly thrown here
dicWrite[names[i]].Flush();
}
//if the chatter is in pm
else if (names[i] != null && pmChatters != null && pmChatters.ContainsKey(names[i]))
{
dicWrite[pmChatters[currentSender]].WriteLine(inputObtained);
dicWrite[pmChatters[currentSender]].Flush();
}
else
{
break;
}
}
}
}
}
catch {
Console.WriteLine(currentSender + ". Name of user:" + names[0] + ". Current User: " + currentSender + ". " +
" m is"+m + " i is "+ i + " " + " client left");
}
}