Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dictionary containing arraylist for a running list of active users 1

Status
Not open for further replies.

hawaiiantrash

Programmer
May 31, 2007
3
US
im trying to store arraylists in a dictionary .. i dont thikn im doing this right because when i output .. i get 3 results .. b1,b2,and a collections object .. the end goal is to have a running list of channels (chat rooms) and their users (username,socketconnection) .. so when i need to send a message to a certain room i can simply use a key to pull the users and then loop through the results to get the users socket and send them a message



here is my code i have so far:





using System;

using System.Collections;

using System.Collections.Generic;


class Test

{

static void Main()

{

Dictionary<String,ArrayList> clientlist = new Dictionary<String,ArrayList>();


ArrayList clientinfo = new ArrayList();


// create channel

clientinfo.Add("a1");

clientinfo.Add("a2");

clientlist.Add("channel1",clientinfo);

clientinfo.Clear();

// add onto existing channel (key)

clientinfo.Add("b1");

clientinfo.Add("b2");

((ArrayList)clientlist["channel1"]).Add(clientinfo);

ArrayList results = (ArrayList)clientlist["channel1"];

Console.WriteLine("results size: " + results.Count);

for (int i = 0; i < results.Count; i++)

{

Console.WriteLine(results);

}

System.Threading.Thread.Sleep(5000);

}



}

perhaps im approaching this the wrong way even, please advise

thanks!
 
You want a lookup of your users in a chat room based on a unique key...

Dictionary<string, List<string>> rooms = new Dictionary<string, List<string>>;

//This creates a Dictionary with key value pairs that consist of a string as key, List<> as the value.


//Load your users into the lists...

private void PopulateRoom()
{
List<string> users = new List<string>;

for (int i = 0; i < 10; i++)
{
users.Add("User" + i.ToString());
}

rooms.Add("Room1", users); //add the room to your dictionary
}


//Now when you want to get your rooms out of the dictionary you can use

private void FindName(string roomid, string name)
{
List<string> users = rooms[roomid]; //I think this is how you get the item by default. Otherwise there's a GetValue method or something.. :)

foreach (string user in users) //loop through all users in the list
{
if (user == name)
{
return user;
}
}
}


Now keep in mind that storing a string key and list of strings is stupid. Your List<> should be of Users() -> a class that contains some info about the user iteself.
 
hmm thanks!
i think the best way would be as you said using a user class because im going to want to store a reference to the socket as well ...

Dictionary<string, new userClass()> rooms = new Dictionary<string, new userClass()>
something on the lines of that?
 
here is what i have come up with so far .. seems relatively functional .. problem is .. it seems to only be able to process a new message about every 2 seconds! .. perhaps i've done something wrong trying to pick all this together heh .. please advise .. thanks in advance!!!

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;
using N = System.Net;
using System.Collections;
using System.Text.RegularExpressions;


class SocketServer
{

public static channelManager chan = new channelManager();
public static System.Net.Sockets.TcpListener server;


static void Main(string[] args)
{

SocketServer server = new SocketServer();
}

public SocketServer()
{



server = new System.Net.Sockets.TcpListener(9001);

while (true)
{

System.Threading.Thread.Sleep(5);

server.Start();
if (server.Pending())
{
N.Sockets.TcpClient connection = server.AcceptTcpClient();
Console.WriteLine("new connection accepted");

listener newthread = new listener(connection);
Thread t = new Thread(new ThreadStart(newthread.listen));
t.Start();

}
}

}
}

class listener
{

System.Net.Sockets.TcpClient usersocket;
System.IO.StreamReader streamin;
System.IO.StreamWriter streamout;

public listener(System.Net.Sockets.TcpClient c)
{
usersocket = c;
Console.WriteLine("new thread");
streamin = new System.IO.StreamReader(usersocket.GetStream());
streamout = new System.IO.StreamWriter(usersocket.GetStream());
streamout.WriteLine("welcome .. you are connected");
streamout.Flush();

}


public void listen()
{

try
{
string datain = "";
string userid = "";
string channel = "";
int registered = 0;

while (true)
{

datain = streamin.ReadLine();
try {

if (datain.Length > 0)
{
Regex regex = new Regex("[^a-z0-9 -?|.:)(]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
datain = regex.Replace(datain, "");

string[] split = datain.Split('|');
Console.Write("x");

// incoming: function|userid|channel|name|msg
// testchan|user|clap



string function = split[0];


if (function == "REG")
{
registered = 1;
userid = split[1];
channel = split[2];

Console.WriteLine(userid + ": registered");
// add user
userManager newuserobj = new userManager(userid, usersocket);
Dictionary<string, userManager> newuserentry = new Dictionary<string, userManager>();
newuserentry.Add(userid, newuserobj);
SocketServer.chan.addUser(channel, newuserobj); // add user to channel listing

}
else if (function == "MSG")
{
userid = split[1];
channel = split[2];
string message = split[3];


SocketServer.chan.sendChannelMsg(channel, userid, message);

}
else if (function == "STAT")
{

SocketServer.chan.outputAll();

}
else
{
Console.WriteLine("function not found (" + datain + ") len: " + datain.Length);

}

Console.Write("o");

}
else
{
//Console.WriteLine("blankmsg");


}
//Console.WriteLine("(" + datain.Length+")"+datain);

System.Threading.Thread.Sleep(2000);
}
catch {

usersocket.Close();
if(registered == 1) {

SocketServer.chan.remUser(channel,userid);

}
usersocket.Close();
Console.WriteLine("client disconnected");
break;
}
}
}
catch (Exception e44) { Console.WriteLine(e44); }

}


}

class channelManager
{
Dictionary<string, Dictionary<string,userManager>> channels = new Dictionary<string, Dictionary<string, userManager>>(); // to hold all channels

public channelManager() {



}

public void addUser(string channel, userManager user)
{
// add user to channel


//check if channel exists
if (channels.ContainsKey(channel) == true)
{
// channel already exists, add user

channels[channel].Add(user.username,user);

}
else
{
// channel does not exist, create channel and add user

Dictionary<string, userManager> newuser = new Dictionary<string, userManager>();
newuser.Add(user.username, user);
channels.Add(channel, newuser);

}

}

public void remUser(string channel, string username)
{
// remove user from channel

channels[channel].Remove(username);

}
public void sendChannelMsg(string channel, string userid, string msg)
{

//check if channel exists
if (channels.ContainsKey(channel) == true)
{
// channel exists

//Console.WriteLine("(" + msg.Length + ")writing: chan: " + channel + " & user: " + userid);

Dictionary<string, userManager> userlist = channels[channel];
List<string> userkeys = new List<string>(userlist.Keys);

foreach (string key in userkeys)
{

userlist[key].userSendMsg(msg);

}

}
else
{
// channel does not exist

Console.WriteLine(userid + ": accessing non-existent channel");

}

}

public void outputAll() {

List<string> channellist = new List<string>(this.channels.Keys);

Console.WriteLine("in outputAll()");
Console.WriteLine("found "+channellist.Count+" channels");

foreach (string channel in channellist)
{

Dictionary<string, userManager> userlist = channels[channel];
Console.WriteLine("channel: " + channel + " (" + userlist.Count + ") users");

List<string> userkeys = new List<string>(userlist.Keys);
foreach(string key in userkeys)
{

Console.WriteLine(key);
}

}

}

}



class userManager
{

// class to manage individual user

public StreamWriter streamout; // stream for sending messages
public System.Net.Sockets.TcpClient client;
public string channel; // for current channel
public string username; // hold current username

//public user(System.Net.Sockets.TcpClient socket,username)
public userManager(string u,System.Net.Sockets.TcpClient conn)
{

// constructor: assign socket
//usersocket = socket;

username = u;
client = conn;
}

public void assignChannel(string assignchannel)
{



}

public void userSendMsg(string message) {

StreamWriter streamout = new StreamWriter(client.GetStream());
streamout.WriteLine("recvd");
streamout.Flush();

Console.WriteLine(message);
streamout.WriteLine(message);
streamout.Flush();

}

}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top