对话框选择文件
private void buttonSelect_Click(object sender, EventArgs e)
{
OpenFileDialog openD = new OpenFileDialog();
openD.InitialDirectory = @"F:\";
openD.Title = @"请选择要发送的文件。";
openD.Filter = "所有文件|*.*|音乐文件|*.wav;*.mp3";
openD.ShowDialog();
textSelect.Text = openD.FileName;
}
ComboBox数据添加删除
comboClient.Items.Add(socketRecive.RemoteEndPoint.ToString());
comboClient.SelectedIndex = 0;
if (dicClient.Count != 0)
{
for (int i = 0; i < dicClient.Count; i++)
{
string strip = comboClient.GetItemText(comboClient.Items[i]);
dicClient[strip].Close();
dicClient.Remove(strip);
comboClient.Items.RemoveAt(i);
}
}
定时器检测客户端是否断开连接
var timerClientCheck = new System.Timers.Timer(1000);
timerClientCheck.Elapsed += new ElapsedEventHandler(ClientCheck);
timerClientCheck.Start();
private void ClientCheck(object o, ElapsedEventArgs e)
{
int count = comboClient.Items.Count;
if(count > 0)
{
List<int> listIndex = new List<int>();
for (int i = 0; i < count; i++)
{
try
{
string strip = comboClient.GetItemText(comboClient.Items[i]);
if (dicClient[strip].Poll(1000, SelectMode.SelectRead))
{
dicClient[strip].Close();
listIndex.Add(i);
dicClient.Remove(strip);
comboClient.Items.RemoveAt(i);
textReceive.AppendText(strip + "=>已下线");
continue;
}
}
catch
{
}
}
}
}