关于C#多线程、网络编程与计时器Timer的一点使用心得
来源:互联网 发布:数据代码10054 编辑:程序博客网 时间:2024/05/19 18:14
初次使用C#进行网络编程,其中用到了多线程与计时器Timer类,以及网络通信相关类,在此总结一下。
功能:服务器启动后一直处于侦听状态,一旦有客户发送数据就显示在文本框中,期间可以对侦听进行停止和启动操作。
1、服务器部分界面与代码
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.Threading;using System.IO;namespace Servers{ public partial class FrmServer : Form { Thread listenThread;//侦听线程 StringBuilder msgs = new StringBuilder(); public FrmServer() { InitializeComponent(); } private void FrmServer_Load(object sender, EventArgs e) { listenThread = new Thread(IPScan); listenThread.IsBackground = true; } //开始侦听 private void btnStartListen_Click(object sender, EventArgs e) { timer1.Enabled = true; timer1.Start(); btnStartListen.Enabled = false; btnStopListen.Enabled = true; //如果需要多次挂起和启动线程,需要加以判断调用合适的方法 if (listenThread.ThreadState == ThreadState.Suspended || (listenThread.ThreadState == (ThreadState.SuspendRequested | ThreadState.Background))) { listenThread.Resume(); } else { listenThread.Start(); } } /// <summary> /// 侦听线程 /// </summary> void IPScan() { IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 }); TcpListener listener = new TcpListener(ip, 8500); listener.Start(); // 开始侦听 const int BufferSize = 8192; // 缓存大小,8192字节 byte[] buffer = new byte[BufferSize]; int bytesRead; string msg; TcpClient remoteClient; NetworkStream streamToClient; while (true)//连续侦听 { try { // 获取一个连接,中断方法 remoteClient = listener.AcceptTcpClient(); // 获得流,并写入buffer中 streamToClient = remoteClient.GetStream(); bytesRead = streamToClient.Read(buffer, 0, BufferSize); // 获得请求的字符串 msg = Encoding.Unicode.GetString(buffer, 0, bytesRead); msgs.AppendLine(msg); Array.Clear(buffer, 0, bytesRead); Thread.Sleep(1000); #region 反馈结果给客户 //可以在此进行其他相关处理 byte[] result = Encoding.Unicode.GetBytes("你好啊,兄弟!"); streamToClient.Write(result, 0, result.Length); #endregion } catch (IOException ioe) { ;//处理异常 } catch (Exception ex) { ;//处理异常 } } /*如果套接字是在子线程中创建的,那么在线程停止时(进入Stopped状态), * 应该同步停止侦听的连接,否则,下次使用同样的套接字侦听时会报错。*/ listener.Stop(); } private void timer1_Tick(object sender, EventArgs e) { textBox1.Text = msgs.ToString(); } //停止侦听 private void btnStopListen_Click(object sender, EventArgs e) { listenThread.Suspend(); timer1.Enabled = false; btnStartListen.Enabled = true; btnStopListen.Enabled = false; } }}
2、客户端部分界面与代码
客户端做的比较简陋,惭愧。
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.Threading;namespace CommonClients{ public partial class v : Form { public v() { InitializeComponent(); } private void btnSendData_Click(object sender, EventArgs e) { TcpClient client = new TcpClient(); try { client.Connect("127.0.0.1", 8500); // 与服务器连接 string msg = textBox1.Text; NetworkStream streamToServer = client.GetStream(); byte[] buffer = Encoding.Unicode.GetBytes(msg); // 获得缓存 streamToServer.Write(buffer, 0, buffer.Length); // 发往服务器 Thread.Sleep(3000); #region 从服务器接收反馈结果 byte[] receiveBuf=new byte[1024];//接收缓冲区 int bytesRead = streamToServer.Read(receiveBuf, 0, 1024); string msgRead = Encoding.Unicode.GetString(receiveBuf, 0, bytesRead); textBox2.Text = msgRead; #endregion } catch (Exception ex) { Console.WriteLine(ex.Message); return; } } }}
总结:有关多线程、网络编程的知识还有很多没有涉及,以后慢慢完善。
- 关于C#多线程、网络编程与计时器Timer的一点使用心得
- 关于破解网络验证的一点心得
- timer的使用--倒计时--计时器
- Java计时器Timer的使用!...
- C# Windows Service与Timer(计时器)
- 关于exe形式编程的一点心得
- C# VS 中计时器Timer的误区
- 关于布局管理器使用的一点心得
- 关于JFig的一点使用心得
- 关于fctix使用的一点心得
- 关于VMware软件的一点使用心得
- 关于使用JScrollPane的一点心得
- 关于SSI的一点使用心得
- 关于SSI的一点使用心得
- 关于UILabel的一点使用心得
- 关于UILabel的一点使用心得
- 关于UILabel的一点使用心得
- 关于UITableView的一点使用心得
- 文件下载
- Wayland面纱 X Window前世今生
- MYSQL数据库导入导出
- android 图表工具推荐(柱状图 饼图 曲线图 栈图 仪表图) 开源工具
- Java多线程编程--(8)学习Java5.0 并发编程包--线程池、Callable & Future 简介
- 关于C#多线程、网络编程与计时器Timer的一点使用心得
- 2012.8.9 言无信,行无果
- linux下获取系统时间的方法
- 为什么我的帐号登录后显示为“系统帐号”(Welcome System Account)
- MySQL分页查询
- android获取网络图片的用法
- hdu 1003
- OCP-047 CREATE VIEW 必须指定列别名
- Androidpn里的Xmpp的理解(消息推送)