public delegate void ShowData(string msg);//委托,防止跨线程访问控件,引起安全异常
public delegate void EnableButtonDelegate(Control control,bool enable);
private const int bufferSize = 8000;//缓存空间
private void EnableButton(Control control,bool enable)
{
if (control.InvokeRequired)
{
//使用委托
control.Invoke(new EnableButtonDelegate((ct, eb) => { ct.Enabled = eb; }),new object[]{control,enable});
}
else
{
control.Enabled = enable;
}
}
private TcpClient client;
private TcpListener server;
/// <summary>
/// 结构体 IP 和端口
/// </summary>
struct IpAndPort
{
public string ip;
public string port;
}

本文展示了如何在C#中实现TCP通信,包括创建TcpListener接收客户端连接,通过TcpClient发送和接收消息。文章详细解释了跨线程更新UI、连接管理以及异常处理的实现方式。
最低0.47元/天 解锁文章
5629

被折叠的 条评论
为什么被折叠?



