首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 软件教学-> 其他工具
c#实现socket传输简单数据_c#应用
作者:网友供稿 点击:0
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 

运行程序后, 先要点击开始接收按钮后才能点击发送数据

Form1.cs代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Net;

namespace WinSocket
...{
    public partial class Form1 : Form
    ...{
        BackgroundWorker bgWorker = null;
        public Form1()
        ...{
            InitializeComponent();
            bgWorker = new BackgroundWorker();
            bgWorker.WorkerSupportsCancellation = true;
            this.AddEvent();
        }

        /**//// <summary>
        /// 注册事件
        /// </summary>
        private void AddEvent()
        ...{
            this.bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            this.btnSend.Click += new EventHandler(btnSend_Click);
            this.btnStartReceive.Click += new EventHandler(btnStartReceive_Click);
            this.btnStopRecevie.Click += new EventHandler(btnStopRecevie_Click);
        }

        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        ...{
            Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint endpoint = new IPEndPoint(0, 8000);

            receiveSocket.Bind(endpoint);
            receiveSocket.Listen(10);

          
            try
            ...{
                while (true)
                ...{
                    Socket tmpSocket = receiveSocket.Accept();
                    byte[] buffer = new byte[tmpSocket.ReceiveBufferSize];
                    if (tmpSocket.Receive(buffer) > 0)
                    ...{
                        textBox2.Text += Encoding.UTF8.GetString(buffer) + Environment.NewLine;                     
                    }
                    else
                    ...{
                        System.Threading.Thread.Sleep(1000);
                    }

                }
            }
            catch(Exception err)
            ...{
                MessageBox.Show(err.Message);
            }
        }

        void btnStopRecevie_Click(object sender, EventArgs e)
        ...{
            if (this.bgWorker.IsBusy)
            ...{
                this.bgWorker.CancelAsync();
                this.btnStartReceive.Enabled = true;
                this.btnStopRecevie.Enabled = false;
            }          
        }

        void btnStartReceive_Click(object sender, EventArgs e)
        ...{
            this.btnStartReceive.Enabled = false;
            this.bgWorker.RunWorkerAsync();
            this.btnStopRecevie.Enabled = true;
        }

        void btnSend_Click(object sender, EventArgs e)
        ...{
            Socket sendSocket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sendSocket.Connect("187.186.0.63", 8000);

            byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text);           
           
            sendSocket.Send(buffer);
          
            sendSocket.Shutdown(SocketShutdown.Both);
            sendSocket.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        ...{
            this.btnStartReceive.Enabled = true;
            this.btnStopRecevie.Enabled = false;           
        }
    }
}
Form.Designer.cs代码如下

 

namespace WinSocket
...{
    partial class Form1
    ...{
        /**//// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /**//// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        ...{
            if (disposing && (components != null))
            ...{
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码

        /**//// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        ...{
            this.btnSend = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.btnStartReceive = new System.Windows.Forms.Button();
            this.btnStopRecevie = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // btnSend
            //
            this.btnSend.Location = new System.Drawing.Point(164, 35);
            this.btnSend.Name = "btnSend";
            this.btnSend.Size = new System.Drawing.Size(75, 23);
            this.btnSend.TabIndex = 0;
            this.btnSend.Text = "发送";
            this.btnSend.UseVisualStyleBackColor = true;
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(24, 37);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 1;
            //
            // textBox2
            //
            this.textBox2.Location = new System.Drawing.Point(24, 123);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(234, 131);
            this.textBox2.TabIndex = 2;
          
            //
            // btnStartReceive
            //
            this.btnStartReceive.Location = new System.Drawing.Point(24, 84);
            this.btnStartReceive.Name = "btnStartReceive";
            this.btnStartReceive.Size = new System.Drawing.Size(75, 23);
            this.btnStartReceive.TabIndex = 3;
            this.btnStartReceive.Text = "开始接收";
            this.btnStartReceive.UseVisualStyleBackColor = true;
            //
            // btnStopRecevie
            //
            this.btnStopRecevie.Location = new System.Drawing.Point(164, 84);
            this.btnStopRecevie.Name = "btnStopRecevie";
            this.btnStopRecevie.Size = new System.Drawing.Size(75, 23);
            this.btnStopRecevie.TabIndex = 4;
            this.btnStopRecevie.Text = "停止接收";
            this.btnStopRecevie.UseVisualStyleBackColor = true;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.btnStopRecevie);
            this.Controls.Add(this.btnStartReceive);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.btnSend);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btnSend;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Button btnStartReceive;
        private System.Windows.Forms.Button btnStopRecevie;
    }
}


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·用javascript调用webservice(webservice behavior)-ASP教程,ASP应用
·Java中对文件的操作-JSP教程,Java技巧及代码
·VB.net进阶:VB.net下的Sniffer-.NET教程,VB.Net语言
·如何利用VB.NET修改系统时间-.NET教程,VB.Net语言
·电源技术与电子变压器
·解析几种有效开关电源电磁干扰抑制
·教你完全卸载智能abc输入法
·电源系统中多个子系统之间电磁兼容
·ASP.net 2.0 自定义控件的开发之数据分页 第一章-.NET教程,Asp.Net开发
·VS2005新控件之GridView 使用高级技巧系列[二][视频]-.NET教程,评论及其它
最新文章
·3000ip以下网站该如何赚钱_网赚技巧
·谈谈我的下载站日ip5000 月收入3000的经验_站长心得
·photoshop绘制逼真的自然背景:山间瀑布_photoshop教程
·删掉系统文件 让windows vista少占用我们的硬盘_windows vista
·突出网页内重要内容保证adsense匹配准确_网赚技巧
·借台电脑办网站—访深圳外来工站长姚永卫_站长访谈
·创建优秀网站的6个好习惯_站长心得
·做一个网站 营造氛围有多重要?_站长心得
·如何让网民爱上你的网站?(3)_站长心得
·专家支招域名安全“三原则”有效规避域名掠夺_站长心得
相关主题
  • C#实现Singleton的两种方法的比较-.NET教程,C#语言
  • C#实现SMTP服务器,使用TCP命令实现,功能比较完善-.NET教程,C#语言
  • c#实现smtp服务器,使用tcp命令实现,功能比较完善
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭