| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using Azylee.YeahWeb.SocketUtils.TcpUtils;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Test.TcpServerApp
- {
- public partial class MainForm : Form
- {
- TcppServer tcpServer = null;
- public MainForm()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void TBText_TextChanged(object sender, EventArgs e)
- {
- LBTextLength.Text = TBText.TextLength.ToString();
- }
- private void BTStart_Click(object sender, EventArgs e)
- {
- TBText.AppendText("正在监听:" + TBPort.Text + "\n");
- tcpServer = new TcppServer(int.Parse(TBPort.Text),
- ReceiveMessage, OnConnect, OnDisconnect);
- tcpServer?.Start();
- }
- private void BTStop_Click(object sender, EventArgs e)
- {
- tcpServer?.Stop();
- CBHost.Items.Clear();
- }
- private void BTSend_Click(object sender, EventArgs e)
- {
- try
- {
- string host = CBHost.SelectedItem.ToString();
- tcpServer.Write(host, new TcpDataModel()
- {
- Type = 1000,
- Data = Encoding.UTF8.GetBytes(TBMessage.Text)
- });
- }
- catch { }
- }
- #region Tcp 委托方法
- private void OnConnect(string host)
- {
- this.Invoke(new Action(() =>
- {
- TBText.AppendText($"Connect : {host}" + Environment.NewLine);
- CBHost.Items.Add(host);
- LBConnect.Text = tcpServer.ClientsCount().ToString();
- }));
- }
- private void OnDisconnect(string host)
- {
- this.Invoke(new Action(() =>
- {
- TBText.AppendText($"Disconnect : {host}" + Environment.NewLine);
- CBHost.Items.Remove(host);
- }));
- }
- private void ReceiveMessage(string host, TcpDataModel data)
- {
- this.Invoke(new Action(() =>
- {
- if (data.Type == 1000)
- {
- string s = Encoding.UTF8.GetString(data.Data);
- int l = s.Length;
- TBText.AppendText(host + " : " + s);
- TBText.AppendText(Environment.NewLine);
- }
- }));
- }
- #endregion
- private void TBMessage_TextChanged(object sender, EventArgs e)
- {
- LBMessageLength.Text = TBMessage.TextLength.ToString();
- }
- private void BTClear_Click(object sender, EventArgs e)
- {
- TBText.Clear();
- }
- }
- }
|