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

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. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » Network » Socket Client 
28. 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());
   }
}
28. 5. Socket Client
28. 5. 1. Echo Client without message encoding
28. 5. 2. Echo Client with UTF8 Encoding
28. 5. 3. Socket connection
28. 5. 4. Send data using Socket
28. 5. 5. Simple Tcp Client: receive data from server
28. 5. 6. Simple Tcp Client: send data to the server
28. 5. 7. Simple Tcp server: send data to the client
28. 5. 8. GUI based Tcp Client
w_ww__.__java2s___.__c_o__m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.