C# 聊天 服务器如何转发给其他客户端(最好能用集合!) 求~
C# 聊天 服务器怎么转发给其他客户端(最好能用集合!) 求~~~
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}
TcpClient newclient=null ;
Thread nth=null ;
Boolean star = true ;
Thread rd=null ;
NetworkStream ns = null;
BinaryReader nsr = null;
BinaryWriter nsw = null;
public static IPAddress localaddr = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(localaddr ,8888);
private void button1_Click(object sender, EventArgs e)
{
listener.Start();
textBox1.Text = "服务器启动";
nth = new Thread(new ThreadStart (clientconnect ));
nth.Start();
}
private void clientconnect()
{
int count=1;
while (star)
{
try
{
newclient = listener.AcceptTcpClient();
textBox1.Text = textBox1.Text+"有"+count +"个客户连接了!\n";
}
catch { }
rd = new Thread(new ThreadStart (receivedata ));
rd.Start();
count++;
}
}
private void receivedata()
{
ns = newclient.GetStream();
nsr = new BinaryReader(ns);
nsw = new BinaryWriter(ns);
star = true;
while (star)
{
string str = nsr.ReadString();
textBox1.Text = str;
}
nsr.Close();
nsw.Close();
ns.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(!ns = null;)ns.Close();
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}
TcpClient newclient=null ;
Thread nth=null ;
Boolean star = true ;
Thread rd=null ;
NetworkStream ns = null;
BinaryReader nsr = null;
BinaryWriter nsw = null;
public static IPAddress localaddr = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(localaddr ,8888);
private void button1_Click(object sender, EventArgs e)
{
listener.Start();
textBox1.Text = "服务器启动";
nth = new Thread(new ThreadStart (clientconnect ));
nth.Start();
}
private void clientconnect()
{
int count=1;
while (star)
{
try
{
newclient = listener.AcceptTcpClient();
textBox1.Text = textBox1.Text+"有"+count +"个客户连接了!\n";
}
catch { }
rd = new Thread(new ThreadStart (receivedata ));
rd.Start();
count++;
}
}
private void receivedata()
{
ns = newclient.GetStream();
nsr = new BinaryReader(ns);
nsw = new BinaryWriter(ns);
star = true;
while (star)
{
string str = nsr.ReadString();
textBox1.Text = str;
}
nsr.Close();
nsw.Close();
ns.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(!ns = null;)ns.Close();