异步通信实例(二)Client

来源:互联网 发布:韩顺平php全套笔记 编辑:程序博客网 时间:2024/05/17 04:46
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace TCLient
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.TextBox txt;
        
private System.Windows.Forms.StatusBar statBar;
        
private System.Windows.Forms.TextBox txtIP;
        
private System.Windows.Forms.TextBox sendText;
        
private System.Windows.Forms.Button btnSend;
        
private System.Windows.Forms.Button btnStart;
        
private System.Net.Sockets.Socket client;
        
private byte[] buffer;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


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

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


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


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


        
private void ConnectCallback( System.IAsyncResult iar )
        
{
            
try
            
{
                Socket server 
= (Socket)iar.AsyncState;
                
this.client.EndConnect( iar );
                
this.txt.Text += " 已连接至服务器。";
                
this.statBar.Text = "连接到服务器:" + server.RemoteEndPoint.ToString();
                
this.client.BeginReceive( this.buffer,0,this.buffer.Length,SocketFlags.None,new AsyncCallback( this.ReceiveCallback ),this.client );        

            }

            
catch( Exception ex )
            
{
                
this.txt.Text += " " + ex.ToString();
            }

        }


        
private void ReceiveCallback( System.IAsyncResult iar )
        
{
            
try
            
{
                Socket remote 
= (Socket)iar.AsyncState;
                
int recv = remote.EndReceive( iar );
                
string tmp = System.Text.Encoding.Default.GetString( this.buffer,0,recv );
                
this.txt.Text += " 从服务器端接收到:" + tmp;
            }

            
catch( Exception ex )
            
{
            }

        }


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


        
private void SendCallback( System.IAsyncResult iar )
        
{
            
try
            
{
                Socket remote 
= (Socket)iar.AsyncState;
                
int s = remote.EndSend( iar );
                remote.BeginReceive( 
this.buffer,0,this.buffer.Length,SocketFlags.None,new AsyncCallback(this.ReceiveCallback),remote );
            }

            
catch( Exception ex )
            
{
                
this.txt.Text += " " + ex.ToString();
            }

        }


        
private void btnSend_Click(object sender, System.EventArgs e)
        
{
            
this.Send();
        }

    }

}

 
原创粉丝点击