[C#] 纯文本查看 复制代码
namespace SocketHelperDemo
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
//掩耳盗铃线程控制UI控件
CheckForIllegalCrossThreadCalls = false;
//创建委托接收数据
DelegateHelper.SocketReceive =SocketMessage;
}
/// <summary>
/// 创建TCPClient实例
/// </summary>
private TCPClient _tcpClient;
/// <summary>
/// 初始化并连接TCPClient
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnConClick(object sender, EventArgs e)
{
_tcpClient = new TCPClient(TxtIp.Text, int.Parse(TxtPort.Text));
_tcpClient.StartConnection();
}
/// <summary>
/// 接收Socket数据
/// </summary>
/// <param name="msg"></param>
private void SocketMessage(string msg)
{
//自定义处理接收的数据
MsgInfomationList.Items.Add(msg);
}
/// <summary>
/// 发送Socket数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnSendClick(object sender, EventArgs e)
{
_tcpClient.SendCommand(TxtSendMsg.Text);
}
/// <summary>
/// 退出时关闭所有线程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
{
System.Environment.Exit(0);
}
}
}