在组合框中列出的每个客户端上发送一条自动消息
Send an automatic message on each client listed in the comboBox
我有一个客户端服务器程序。客户端连接到服务器后,客户端的 IP 地址将存储到组合框中。如果我想向客户端发送消息(使用按钮),我只需要 select 某个 IP 地址并发送我的消息。那只是为了测试。
我的主要目的是,一旦连接到服务器,我想向 comboBox 内的每个客户端发送相同的消息(自动)。如何让服务器向那些客户端发送消息?还检查是否有人新连接,然后自动发送相同的消息。
这是自动发送相同消息的地方。
public void sendToClient()
{
try
{
for(int i = 0; i < myComboBox.Items.Count; i++)
{
//string value = comboBox1.GetItemText(comboBox1.Items[i]);
sendData(openedFile.ToString());
}
}
catch (Exception ex)
{
output.Text += "Error.....\n " + ex.StackTrace;
}
}
然后是 sendData() 函数
private void sendData(String data)
{
IPAddress ipep = IPAddress.Parse(comboBox1.SelectedItem.ToString());
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipept = new IPEndPoint(ipep, hostPort);
NetworkStream nStream = tcpClient.GetStream();
ASCIIEncoding asciidata = new ASCIIEncoding();
byte[] buffer = asciidata.GetBytes(data);
if (nStream.CanWrite)
{
nStream.Write(buffer, 0, buffer.Length);
nStream.Flush();
}
}
我猜你可能会这样做:
1) 使用不同的线程调用 SendData。您将获得更高的性能和更少的延迟(见下文)。
2) 我相信您可能会检测到将 IP 添加到组合框中以进行上述调用的确切位置 - 这比始终在组合框中检查整个 IP 更有效。例如:
' At some point of MODULE or top of form
dim ListOfIPS as List(of String)
' At new connection event
ListOFIPs.Add(IPNumber)
Dim MyThread As Thread = New Thread(CType(Sub() SendData(IPNumber, MyCustomMessage), ThreadStart))
MyThread.SetApartmentState(Threading.ApartmentState.MTA)
MyThread.Start()
3) 而且,如果你真的需要向所有连接的人发送消息,你可以尝试类似的方法:
for each Guy as String in ListOfIPS
Dim MyThread As Thread = New Thread(CType(Sub() SendData(Guy, MyCustomMessage), ThreadStart))
MyThread.IsBackground = False
MyThread.SetApartmentState(Threading.ApartmentState.MTA)
MyThread.Start()
next
4) 如果您看到 LIST(of T) 帮助,您会发现如何在断开连接时定位和删除(或将其设为空白字符串)一些 IP...
我有一个客户端服务器程序。客户端连接到服务器后,客户端的 IP 地址将存储到组合框中。如果我想向客户端发送消息(使用按钮),我只需要 select 某个 IP 地址并发送我的消息。那只是为了测试。
我的主要目的是,一旦连接到服务器,我想向 comboBox 内的每个客户端发送相同的消息(自动)。如何让服务器向那些客户端发送消息?还检查是否有人新连接,然后自动发送相同的消息。
这是自动发送相同消息的地方。
public void sendToClient()
{
try
{
for(int i = 0; i < myComboBox.Items.Count; i++)
{
//string value = comboBox1.GetItemText(comboBox1.Items[i]);
sendData(openedFile.ToString());
}
}
catch (Exception ex)
{
output.Text += "Error.....\n " + ex.StackTrace;
}
}
然后是 sendData() 函数
private void sendData(String data)
{
IPAddress ipep = IPAddress.Parse(comboBox1.SelectedItem.ToString());
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipept = new IPEndPoint(ipep, hostPort);
NetworkStream nStream = tcpClient.GetStream();
ASCIIEncoding asciidata = new ASCIIEncoding();
byte[] buffer = asciidata.GetBytes(data);
if (nStream.CanWrite)
{
nStream.Write(buffer, 0, buffer.Length);
nStream.Flush();
}
}
我猜你可能会这样做:
1) 使用不同的线程调用 SendData。您将获得更高的性能和更少的延迟(见下文)。
2) 我相信您可能会检测到将 IP 添加到组合框中以进行上述调用的确切位置 - 这比始终在组合框中检查整个 IP 更有效。例如:
' At some point of MODULE or top of form
dim ListOfIPS as List(of String)
' At new connection event
ListOFIPs.Add(IPNumber)
Dim MyThread As Thread = New Thread(CType(Sub() SendData(IPNumber, MyCustomMessage), ThreadStart))
MyThread.SetApartmentState(Threading.ApartmentState.MTA)
MyThread.Start()
3) 而且,如果你真的需要向所有连接的人发送消息,你可以尝试类似的方法:
for each Guy as String in ListOfIPS
Dim MyThread As Thread = New Thread(CType(Sub() SendData(Guy, MyCustomMessage), ThreadStart))
MyThread.IsBackground = False
MyThread.SetApartmentState(Threading.ApartmentState.MTA)
MyThread.Start()
next
4) 如果您看到 LIST(of T) 帮助,您会发现如何在断开连接时定位和删除(或将其设为空白字符串)一些 IP...