本文介绍了c#新手服务器发送多个客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是c#新手,我正在写服务器发送多个客户端。我能够发送到单个客户端,但不能够多个客户端。
服务器代码
i am c# novice and am writing server send multiple clients. i am able to send to single client however not able to multiple clients.
server code
namespace server2
{
public partial class server2 : Form
{
List<tcpclient> _clients = new List<tcpclient>();
_clients.Add(client1);
_clients.Add(client2);
public server2()
{
InitializeComponent();
}
private void server2_Load(object sender, EventArgs e)
{
byte[] bt;
bt = new byte[] { 127,0,0,1 };
IPAddress address = new IPAddress(bt);
byte[] bt;
bt = new byte[] { 127,0,1,1 };
IPAddress address = new IPAddress(bt);
}
private void send_Click(object sender, EventArgs e)
{
foreach (TcpClient client in _clients)
{
NetworkStream stream = client.GetStream();
BinaryWriter bwrite = new BinaryWriter(stream);
bwrite.Write(txtServer.Text);
}
}
private void add_Click(object sender, EventArgs e)
{
TcpClient client = new TcpClient();
string ipAddress = txtClientAddress.Text;
client.Connect(ipAddress, PORT);
_clients.Add(client);
txtClientAddress.Text = "";
}
}
}
[edit]Code block added - OriginalGriff[/edit]
推荐答案
这篇关于c#新手服务器发送多个客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!