VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#教程之C# Socket异步实现消息发送--附带源码(4)

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

.label2 = new System.Windows.Forms.Label(); this.txt_ip = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.btn_SendImg = new System.Windows.Forms.Button(); this.btn_SendShake = new System.Windows.Forms.Button(); this.btn_SendMes = new System.Windows.Forms.Button(); this.label4 = new System.Windows.Forms.Label(); this.txt_mes = new System.Windows.Forms.TextBox(); this.listBox1 = new System.Windows.Forms.ListBox(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.lab_ImgName = new System.Windows.Forms.Label(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // // btn_StopSocket // this.btn_StopSocket.Location = new System.Drawing.Point(635, 10); this.btn_StopSocket.Name = "btn_StopSocket"; this.btn_StopSocket.Size = new System.Drawing.Size(75, 23); this.btn_StopSocket.TabIndex = 15; this.btn_StopSocket.Text = "取消连接"; this.btn_StopSocket.UseVisualStyleBackColor = true; this.btn_StopSocket.Click += new System.EventHandler(this.btn_StopSocket_Click); // // txt_Monitor // this.txt_Monitor.Location = new System.Drawing.Point(457, 14); this.txt_Monitor.Name = "txt_Monitor"; this.txt_Monitor.ReadOnly = true; this.txt_Monitor.Size = new System.Drawing.Size(144, 21); this.txt_Monitor.TabIndex = 14; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(398, 17); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(53, 12); this.label3.TabIndex = 13; this.label3.Text = "正在连接"; // // btn_StartSocket // this.btn_StartSocket.Location = new System.Drawing.Point(307, 12); this.btn_StartSocket.Name = "btn_StartSocket"; this.btn_StartSocket.Size = new System.Drawing.Size(75, 23); this.btn_StartSocket.TabIndex = 12; this.btn_StartSocket.Text = "开始连接"; this.btn_StartSocket.UseVisualStyleBackColor = true; this.btn_StartSocket.Click += new System.EventHandler(this.btn_StartSocket_Click); // // txt_port // this.txt_port.Location = new System.Drawing.Point(183, 14); this.txt_port.Name = "txt_port"; this.txt_port.Size = new System.Drawing.Size(100, 21); this.txt_port.TabIndex = 11; this.txt_port.Text = "9999"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(148, 19); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(29, 12); this.label2.TabIndex = 10; this.label2.Text = "端口"; // // txt_ip // this.txt_ip.Location = new System.Drawing.Point(36, 14); this.txt_ip.Name = "txt_ip"; this.txt_ip.Size = new System.Drawing.Size(100, 21); this.txt_ip.TabIndex = 9; this.txt_ip.Text = "127.0.0.1"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(13, 17); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(17, 12); this.label1.TabIndex = 8; this.label1.Text = "IP"; // // groupBox1 // this.groupBox1.Controls.Add(this.lab_ImgName); this.groupBox1.Controls.Add(this.pictureBox1); 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.label4); this.groupBox1.Controls.Add(this.txt_mes); this.groupBox1.Controls.Add(this.listBox1); this.groupBox1.Location = new System.Drawing.Point(15, 48); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(939, 324); this.groupBox1.TabIndex = 16; this.groupBox1.TabStop = false; this.groupBox1.Text = "发送与接收"; // // pictureBox1 // this.pictureBox1.Location = new System.Drawing.Point(346, 21); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(281, 280); this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.pictureBox1.TabIndex = 20; this.pictureBox1.TabStop = false; // // btn_SendImg // this.btn_SendImg.Location = new System.Drawing.Point(795, 267); this.btn_SendImg.Name = "btn_SendImg"; this.btn_SendImg.Size = new System.Drawing.Size(75, 23); this.btn_SendImg.TabIndex = 19; 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(714, 267); this.btn_SendShake.Name = "btn_SendShake"; this.btn_SendShake.Size = new System.Drawing.Size(75, 23); this.btn_SendShake.TabIndex = 18; 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(633, 267); this.btn_SendMes.Name = "btn_SendMes"; this.btn_SendMes.Size = new System.Drawing.Size(75, 23); this.btn_SendMes.TabIndex = 17; this.btn_SendMes.Text = "消息发送"; this.btn_SendMes.UseVisualStyleBackColor = true; this.btn_SendMes.Click += new System.EventHandler(this.btn_SendMes_Click); // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(633, 6); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(53, 12); this.label4.TabIndex = 17; this.label4.Text = "消息输入"; // // txt_mes // this.txt_mes.Location = new System.Drawing.Point(633, 21); this.txt_mes.Multiline = true; this.txt_mes.Name = "txt_mes"; this.txt_mes.Size = new System.Drawing.Size(298, 240); this.txt_mes.TabIndex = 1; // // listBox1 // this.listBox1.FormattingEnabled = true; this.listBox1.ItemHeight = 12; this.listBox1.Location = new System.Drawing.Point(7, 21); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(332, 280); this.listBox1.TabIndex = 0; // // openFileDialog1 // this.openFileDialog1.FileName = "openFileDialog1"; // // lab_ImgName // this.lab_ImgName.AutoSize = true; this.lab_ImgName.Location = new System.Drawing.Point(351, 307); this.lab_ImgName.Name = "lab_ImgName"; this.lab_ImgName.Size = new System.Drawing.Size(0, 12); this.lab_ImgName.TabIndex = 21; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(961, 386); this.Controls.Add(this.groupBox1); this.Controls.Add(this.btn_StopSocket); 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.pictureBox1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button btn_StopSocket; private System.Windows.Forms.TextBox txt_Monitor; private System.Windows.Forms.Label label3; private System.Windows.Forms.Button btn_StartSocket; private System.Windows.Forms.TextBox txt_port; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txt_ip; private System.Windows.Forms.Label label1; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button btn_SendShake; private System.Windows.Forms.Button btn_SendMes; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox txt_mes; private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.Button btn_SendImg; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.Label lab_ImgName; } }
复制代码

运行代码:

复制代码
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.IO;
using System.Collections;
using System.Drawing.Imaging;
using System.Xml;

namespace SocketClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region 参数声明
        /// <summary>
        /// 根据IP:Port 存储值

        /// </summary>
        static Dictionary<string, ServiceState> dic_ip = new Dictionary<string, ServiceState>();
        /// <summary>
        /// 客户端socket
        /// </summary>
        Socket c_socket;

        Thread th_socket;

        int ConnectionResult = -2;
        #endregion

        /// <summary>
        /// 开启Scoket连接
        /// </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;
            }
            try
            {
                this.txt_ip.Enabled = false;
                this.txt_port.Enabled = false;
                Start();
                th_socket = new Thread(MonitorSocker);
                th_socket.IsBackground = true;
                th_socket.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " " + ex.StackTrace);
                this.txt_ip.Enabled = true;
                this.txt_port.Enabled = true;
            }
        }

        //监听Socket
        void MonitorSocker()
        {
            while (true)
            {
                if (ConnectionResult != 0 && ConnectionResult != -2)//通过错误码判断
                {
                    Start();
                    this.BeginInvoke((MethodInvoker)delegate ()
                    {
                        this.label3.Text = "重连中..";
                        this.txt_Monitor.Text = "errorCode" + ConnectionResult.ToString();
                    });
                    dic_ip = new Dictionary<string, ServiceState>();
                }
                Thread.Sleep(1000);
            }
        }

        /// <summary>
        /// 关闭Socket连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_StopSocket_Click(object sender, EventArgs e)
        {
            Stop();
        }

        /// <summary>
        /// 发送文本消息

        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SendMes_Click(object sender, EventArgs e)
        {
            try
            {
                SendSocket send = new SocketClient.SendSocket()
                {
                    Message = this.txt_mes.Text.Trim(),
                };
                Send(dic_ip[this.txt_Monitor.Text.Trim()].serviceSocket, send.ToArray());
                this.txt_mes.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " " + ex.StackTrace);
            }
        }

        /// <summary>
        /// 发送震动

        /// </summary>
        private void btn_SendShake_Click(object sender, EventArgs e)
        {
            SendSocket send = new SocketClient.SendSocket()
            {
                SendShake = true,
                Message = "震动",
            };
            Send(dic_ip[this.txt_Monitor.Text.Trim()].serviceSocket, send.ToArray());
        }

        /// <summary>
        /// 发送图片数据

        /// </summary>
        /// <param name="sender"></param>
      



  
相关教程