Async Client
Designer
namespace MyAsyncClient
{
partial class MyAsyncClientForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Button cmdSend;
private System.Windows.Forms.TextBox textBoxArrived;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Button buttonConnect;
private System.Windows.Forms.Button buttonClose;
private System.Windows.Forms.TextBox textBoxSend;
private System.Windows.Forms.TextBox txtIPAddr;
private System.ComponentModel.IContainer components = null;
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.buttonClose = new System.Windows.Forms.Button();
this.buttonConnect = new System.Windows.Forms.Button();
this.txtPort = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtIPAddr = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.textBoxSend = new System.Windows.Forms.TextBox();
this.cmdSend = new System.Windows.Forms.Button();
this.textBoxArrived = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.labelStatus = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox3.SuspendLayout();
this.groupBox4.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.buttonClose);
this.groupBox1.Controls.Add(this.buttonConnect);
this.groupBox1.Controls.Add(this.txtPort);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.txtIPAddr);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = “groupBox1″;
this.groupBox1.Size = new System.Drawing.Size(280, 77);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = “Settings”;
//
// buttonClose
//
this.buttonClose.Enabled = false;
this.buttonClose.Location = new System.Drawing.Point(160, 48);
this.buttonClose.Name = “buttonClose”;
this.buttonClose.Size = new System.Drawing.Size(96, 24);
this.buttonClose.TabIndex = 5;
this.buttonClose.Text = “Close”;
this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);
//
// buttonConnect
//
this.buttonConnect.Location = new System.Drawing.Point(160, 16);
this.buttonConnect.Name = “buttonConnect”;
this.buttonConnect.Size = new System.Drawing.Size(96, 24);
this.buttonConnect.TabIndex = 4;
this.buttonConnect.Text = “Connect”;
this.buttonConnect.Click += new System.EventHandler(this.cmdConnect_Click);
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(72, 48);
this.txtPort.Name = “txtPort”;
this.txtPort.Size = new System.Drawing.Size(40, 20);
this.txtPort.TabIndex = 3;
this.txtPort.Text = “8000″;
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 48);
this.label2.Name = “label2″;
this.label2.Size = new System.Drawing.Size(32, 16);
this.label2.TabIndex = 2;
this.label2.Text = “Port:”;
//
// txtIPAddr
//
this.txtIPAddr.Location = new System.Drawing.Point(72, 24);
this.txtIPAddr.Name = “txtIPAddr”;
this.txtIPAddr.Size = new System.Drawing.Size(80, 20);
this.txtIPAddr.TabIndex = 1;
this.txtIPAddr.Text = “127.0.0.1″;
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = “label1″;
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 0;
this.label1.Text = “Host IP:”;
//
// textBoxSend
//
this.textBoxSend.Location = new System.Drawing.Point(16, 104);
this.textBoxSend.Multiline = true;
this.textBoxSend.Name = “textBoxSend”;
this.textBoxSend.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBoxSend.Size = new System.Drawing.Size(264, 82);
this.textBoxSend.TabIndex = 1;
//
// cmdSend
//
this.cmdSend.Location = new System.Drawing.Point(19, 104);
this.cmdSend.Name = “cmdSend”;
this.cmdSend.Size = new System.Drawing.Size(237, 24);
this.cmdSend.TabIndex = 2;
this.cmdSend.Text = “Send”;
this.cmdSend.Click += new System.EventHandler(this.buttonSend_Click);
//
// textBoxArrived
//
this.textBoxArrived.Location = new System.Drawing.Point(16, 244);
this.textBoxArrived.Multiline = true;
this.textBoxArrived.Name = “textBoxArrived”;
this.textBoxArrived.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBoxArrived.Size = new System.Drawing.Size(264, 72);
this.textBoxArrived.TabIndex = 3;
//
// groupBox2
//
this.groupBox2.Location = new System.Drawing.Point(8, 228);
this.groupBox2.Name = “groupBox2″;
this.groupBox2.Size = new System.Drawing.Size(280, 96);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
this.groupBox2.Text = “Data Arrived”;
//
// groupBox3
//
this.groupBox3.Controls.Add(this.cmdSend);
this.groupBox3.Location = new System.Drawing.Point(8, 88);
this.groupBox3.Name = “groupBox3″;
this.groupBox3.Size = new System.Drawing.Size(280, 134);
this.groupBox3.TabIndex = 5;
this.groupBox3.TabStop = false;
this.groupBox3.Text = “Send Data”;
//
// groupBox4
//
this.groupBox4.Controls.Add(this.labelStatus);
this.groupBox4.Location = new System.Drawing.Point(8, 330);
this.groupBox4.Name = “groupBox4″;
this.groupBox4.Size = new System.Drawing.Size(280, 42);
this.groupBox4.TabIndex = 6;
this.groupBox4.TabStop = false;
this.groupBox4.Text = “Status”;
//
// labelStatus
//
this.labelStatus.AutoSize = true;
this.labelStatus.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.labelStatus.Location = new System.Drawing.Point(5, 16);
this.labelStatus.Name = “labelStatus”;
this.labelStatus.Size = new System.Drawing.Size(99, 13);
this.labelStatus.TabIndex = 0;
this.labelStatus.Text = “Status messages …”;
//
// MyAsyncClientForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(294, 382);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.textBoxArrived);
this.Controls.Add(this.textBoxSend);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox3);
this.Name = “MyAsyncClientForm”;
this.Text = “MyAsyncClient”;
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox4.ResumeLayout(false);
this.groupBox4.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
/// <summary>
///
/// </summary>
/// <param name=”disposing”></param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#endregion
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.Label labelStatus;
}
}
Form.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.Net;
using MyPacket;
using System.IO;
namespace MyAsyncClient
{
public partial class MyAsyncClientForm : Form
{
private delegate void CrossThreadCallsDelegate(string status);
private CrossThreadCallsDelegate crossThreadCall;
private CrossThreadCallsDelegate crossThreadCallForStatus;
public TcpClient tcpClient;
public MyAsyncClientForm()
{
InitializeComponent();
crossThreadCall = new CrossThreadCallsDelegate(WriteMessage);
crossThreadCallForStatus = new CrossThreadCallsDelegate(ReportStatus);
labelStatus.Text = “Client application started!”;
}
private void WriteMessage(string message)
{
textBoxArrived.Text += message + Environment.NewLine;
}
private void ReportStatus(string message)
{
labelStatus.Text = message + Environment.NewLine;
}
private void EnableCommands(bool abEnableConnect)
{
buttonClose.Enabled = !abEnableConnect;
buttonConnect.Enabled = abEnableConnect;
}
private void cmdConnect_Click(object sender, System.EventArgs e)
{
try
{
EnableCommands(true);
// get the remote IP address…
IPAddress ip = IPAddress.Parse(txtIPAddr.Text);
int iPortNo = int.Parse(txtPort.Text);
//create the end point
IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo);
//create the socket instance…
tcpClient = new TcpClient();
//connect to the remote host…
tcpClient.Connect(ipEnd);
labelStatus.Invoke(crossThreadCallForStatus, new object[] { “Client connected to ” + ipEnd });
EnableCommands(false);
//watch for data ( asynchronously )…
WaitForData();
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
EnableCommands(true);
}
}
public void WaitForData()
{
try
{
TcpSpecificPacket theSocPkt = new TcpSpecificPacket();
theSocPkt.thisClient = tcpClient;
// now start to listen for any data…
theSocPkt.thisClient.GetStream().BeginRead(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, OnDataReceived, theSocPkt);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
private void buttonSend_Click(object sender, System.EventArgs e)
{
try
{
byte[] byData = System.Text.Encoding.ASCII.GetBytes(textBoxSend.Text);
tcpClient.GetStream().Write(byData, 0, byData.Length);
textBoxSend.Text = “”;
labelStatus.Invoke(crossThreadCallForStatus, new object[] { “Sent ” + byData.Length + ” bytes of data.” });
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
private void buttonClose_Click(object sender, System.EventArgs e)
{
if (tcpClient != null && tcpClient.Connected)
{
tcpClient.Close();
tcpClient = null;
EnableCommands(true);
}
}
public void OnDataReceived(IAsyncResult asyn)
{
try
{
TcpSpecificPacket theSockId = (TcpSpecificPacket)asyn.AsyncState;
int dataReceivedLength = theSockId.thisClient.GetStream().EndRead(asyn);
if (dataReceivedLength != 0)
{
textBoxArrived.Invoke(crossThreadCall, new object[] { Encoding.UTF8.GetString(theSockId.dataBuffer, 0, dataReceivedLength) });
//crossThreadCall(Encoding.UTF8.GetString(theSockId.dataBuffer, 0, dataReceivedLength));
labelStatus.Invoke(crossThreadCallForStatus, new object[] { “Received ” + dataReceivedLength + ” bytes of data from server.” });
WaitForData();
}
} catch (IOException)
{
labelStatus.Invoke(crossThreadCallForStatus, new object[] { “Server has been closed!” });
}
catch (ObjectDisposedException)
{
labelStatus.Invoke(crossThreadCallForStatus, new object[] { “Socket has been closed!” });
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
}
}
Filed under: Samples