1.加入一个很牛的包
2.创建一个项目命名为Client
代码如下:
using STTech.BytesIO.Tcp;
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 Client
{
public partial class MyClient : Form
{
TcpClient tcpClient;
public MyClient()
{
InitializeComponent();
}
//当点击连接的时候响应的事件
private void btnClient_Click(object sender, EventArgs e)
{
//创建一个TcpCLient
tcpClient = new TcpClient() {
Host = this.txtHost.Text,
Port = Convert.ToInt32(this.txtPort.Text)
};
//绑定一个连接成功的事件
tcpClient.OnConnectedSuccessfully += TcpClient_OnConnectedSuccessfully;
//绑定一个连接失败的事件
tcpClient.OnConnectionFailed += TcpClient_OnConnectionFailed;
//绑定一个断开连接的事件
tcpClient.OnDisconnected += TcpClient_OnDisconnected;
//绑定一个接收到消息的事件
tcpClient.OnDataReceived += TcpClient_OnDataReceived;
//创建连接
tcpClient.Connect();
}
//数据接受
private void TcpClient_OnDataReceived(object sender, STTech.BytesIO.Core.DataReceivedEventArgs e)
{
ShowMsg($"{e.Data.EncodeToString("utf-8")}");
}
//断开连接
private void TcpClient_OnDisconnected(object sender, STTech.BytesIO.Core.DisconnectedEventArgs e)
{
ShowMsg($"断开连接");
}
//连接失败
private void TcpClient_OnConnectionFailed(object sender, STTech.BytesIO.Core.ConnectionFailedEventArgs e)
{
ShowMsg($"连接失败");
}
//连接成功
private void TcpClient_OnConnectedSuccessfully(object sender, STTech.BytesIO.Core.ConnectedSuccessfullyEventArgs e)
{
ShowMsg($"连接成功");
}
//点击关闭连接
private void btnClose_Click(object sender, EventArgs e)
{
tcpClient.Disconnect();
}
//点击发送消息按钮
private void btnSend_Click(object sender, EventArgs e)
{
tcpClient.Send(this.txtSendMsg.Text.GetBytes("utf-8"));
ShowMsg($"客户端发送{this.txtSendMsg.Text}");
this.txtSendMsg.Clear();
}
//Log显示方法
void ShowMsg(string s)
{
if (this.txtShowMsg.InvokeRequired)
{
Invoke((EventHandler)delegate { this.txtShowMsg.Text += $"{DateTime.Now.ToString()} {s}\r\n"; });
}
else
{
this.txtShowMsg.Text += $"{DateTime.Now.ToString()} {s}\r\n";
}
}
}
}
3.创建一个Server项目
代码如下:
using STTech.BytesIO.Tcp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyBytesIO
{
public partial class Server : Form
{
TcpServer tcpServer;
public Server()
{
InitializeComponent();
}
private void TcpServer_Closed(object sender, EventArgs e)
{
ShowMsg($"IP地址为{tcpServer.Host.ToString()},端口号为{tcpServer.Port.ToString()}的服务器关闭监听");
}
private void TcpServer_Started(object sender, EventArgs e)
{
ShowMsg($"IP地址为{tcpServer.Host.ToString()},端口号为{tcpServer.Port.ToString()}的服务器开始监听");
}
private void btnStart_Click(object sender, EventArgs e)
{
tcpServer = new TcpServer()
{
Host = "192.168.1.7",
Port = Convert.ToInt32(this.textPort.Text)
};
tcpServer.Started += TcpServer_Started;
tcpServer.Closed += TcpServer_Closed;
tcpServer.ClientConnected += TcpServer_ClientConnected;
tcpServer.ClientDisconnected += TcpServer_ClientDisconnected;
tcpServer.StartAsync();
}
private void TcpServer_ClientDisconnected(object sender, STTech.BytesIO.Tcp.Entity.ClientDisconnectedEventArgs e)
{
ShowMsg($"客户端{e.Client.Host}:{e.Client.Port} 已断开连接");
}
private void TcpServer_ClientConnected(object sender, STTech.BytesIO.Tcp.Entity.ClientConnectedEventArgs e)
{
ShowMsg($"客户端{e.Client.Host}:{e.Client.Port} 已连接");
e.Client.OnDataReceived += Client_OnDataReceived;
}
private void Client_OnDataReceived(object sender, STTech.BytesIO.Core.DataReceivedEventArgs e)
{
TcpClient tcpClient=(TcpClient)sender;
ShowMsg($"来自{tcpClient.RemoteEndPoint.ToString()}的消息:{e.Data.EncodeToString("utf-8")}");
foreach (var item in tcpServer.Clients)
{
if (tcpClient !=item)
{
item.Send(($"来自{tcpClient.RemoteEndPoint.ToString()}的消息:{e.Data.EncodeToString("utf-8")}").GetBytes("utf-8"));
}
}
}
private void btnStop_Click(object sender, EventArgs e)
{
tcpServer.CloseAsync();
}
void ShowMsg(string s)
{
if (this.txtShowMsg.InvokeRequired)
{
Invoke((EventHandler)delegate { this.txtShowMsg.Text += $"{DateTime.Now.ToString()} {s}\r\n"; });
}
else
{
this.txtShowMsg.Text += $"{DateTime.Now.ToString()} {s}\r\n";
}
}
}
}
4.效果如下