-
C#教程之C#教程之C# Socket异步实现消息发送--附带源码(2)
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
.label3.Text = "正在监听"; // // groupBox1 // this.groupBox1.Controls.Add(this.btn_SendImg); this.groupBox1.Controls.Add(this.btn_SendShake); this.groupBox1.Controls.Add(this.btn_SendMes); this.groupBox1.Controls.Add(this.txt_Mes); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.dataGridView1); this.groupBox1.Location = new System.Drawing.Point(13, 68); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(681, 275); this.groupBox1.TabIndex = 8; this.groupBox1.TabStop = false; this.groupBox1.Text = "客户端连接列表"; // // btn_SendImg // this.btn_SendImg.Location = new System.Drawing.Point(466, 246); this.btn_SendImg.Name = "btn_SendImg"; this.btn_SendImg.Size = new System.Drawing.Size(75, 23); this.btn_SendImg.TabIndex = 11; this.btn_SendImg.Text = "发送图片"; this.btn_SendImg.UseVisualStyleBackColor = true; this.btn_SendImg.Click += new System.EventHandler(this.btn_SendImg_Click); // // btn_SendShake // this.btn_SendShake.Location = new System.Drawing.Point(380, 246); this.btn_SendShake.Name = "btn_SendShake"; this.btn_SendShake.Size = new System.Drawing.Size(75, 23); this.btn_SendShake.TabIndex = 10; this.btn_SendShake.Text = "发送震动"; this.btn_SendShake.UseVisualStyleBackColor = true; this.btn_SendShake.Click += new System.EventHandler(this.btn_SendShake_Click); // // btn_SendMes // this.btn_SendMes.Location = new System.Drawing.Point(294, 246); this.btn_SendMes.Name = "btn_SendMes"; this.btn_SendMes.Size = new System.Drawing.Size(75, 23); this.btn_SendMes.TabIndex = 9; this.btn_SendMes.Text = "发送"; this.btn_SendMes.UseVisualStyleBackColor = true; this.btn_SendMes.Click += new System.EventHandler(this.btn_SendMes_Click); // // txt_Mes // this.txt_Mes.Location = new System.Drawing.Point(294, 50); this.txt_Mes.Multiline = true; this.txt_Mes.Name = "txt_Mes"; this.txt_Mes.Size = new System.Drawing.Size(368, 190); this.txt_Mes.TabIndex = 2; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(292, 35); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(53, 12); this.label4.TabIndex = 1; this.label4.Text = "消息输入"; // // dataGridView1 // this.dataGridView1.AllowUserToOrderColumns = true; this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Column1, this.Column2}); this.dataGridView1.Location = new System.Drawing.Point(7, 35); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.RowTemplate.Height = 23; this.dataGridView1.Size = new System.Drawing.Size(249, 234); this.dataGridView1.TabIndex = 0; // // Column1 // this.Column1.HeaderText = "IP"; this.Column1.Name = "Column1"; // // Column2 // this.Column2.HeaderText = "Port"; this.Column2.Name = "Column2"; // // groupBox2 // this.groupBox2.Controls.Add(this.label7); this.groupBox2.Controls.Add(this.label6); this.groupBox2.Controls.Add(this.label5); this.groupBox2.Controls.Add(this.lab_ImgName); this.groupBox2.Controls.Add(this.pictureBox1); this.groupBox2.Controls.Add(this.listBox_Mes); this.groupBox2.Controls.Add(this.listBox_attribute); this.groupBox2.Location = new System.Drawing.Point(20, 349); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(898, 293); this.groupBox2.TabIndex = 9; this.groupBox2.TabStop = false; this.groupBox2.Text = "接收客户端消息"; // // lab_ImgName // this.lab_ImgName.AutoSize = true; this.lab_ImgName.Location = new System.Drawing.Point(443, 265); this.lab_ImgName.Name = "lab_ImgName"; this.lab_ImgName.Size = new System.Drawing.Size(0, 12); this.lab_ImgName.TabIndex = 12; // // pictureBox1 // this.pictureBox1.Location = new System.Drawing.Point(440, 37); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(208, 220); this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.pictureBox1.TabIndex = 3; this.pictureBox1.TabStop = false; // // listBox_Mes // this.listBox_Mes.FormattingEnabled = true; this.listBox_Mes.ItemHeight = 12; this.listBox_Mes.Location = new System.Drawing.Point(6, 37); this.listBox_Mes.Name = "listBox_Mes"; this.listBox_Mes.Size = new System.Drawing.Size(428, 220); this.listBox_Mes.TabIndex = 2; // // listBox_attribute // this.listBox_attribute.FormattingEnabled = true; this.listBox_attribute.ItemHeight = 12; this.listBox_attribute.Location = new System.Drawing.Point(654, 37); this.listBox_attribute.Name = "listBox_attribute"; this.listBox_attribute.Size = new System.Drawing.Size(202, 220); this.listBox_attribute.TabIndex = 1; // // openFileDialog1 // this.openFileDialog1.FileName = "openFileDialog1"; // // btn_Stop // this.btn_Stop.Location = new System.Drawing.Point(632, 8); this.btn_Stop.Name = "btn_Stop"; this.btn_Stop.Size = new System.Drawing.Size(75, 23); this.btn_Stop.TabIndex = 11; this.btn_Stop.Text = "关闭监听"; this.btn_Stop.UseVisualStyleBackColor = true; this.btn_Stop.Click += new System.EventHandler(this.btn_Stop_Click); // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(6, 17); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(77, 12); this.label5.TabIndex = 12; this.label5.Text = "接收消息显示"; // // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(438, 22); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(77, 12); this.label6.TabIndex = 13; this.label6.Text = "接收图片显示"; // // label7 // this.label7.AutoSize = true; this.label7.Location = new System.Drawing.Point(652, 22); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(53, 12); this.label7.TabIndex = 14; this.label7.Text = "数据显示"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(930, 645); this.Controls.Add(this.btn_Stop); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.Controls.Add(this.txt_Monitor); this.Controls.Add(this.label3); this.Controls.Add(this.btn_StartSocket); this.Controls.Add(this.txt_port); this.Controls.Add(this.label2); this.Controls.Add(this.txt_ip); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "服务端"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox txt_ip; private System.Windows.Forms.TextBox txt_port; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button btn_StartSocket; private System.Windows.Forms.TextBox txt_Monitor; private System.Windows.Forms.Label label3; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button btn_SendShake; private System.Windows.Forms.Button btn_SendMes; private System.Windows.Forms.TextBox txt_Mes; private System.Windows.Forms.Label label4; private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.ListBox listBox_attribute; private System.Windows.Forms.DataGridViewTextBoxColumn Column1; private System.Windows.Forms.DataGridViewTextBoxColumn Column2; private System.Windows.Forms.ListBox listBox_Mes; private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.Button btn_SendImg; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.Label lab_ImgName; private System.Windows.Forms.Button btn_Stop; private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label5; } }
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
.label3.Text = "正在监听"; // // groupBox1 // this.groupBox1.Controls.Add(this.btn_SendImg); this.groupBox1.Controls.Add(this.btn_SendShake); this.groupBox1.Controls.Add(this.btn_SendMes); this.groupBox1.Controls.Add(this.txt_Mes); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.dataGridView1); this.groupBox1.Location = new System.Drawing.Point(13, 68); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(681, 275); this.groupBox1.TabIndex = 8; this.groupBox1.TabStop = false; this.groupBox1.Text = "客户端连接列表"; // // btn_SendImg // this.btn_SendImg.Location = new System.Drawing.Point(466, 246); this.btn_SendImg.Name = "btn_SendImg"; this.btn_SendImg.Size = new System.Drawing.Size(75, 23); this.btn_SendImg.TabIndex = 11; this.btn_SendImg.Text = "发送图片"; this.btn_SendImg.UseVisualStyleBackColor = true; this.btn_SendImg.Click += new System.EventHandler(this.btn_SendImg_Click); // // btn_SendShake // this.btn_SendShake.Location = new System.Drawing.Point(380, 246); this.btn_SendShake.Name = "btn_SendShake"; this.btn_SendShake.Size = new System.Drawing.Size(75, 23); this.btn_SendShake.TabIndex = 10; this.btn_SendShake.Text = "发送震动"; this.btn_SendShake.UseVisualStyleBackColor = true; this.btn_SendShake.Click += new System.EventHandler(this.btn_SendShake_Click); // // btn_SendMes // this.btn_SendMes.Location = new System.Drawing.Point(294, 246); this.btn_SendMes.Name = "btn_SendMes"; this.btn_SendMes.Size = new System.Drawing.Size(75, 23); this.btn_SendMes.TabIndex = 9; this.btn_SendMes.Text = "发送"; this.btn_SendMes.UseVisualStyleBackColor = true; this.btn_SendMes.Click += new System.EventHandler(this.btn_SendMes_Click); // // txt_Mes // this.txt_Mes.Location = new System.Drawing.Point(294, 50); this.txt_Mes.Multiline = true; this.txt_Mes.Name = "txt_Mes"; this.txt_Mes.Size = new System.Drawing.Size(368, 190); this.txt_Mes.TabIndex = 2; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(292, 35); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(53, 12); this.label4.TabIndex = 1; this.label4.Text = "消息输入"; // // dataGridView1 // this.dataGridView1.AllowUserToOrderColumns = true; this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Column1, this.Column2}); this.dataGridView1.Location = new System.Drawing.Point(7, 35); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.RowTemplate.Height = 23; this.dataGridView1.Size = new System.Drawing.Size(249, 234); this.dataGridView1.TabIndex = 0; // // Column1 // this.Column1.HeaderText = "IP"; this.Column1.Name = "Column1"; // // Column2 // this.Column2.HeaderText = "Port"; this.Column2.Name = "Column2"; // // groupBox2 // this.groupBox2.Controls.Add(this.label7); this.groupBox2.Controls.Add(this.label6); this.groupBox2.Controls.Add(this.label5); this.groupBox2.Controls.Add(this.lab_ImgName); this.groupBox2.Controls.Add(this.pictureBox1); this.groupBox2.Controls.Add(this.listBox_Mes); this.groupBox2.Controls.Add(this.listBox_attribute); this.groupBox2.Location = new System.Drawing.Point(20, 349); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(898, 293); this.groupBox2.TabIndex = 9; this.groupBox2.TabStop = false; this.groupBox2.Text = "接收客户端消息"; // // lab_ImgName // this.lab_ImgName.AutoSize = true; this.lab_ImgName.Location = new System.Drawing.Point(443, 265); this.lab_ImgName.Name = "lab_ImgName"; this.lab_ImgName.Size = new System.Drawing.Size(0, 12); this.lab_ImgName.TabIndex = 12; // // pictureBox1 // this.pictureBox1.Location = new System.Drawing.Point(440, 37); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(208, 220); this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.pictureBox1.TabIndex = 3; this.pictureBox1.TabStop = false; // // listBox_Mes // this.listBox_Mes.FormattingEnabled = true; this.listBox_Mes.ItemHeight = 12; this.listBox_Mes.Location = new System.Drawing.Point(6, 37); this.listBox_Mes.Name = "listBox_Mes"; this.listBox_Mes.Size = new System.Drawing.Size(428, 220); this.listBox_Mes.TabIndex = 2; // // listBox_attribute // this.listBox_attribute.FormattingEnabled = true; this.listBox_attribute.ItemHeight = 12; this.listBox_attribute.Location = new System.Drawing.Point(654, 37); this.listBox_attribute.Name = "listBox_attribute"; this.listBox_attribute.Size = new System.Drawing.Size(202, 220); this.listBox_attribute.TabIndex = 1; // // openFileDialog1 // this.openFileDialog1.FileName = "openFileDialog1"; // // btn_Stop // this.btn_Stop.Location = new System.Drawing.Point(632, 8); this.btn_Stop.Name = "btn_Stop"; this.btn_Stop.Size = new System.Drawing.Size(75, 23); this.btn_Stop.TabIndex = 11; this.btn_Stop.Text = "关闭监听"; this.btn_Stop.UseVisualStyleBackColor = true; this.btn_Stop.Click += new System.EventHandler(this.btn_Stop_Click); // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(6, 17); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(77, 12); this.label5.TabIndex = 12; this.label5.Text = "接收消息显示"; // // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(438, 22); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(77, 12); this.label6.TabIndex = 13; this.label6.Text = "接收图片显示"; // // label7 // this.label7.AutoSize = true; this.label7.Location = new System.Drawing.Point(652, 22); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(53, 12); this.label7.TabIndex = 14; this.label7.Text = "数据显示"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(930, 645); this.Controls.Add(this.btn_Stop); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.Controls.Add(this.txt_Monitor); this.Controls.Add(this.label3); this.Controls.Add(this.btn_StartSocket); this.Controls.Add(this.txt_port); this.Controls.Add(this.label2); this.Controls.Add(this.txt_ip); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "服务端"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox txt_ip; private System.Windows.Forms.TextBox txt_port; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button btn_StartSocket; private System.Windows.Forms.TextBox txt_Monitor; private System.Windows.Forms.Label label3; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button btn_SendShake; private System.Windows.Forms.Button btn_SendMes; private System.Windows.Forms.TextBox txt_Mes; private System.Windows.Forms.Label label4; private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.ListBox listBox_attribute; private System.Windows.Forms.DataGridViewTextBoxColumn Column1; private System.Windows.Forms.DataGridViewTextBoxColumn Column2; private System.Windows.Forms.ListBox listBox_Mes; private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.Button btn_SendImg; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.Label lab_ImgName; private System.Windows.Forms.Button btn_Stop; private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label5; } }
运行代码:
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; using System.Net.Sockets; using System.Net; using System.Threading; using System.Drawing.Imaging; using System.IO; using System.Collections; using System.Xml; namespace SocketService { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region 参数声明 /// <summary> /// 字典 IP加客户端状态 /// </summary> static Dictionary<string, ClientState> dic_ip = new Dictionary<string, ClientState>(); /// <summary> /// 服务端启动的Socket /// </summary> Socket s_socket; #endregion /// <summary> /// 开启监听 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_StartSocket_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(this.txt_ip.Text.Trim()) && string.IsNullOrWhiteSpace(this.txt_port.Text.Trim()) && !string.IsNullOrWhiteSpace(this.txt_Monitor.Text.Trim())) { MessageBox.Show("输入不符合或已在监听"); return; } //获取IP PORT异步连接Socket string ip = this.txt_ip.Text.Trim(); int port = int.Parse(this.txt_port.Text.Trim()); s_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { s_socket.Bind(new IPEndPoint(IPAddress.Parse(ip), port)); s_socket.Listen(1);//同时连接的最大连接数 s_socket.BeginAccept(new AsyncCallback(Accept), s_socket);//获取连接 this.txt_Monitor.Text = ip + ":" + port; this.txt_ip.Enabled = false; this.txt_port.Enabled = false; } catch (Exception ex) { Console.WriteLine("error ex=" + ex.Message + " " + ex.StackTrace); } } /// <summary> /// 异步连接回调 获取请求Socket 添加信息到控件 /// </summary> /// <param name="ar"></param> private void Accept(IAsyncResult ar) { try { //获取连接Socket 创建新的连接 Socket myServer = ar.AsyncState as Socket; Socket service = myServer.EndAccept(ar); #region 内部逻辑 UI处理部分 ClientState obj = new ClientState(); obj.clientSocket = service; //添加到字典 dic_ip.Add(service.RemoteEndPoint.ToString(), obj); var point = service.RemoteEndPoint.ToString().Split(':'); this.BeginInvoke((MethodInvoker)delegate () { //获取IP 端口添加到控件 int index = this.dataGridView1.Rows.Add(); dataGridView1.Rows[index].Cells[0].Value = point[0]; dataGridView1.Rows[index].Cells[1].Value = point[1]; }); #endregion //接收连接Socket数据 service.BeginReceive(obj.buffer, 0, ClientState.bufsize, SocketFlags.None, new AsyncCallback(ReadCallback), obj); myServer.BeginAccept(new AsyncCallback(Accept), myServer);//等待下一个连接 } catch (Exception ex) { Console.WriteLine("服务端关闭"+ex.Message+" "+ex.StackTrace); } } /// <summary> /// 发送消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_SendMes_Click(object sender, EventArgs e) {
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能