Async Tcp Client : TCP Client « Network « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Network » TCP ClientScreenshots 
Async Tcp Client
Async Tcp Client

/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/

using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;


public class AsyncTcpClient : Form
{
   private TextBox newText;
   private TextBox conStatus;
   private ListBox results;
   private Socket client;
   private byte[] data = new byte[1024];
   private int size = 1024;

   public AsyncTcpClient()
   {
      Text = "Asynchronous TCP Client";
      Size = new Size(400380);
      
      Label label1 = new Label();
      label1.Parent = this;
      label1.Text = "Enter text string:";
      label1.AutoSize = true;
      label1.Location = new Point(1030);

      newText = new TextBox();
      newText.Parent = this;
      newText.Size = new Size(200* Font.Height);
      newText.Location = new Point(1055);

      results = new ListBox();
      results.Parent = this;
      results.Location = new Point(1085);
      results.Size = new Size(36018 * Font.Height);

      Label label2 = new Label();
      label2.Parent = this;
      label2.Text = "Connection Status:";
      label2.AutoSize = true;
      label2.Location = new Point(10330);

      conStatus = new TextBox();
      conStatus.Parent = this;
      conStatus.Text = "Disconnected";
      conStatus.Size = new Size(200* Font.Height);
      conStatus.Location = new Point(110325);

      Button sendit = new Button();
      sendit.Parent = this;
      sendit.Text = "Send";
      sendit.Location = new Point(220,52);
      sendit.Size = new Size(* Font.Height, * Font.Height);
      sendit.Click += new EventHandler(ButtonSendOnClick);

      Button connect = new Button();
      connect.Parent = this;
      connect.Text = "Connect";
      connect.Location = new Point(29520);
      connect.Size = new Size(* Font.Height, * Font.Height);
      connect.Click += new EventHandler(ButtonConnectOnClick);

      Button discon = new Button();
      discon.Parent = this;
      discon.Text = "Disconnect";
      discon.Location = new Point(295,52);
      discon.Size = new Size(* Font.Height, * Font.Height);
      discon.Click += new EventHandler(ButtonDisconOnClick);
   }

   void ButtonConnectOnClick(object obj, EventArgs ea)
   {
      conStatus.Text = "Connecting...";
      Socket newsock = new Socket(AddressFamily.InterNetwork,
                            SocketType.Stream, ProtocolType.Tcp);
      IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1")9050);
      newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);
   }

   void ButtonSendOnClick(object obj, EventArgs ea)
   {
      byte[] message = Encoding.ASCII.GetBytes(newText.Text);
      newText.Clear();
      client.BeginSend(message, 0, message.Length, SocketFlags.None,
                   new AsyncCallback(SendData), client);
   }

   void ButtonDisconOnClick(object obj, EventArgs ea)
   {
      client.Close();
      conStatus.Text = "Disconnected";
   }

   void Connected(IAsyncResult iar)
   {
      client = (Socket)iar.AsyncState;
      try
      {
         client.EndConnect(iar);
         conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
         client.BeginReceive(data, 0, size, SocketFlags.None,
                       new AsyncCallback(ReceiveData), client);
      catch (SocketException)
      {
         conStatus.Text = "Error connecting";
      }
   }

   void ReceiveData(IAsyncResult iar)
   {
      Socket remote = (Socket)iar.AsyncState;
      int recv = remote.EndReceive(iar);
      string stringData = Encoding.ASCII.GetString(data, 0, recv);
      results.Items.Add(stringData);
   }

   void SendData(IAsyncResult iar)
   {
      Socket remote = (Socket)iar.AsyncState;
      int sent = remote.EndSend(iar);
      remote.BeginReceive(data, 0, size, SocketFlags.None,
                    new AsyncCallback(ReceiveData), remote);
   }

   public static void Main()
   {
      Application.Run(new AsyncTcpClient());
   }
}
           
       
Related examples in the same category
1.Uses a TcpClient to handle HTTP
2.Network Stream Tcp Client
3.Simple Tcp Client
4.Var Tcp Client
5.Bad Tcp Client
6.Fixed Tcp Client
7.Stream Tcp Client
8.Employee Client
9.Network Order Client
10.Tcp Client Sample
11.Select Tcp Client
12.Picky Tcp Client
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.