using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.IO;using System.Threading;namespace TCPServer{ public partial class frmSyncTcpServer : Form { #region 变量 // 申明变量 private TcpListener tcpLister = null; private TcpClient tcpClient = null; //给定网络服务器地址 IPAddress ipaddress=IPAddress.Parse("192.168.2.123"); //给定串口号 private const int Port = 8087; private NetworkStream networkStream = null; private BinaryReader TCPreader; private BinaryWriter TCPwriter; // 申明委托 // 显示消息 private delegate void ShowMessage(string str); private ShowMessage showMessageCallback; // 显示状态 private delegate void ShowStatus(string str); private ShowStatus showStatusCallBack; // 清空消息 private delegate void ResetMessage(); private ResetMessage resetMessageCallBack; #endregion public frmSyncTcpServer() { InitializeComponent(); #region 实例化委托 // 显示消息 showMessageCallback = new ShowMessage(showMessage); // 显示状态 showStatusCallBack = new ShowStatus(showStatus); // 重置消息 resetMessageCallBack = new ResetMessage(resetMessage); #endregion //在页面默认显示的IP地址 //ipaddress = IPAddress.Loopback;"192.168.2.104"; tbxserverIp.Text = ipaddress.ToString(); tbxPort.Text = Port.ToString(); } #region 定义回调函数 // 显示消息 private void showMessage(string str) { lstbxMessageView.Items.Add(tcpClient.Client.RemoteEndPoint); lstbxMessageView.Items.Add(str); lstbxMessageView.TopIndex = lstbxMessageView.Items.Count - 1; } // 显示状态 private void showStatus(string str) { toolStripStatusInfo.Text = str; } // 清空消息 private void resetMessage() { tbxMessage.Text = string.Empty; tbxMessage.Focus(); } #endregion #region Click事件 // 开始监听 private void btnStart_Click(object sender, EventArgs e) { //连接服务器IP,端口 //tcpLister = new TcpListener(ipaddress, Port); tcpLister = new TcpListener(IPAddress.Parse("192.168.2.123"),8087); //线程启动 tcpLister.Start(); // 启动一个线程来接受请求 Thread acceptThread =new Thread(acceptClientConnect); acceptThread.Start(); } // 线程里面的动作---接受请求 private void acceptClientConnect() { statusStripInfo.Invoke(showStatusCallBack,"正在监听"); Thread.Sleep(500); try { statusStripInfo.Invoke(showStatusCallBack,"等待连接"); tcpClient = tcpLister.AcceptTcpClient(); //有监听的客户端 if (tcpLister!= null) { statusStripInfo.Invoke(showStatusCallBack,"接受到连接"); networkStream = tcpClient.GetStream(); TCPreader = new BinaryReader(networkStream); TCPwriter = new BinaryWriter(networkStream); try { string receivemessage = TCPreader.ReadString(); //list显示接收到的信息 lstbxMessageView.Invoke(showMessageCallback, receivemessage); } catch { if (TCPreader != null) { TCPreader.Close(); } if (TCPwriter != null) { TCPwriter.Close(); } if (tcpClient != null) { tcpClient.Close(); } } } } catch { statusStripInfo.Invoke(showStatusCallBack, "停止监听"); Thread.Sleep(500); //没有接收到客户端时候的状态 statusStripInfo.Invoke(showStatusCallBack, "就绪"); } } // 关闭监听 private void btnStop_Click(object sender, EventArgs e) { tcpLister.Stop(); } // 清空消息 private void btnClear_Click(object sender, EventArgs e) { lstbxMessageView.Items.Clear(); } // 接受消息 private void btnReceive_Click(object sender, EventArgs e) { statusStripInfo.Invoke(showStatusCallBack, "接受消息中"); try { string receivemessage = TCPreader.ReadString(); //list显示接收到的信息 lstbxMessageView.Invoke(showMessageCallback, receivemessage); } catch { if (TCPreader != null) { TCPreader.Close(); } if (TCPwriter != null) { TCPwriter.Close(); } if (tcpClient != null) { tcpClient.Close(); } statusStripInfo.Invoke(showStatusCallBack, "断开了连接"); // 重新开启一个线程等待新的连接 Thread acceptThread = new Thread(acceptClientConnect); acceptThread.Start(); } } // 发送消息 private void btnSend_Click(object sender, EventArgs e) { Thread sendThread = new Thread(SendMessage); sendThread.Start(tbxMessage.Text); } // 发送消息 private void SendMessage(object state) { statusStripInfo.Invoke(showStatusCallBack, "正在发送"); try { TCPwriter.Write(state.ToString()); Thread.Sleep(2000); TCPwriter.Flush(); statusStripInfo.Invoke(showStatusCallBack, "完毕"); tbxMessage.Invoke(resetMessageCallBack, null); lstbxMessageView.Invoke(showMessageCallback, state.ToString()); } catch { if (TCPreader != null) { TCPreader.Close(); } if (TCPwriter != null) { TCPwriter.Close(); } if (tcpClient != null) { tcpClient.Close(); } statusStripInfo.Invoke(showStatusCallBack, "断开了连接"); // 重新开启一个线程等待新的连接 Thread acceptThread = new Thread(acceptClientConnect); acceptThread.Start(); } } private void button1_Click(object sender, EventArgs e) { if (TCPreader != null) { TCPreader.Close(); } if (TCPwriter != null) { TCPwriter.Close(); } if (tcpClient != null) { // 断开连接 tcpClient.Close(); } toolStripStatusInfo.Text = "断开连接"; // 启动一个线程等待接受新的请求 Thread acceptThread = new Thread(acceptClientConnect); acceptThread.Start(); } #endregion private void tbxMessage_TextChanged(object sender, EventArgs e) { } private void frmSyncTcpServer_Load(object sender, EventArgs e) { } }}
下载CSharpTCP编程,客户端服务器通信用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论