You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
6.6 KiB
C#
213 lines
6.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Xml;
|
|
|
|
namespace SocketExample
|
|
{
|
|
/// <summary>
|
|
/// MultiClientsWindow.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class MultiClientsWindow : Window
|
|
{
|
|
|
|
public class PanelItem:INotifyPropertyChanged //实现组件更改的接口
|
|
{
|
|
IPAddress ip;
|
|
int port;
|
|
TcpClient client;
|
|
NetworkStream stream;
|
|
Thread thread;
|
|
public string Text { get; set; }
|
|
public string IPtext { get; set; }
|
|
public string Porttext { get; set; }
|
|
public string MessageText { get; set; }
|
|
|
|
private string _infotext;
|
|
public string Infotext { get => _infotext; set
|
|
{
|
|
_infotext = value;
|
|
OnPropertyChanged(); // 通知 UI 更新
|
|
}
|
|
}
|
|
|
|
public LinkCommand linkcomn;
|
|
|
|
public LinkCommand sendcomn;
|
|
|
|
public LinkCommand disconnectcomn;
|
|
|
|
public LinkCommand clearcomn;
|
|
|
|
|
|
public PanelItem(int index)
|
|
{
|
|
Text = $"客户端{index + 1}";
|
|
linkcomn = new LinkCommand(Button_Link);//绑定按钮的链接事件
|
|
sendcomn = new LinkCommand(Button_Click_Send);
|
|
disconnectcomn = new LinkCommand(Button_disconnect);
|
|
clearcomn = new LinkCommand(Button_clear);
|
|
|
|
MessageText = "请输入需要发送的内容:";
|
|
IPtext = "127.0.0.1";//默认IP
|
|
Porttext = "8999";//默认端口
|
|
}
|
|
|
|
public LinkCommand _linkcomn { get { return linkcomn; } }
|
|
|
|
public LinkCommand _sendcomn { get { return sendcomn; } }
|
|
|
|
public LinkCommand _disconnectcomn { get { return disconnectcomn; } }
|
|
|
|
public LinkCommand _clearcomn { get { return clearcomn; } }
|
|
|
|
void Button_Link()//链接按钮的事件
|
|
{
|
|
try
|
|
{
|
|
ip = IPAddress.Parse(IPtext);
|
|
port = int.Parse(Porttext);
|
|
client = new TcpClient();
|
|
|
|
// 开始监听
|
|
client.Connect(ip, port);
|
|
|
|
thread = new Thread(Recive);
|
|
thread.IsBackground = true;
|
|
thread.Start();
|
|
Infotext += "已连接!\n";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Infotext += $"处理请求时出错: {ex.Message}\n";
|
|
}
|
|
}
|
|
|
|
void Button_Click_Send()//发送按钮的事件
|
|
{
|
|
try
|
|
{
|
|
string message = MessageText;
|
|
// 如果发送"exit"则退出
|
|
byte[] data = Encoding.UTF8.GetBytes(message);
|
|
stream.Write(data, 0, data.Length);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Infotext += $"处理请求时出错: {ex.Message}\n";
|
|
}
|
|
}
|
|
|
|
void Button_disconnect()
|
|
{
|
|
stream.Close();
|
|
client.Close();
|
|
thread.Abort();
|
|
Infotext += "已断开链接\n";
|
|
}
|
|
|
|
void Button_clear()
|
|
{
|
|
Infotext = string.Empty;
|
|
}
|
|
|
|
void Recive()//接收消息线程
|
|
{
|
|
stream = client.GetStream();
|
|
while (true)
|
|
{
|
|
// 处理客户端请求
|
|
try
|
|
{
|
|
// 接收响应
|
|
byte[] buffer = new byte[1024];
|
|
int bytesRead = stream.Read(buffer, 0, buffer.Length);
|
|
if (bytesRead == 0)
|
|
{
|
|
Infotext += $"服务器断开连接\n";
|
|
break;
|
|
}
|
|
string response = Encoding.UTF8.GetString(buffer, 0, bytesRead);
|
|
Infotext += $"服务器响应: {response}\n";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Infotext += $"处理请求时出错: {ex.Message}\n";
|
|
}
|
|
}
|
|
stream.Close();
|
|
client.Close();
|
|
// 关闭连接
|
|
}
|
|
|
|
//组件属性更改事件
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
}
|
|
public MultiClientsWindow()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
int currentcount = 0;
|
|
|
|
List<PanelItem> items = new List<PanelItem>();
|
|
|
|
private void GenerateButton_Click(object sender, RoutedEventArgs e)//添加客户端框体
|
|
{
|
|
if (int.TryParse(CountTextBox.Text, out int count) && count > 0)
|
|
{
|
|
//var items = new List<PanelItem>();
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
items.Add(new PanelItem(i + currentcount));
|
|
}
|
|
currentcount += count;
|
|
PanelContainer.ItemsSource = null;//刷新
|
|
PanelContainer.ItemsSource = items;
|
|
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("请输入有效的正整数", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
public class LinkCommand : ICommand //command方法实现
|
|
{
|
|
private Action _excute;
|
|
public LinkCommand(Action action) {
|
|
_excute = action;
|
|
}
|
|
public event EventHandler CanExecuteChanged;
|
|
|
|
public bool CanExecute(object parameter)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public void Execute(object parameter)
|
|
{
|
|
_excute();
|
|
}
|
|
}
|
|
|
|
private void ClearButton_Click(object sender, RoutedEventArgs e)//清空客户端
|
|
{
|
|
items.Clear();
|
|
PanelContainer.ItemsSource = null;
|
|
currentcount = 0;
|
|
}
|
|
}
|
|
}
|