异步通信实例(二)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 byte[ 1024 ];
}
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();
}
}
}
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 byte[ 1024 ];
}
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();
}
}
}
- 异步通信实例(二)Client
- 异步通信实例(一)Server
- socket通信实例client端
- C#异步委托实例概述(二)
- asio异步网络通信实例
- Client-ServerRSA加解密通信方案-Client端(C#)(二)
- Android Binder 修炼之道(二)Client Server 实例
- Android异步通信机制(二)
- Socket通信实例二
- iOS Client 与WebSocket 通信(二)
- iOS Client 与WebSocket 通信(二)
- iOS Client 与WebSocket 通信(二)
- iOS Client 与WebSocket 通信(二)
- iOS Client 与WebSocket 通信(二)
- (学习Asp.net Ajax笔记二)异步通信层
- TCP通信(二)——异步连接
- Apache Mina 入门 (二)—— 异步通信机制
- Socket通信实例(二)——单线程实例
- 字符串操作
- 异步通信实例(一)Server
- poi(2)解析jar并写入excel
- textarea宽度固定,自动增高,input高度固定,自动增宽
- IOI2006 D2-3:A BLACK BOX GAME(带中文)
- 异步通信实例(二)Client
- IOI2006 D2-1:THE VALLEY OF MEXICO
- IOI2006 D2-2:JOINING POINTS
- IOI2006 D1-3:FORBIDDEN SUBGRAPH
- IOI2006 D1-1:DECIPHERING THE MAYAN WRITING
- IOI2006 D1-2:PYRAMID
- form表单中的enctype="multipart/form-data"
- 程序员每天该做的事
- IOI2006 D0-3:HIGHWAYS