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.
200 lines
6.4 KiB
C#
200 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Net;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Xml;
|
|
using TouchSocket.Core;
|
|
using TouchSocket.Sockets;
|
|
|
|
namespace SocketExample
|
|
{
|
|
/// <summary>
|
|
/// MultiClientsWindow.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class TouchSocketWindow : Window
|
|
{
|
|
|
|
public class PanelItem:INotifyPropertyChanged //实现组件更改的接口
|
|
{
|
|
IPAddress ip;
|
|
int port;
|
|
TcpClient client;
|
|
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 = string.Empty;
|
|
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; } }
|
|
private Task onRecieved(ITcpClient client,ReceivedDataEventArgs e)
|
|
{
|
|
string time = DateTime.Now.ToString();
|
|
string currentTime = time.Substring(10, 5);
|
|
var mes = e.ByteBlock.Span.ToString(Encoding.UTF8);
|
|
Infotext += $"({currentTime})客户端接收到信息:\n{mes}\n\n";
|
|
return EasyTask.CompletedTask;
|
|
}
|
|
|
|
async void Button_Link()//链接按钮的事件
|
|
{
|
|
try
|
|
{
|
|
ip = IPAddress.Parse(IPtext);
|
|
port = int.Parse(Porttext);
|
|
client = new TcpClient();
|
|
client.Connected = (client, e) => { Infotext += "已连接!\n"; return EasyTask.CompletedTask; };
|
|
client.Closed = (client, e) => { Infotext += "已断开!\n"; return EasyTask.CompletedTask; };
|
|
client.Received += onRecieved;
|
|
|
|
await client.SetupAsync(new TouchSocketConfig().SetRemoteIPHost($"{IPtext}:{Porttext}").ConfigureContainer(a =>
|
|
{
|
|
a.AddConsoleLogger();//添加一个日志注入
|
|
}));
|
|
|
|
await client.ConnectAsync();
|
|
|
|
// 开始监听
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Infotext += $"处理请求时出错: {ex.Message}\n";
|
|
}
|
|
}
|
|
|
|
void Button_Click_Send()//发送按钮的事件
|
|
{
|
|
try
|
|
{
|
|
string message = MessageText;
|
|
if (message == string.Empty)
|
|
{
|
|
MessageBox.Show("输入内容不能为空!");
|
|
}
|
|
else
|
|
{
|
|
byte[] data = Encoding.UTF8.GetBytes(message);
|
|
client.Send(data);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Infotext += $"处理请求时出错: {ex.Message}\n";
|
|
}
|
|
}
|
|
|
|
void Button_disconnect()
|
|
{
|
|
client.Close();
|
|
}
|
|
|
|
void Button_clear()
|
|
{
|
|
Infotext = string.Empty;
|
|
}
|
|
|
|
//组件属性更改事件
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
}
|
|
public TouchSocketWindow()
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|