GUI based Tcp Client : Socket Client « Network « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Network » Socket Client 
33.5.8.GUI based Tcp Client
/*
Quote from 

C# Network Programming
# Paperback: 656 pages
# Publisher: Sybex (November 26, 2002)
# Language: English
# ISBN-10: 0782141765
# ISBN-13: 978-0782141764
*/
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());
   }
}
33.5.Socket Client
33.5.1.Echo Client without message encoding
33.5.2.Echo Client with UTF8 Encoding
33.5.3.Socket connection
33.5.4.Send data using Socket
33.5.5.Simple Tcp Client: receive data from server
33.5.6.Simple Tcp Client: send data to the server
33.5.7.Simple Tcp server: send data to the client
33.5.8.GUI based 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.