欢迎来到飞鸟慕鱼博客,开始您的技术之旅!
当前位置: 首页知识笔记正文

Title: Asynchronous communication instance 2 (client)

终极管理员 知识笔记 32阅读

1 use the system; 2 use the system. Drawing; 3 use the system. Collection; 4 use the system. Component model; 5 use the system. Windows. Forms6 uses this system. Data; 7 use the system. Net; 8 use the system. Net. Sockets9 uses the system. Threading; 10 color 3360 rgba (0,0,0,1)' 11NamespaceClient 12 {13/*//Summary 14//Summary 16 Public Class Form 13360 System. Window. Form. Form

style="color: rgba(0, 128, 128, 1)"> 17    {
 18        private System.Windows.Forms.TextBox txt;
 19        private System.Windows.Forms.StatusBar statBar;
 20        private System.Windows.Forms.TextBox txtIP;
 21        private System.Windows.Forms.TextBox sendText;
 22        private System.Windows.Forms.Button btnSend;
 23        private System.Windows.Forms.Button btnStart;
 24        private System.Net.Sockets.Socket client;
 25        private byte[] buffer;
 26        /// <summary>
 27        /// 必需的设计器变量。
 28        /// </summary>

 29        private System.ComponentModel.Container components = null;
 30
 31        public Form1()
 32        {
 33            //
 34            // Windows 窗体设计器支持所必需的
 35            //
 36            InitializeComponent();
 37
 38            //
 39            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 40            //
 41        }

 42
 43        /// <summary>
 44        /// 清理所有正在使用的资源。
 45        /// </summary>

 46        protected override void Dispose( bool disposing )
 47        {
 48            if( disposing )
 49            {
 50                if (components != null
 51                {
 52                    components.Dispose();
 53                }

 54            }

 55            base.Dispose( disposing );
 56        }

 57
 58        Windows 窗体设计器生成的代码
141
142        /// <summary>
143        /// 应用程序的主入口点。
144        /// </summary>

145        [STAThread]
146        static void Main() 
147        {
148            Application.Run(new Form1());
149        }

150
151        private void Form1_Load(object sender, System.EventArgs e)
152        {
153            this.client = new Socket( AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp );
154            this.statBar.Text = "初始化完毕";
155            this.buffer = new byte1024 ];
156        }

157
158        private void btnStart_Click(object sender, System.EventArgs e)
159        {
160            IPEndPoint ipep = new IPEndPoint( IPAddress.Parse( this.txtIP.Text ),9000 );
161            this.client.BeginConnect( (EndPoint)ipep,new AsyncCallback(this.ConnectCallback ),this.client );
162        }

163
164        private void ConnectCallback( System.IAsyncResult iar )
165        {
166            try
167            {
168                Socket server = (Socket)iar.AsyncState;
169                this.client.EndConnect( iar );
170                this.txt.Text += "\r\n已连接至服务器。";
171                this.statBar.Text = "连接到服务器:" + server.RemoteEndPoint.ToString();
172                this.client.BeginReceive( this.buffer,0,this.buffer.Length,SocketFlags.None,new AsyncCallback( this.ReceiveCallback ),this.client );        
173
174            }

175            catch( Exception ex )
176            {
177                this.txt.Text += "\r\n" + ex.ToString();
178            }

179        }

180
181        private void ReceiveCallback( System.IAsyncResult iar )
182        {
183            try
184            {
185                Socket remote = (Socket)iar.AsyncState;
186                int recv = remote.EndReceive( iar );
187                string tmp = System.Text.Encoding.Default.GetString( this.buffer,0,recv );
188                this.txt.Text += "\r\n从服务器端接收到:" + tmp;
189            }

190            catch( Exception ex )
191            {
192            }

193        }

194
195        private void Send()
196        {
197            byte[] tmp = System.Text.Encoding.Default.GetBytes( this.sendText.Text );
198            this.client.BeginSend( tmp,0,tmp.Length,SocketFlags.None,new AsyncCallback(this.SendCallback),this.client );
199        }

200
201        private void SendCallback( System.IAsyncResult iar )
202        {
203            try
204            {
205                Socket remote = (Socket)iar.AsyncState;
206                int s = remote.EndSend( iar );
207                remote.BeginReceive( this.buffer,0,this.buffer.Length,SocketFlags.None,new AsyncCallback(this.ReceiveCallback),remote );
208            }

209            catch( Exception ex )
210            {
211                this.txt.Text += "\r\n" + ex.ToString();
212            }

213        }

214
215        private void btnSend_Click(object sender, System.EventArgs e)
216        {
217            this.Send();
218        }

219    }

220}
221
标签:
声明:无特别说明,转载请标明本文来源!