具体点就是我不很不理解hashtable的具体内容,所以也就不会用了下面是我自己写的东东,写到这里就不会了,
public partial class Form1 : Form
{
Hashtable onLineUsers;
TcpListener tcpListener;
TcpClient tcpClient;
Database db;
public Form1()
{
InitializeComponent();
tcpListener = new TcpListener(2000);
onLineUsers = new Hashtable();
}
private void button3_Click(object sender, EventArgs e)
{
Thread listenThread = new Thread(new ThreadStart(startListen));
listenThread.Start();
}
private void startListen()
{
tcpListener.Start();
while (true)
{
tcpClient = tcpListener.AcceptTcpClient();
Thread serviceThread = new Thread(new ParameterizedThreadStart(service));
serviceThread.Start(tcpClient);
//service(tcpClient);
}
}
private void service(object client)
{
//针对client,对它进行服务(为client提供所有
//它应该具有的能力)
TcpClient tcpc=client as TcpClient;
NetworkStream ns = tcpc.GetStream();
while (true)
{
byte[] mss = new byte[1024];
string ms="";
ns.Read(mss, 0, tcpc.Available);
ms += Encoding.UTF8.GetString(mss);
string[] messages = ms.Split(new char[] { '^' });
switch (messages[0])//chat^zjl^huzhuxi^asdfasd
{
case "chat":
NetworkStream nsClient=(NetworkStream)onLineUsers[messages[2]];
byte[] sendms=Encoding.UTF8.GetBytes(messages[1]+"说:"+messages[3]);
nsClient.Write(sendms, 0, sendms.Length);
break;//在这里执行转发
case "login":
onLineUsers.Add(messages[1], ns);
break;//在这里执行登陆
case "register":
bool exist;
DataTable dt;
dt = new DataTable();
db=new Database();
dt=db.sel("select * from user where username='" +messages[1].ToString()+ "'");
if (dt.Rows.Count > 0)
exist = true;
else
{
db.not_sel("insert into user values('"+messages[1].ToString()+"','"+messages[2].ToString()+"','"+messages[3].ToString() +"','"+messages[4].ToString()+"')");
exist = false;
}
byte[] sendms1 = Encoding.UTF8.GetBytes(exist.ToString());
NetworkStream nets= tcpClient.GetStream();
nets.Write(sendms1,0,sendms1.Length);
break;
case"sxym":
IDictionaryEnumerator myEnumerator = onLineUsers.GetEnumerator();
while (myEnumerator.MoveNext())
{
就是在这里我想把hashtable表里面存放的在线用户的姓名都取出来然后发送给客户端通过刷性在线用户按钮来显示在线的用户
}