GUI based Tcp Server : Socket Server « 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 Server 
33.6.6.GUI based Tcp Server
/*
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 AsyncTcpSrvr : Form
{
   private TextBox conStatus;
   private ListBox results;
   private byte[] data = new byte[1024];
   private int size = 1024;
   private Socket server;


   public AsyncTcpSrvr()
   {
      Text = "Asynchronous TCP Server";
      Size = new Size(400380);

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

      Label label1 = new Label();
      label1.Parent = this;
      label1.Text = "Text received from client:";
      label1.AutoSize = true;
      label1.Location = new Point(1045);

      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 = "Waiting for client...";
      conStatus.Size = new Size(200* Font.Height);
      conStatus.Location = new Point(110325);

      Button stopServer = new Button();
      stopServer.Parent = this;
      stopServer.Text = "Stop Server";
      stopServer.Location = new Point(260,32);
      stopServer.Size = new Size(* Font.Height, * Font.Height);
      stopServer.Click += new EventHandler(ButtonStopOnClick);

      server = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
      IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
      server.Bind(iep);
      server.Listen(5);
      server.BeginAccept(new AsyncCallback(AcceptConn), server);
   }

   void ButtonStopOnClick(object obj, EventArgs ea)
   {
      Close();
   }

   void AcceptConn(IAsyncResult iar)
   {
      Socket oldserver = (Socket)iar.AsyncState;
      Socket client = oldserver.EndAccept(iar);
      conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
      string stringData = "Welcome to my server";
      byte[] message1 = Encoding.ASCII.GetBytes(stringData);
      client.BeginSend(message1, 0, message1.Length, SocketFlags.None,
                  new AsyncCallback(SendData), client);
   }

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

   void ReceiveData(IAsyncResult iar)
   {
      Socket client = (Socket)iar.AsyncState;
      int recv = client.EndReceive(iar);
      if (recv == 0)
      {
         client.Close();
         conStatus.Text = "Waiting for client...";
         server.BeginAccept(new AsyncCallback(AcceptConn), server);
         return;
      }
      string receivedData = Encoding.ASCII.GetString(data, 0, recv);
      results.Items.Add(receivedData);
      byte[] message2 = Encoding.ASCII.GetBytes(receivedData);
      client.BeginSend(message2, 0, message2.Length, SocketFlags.None,
                   new AsyncCallback(SendData), client);
   }

   public static void Main()
   {
      Application.Run(new AsyncTcpSrvr());
   }
}
33.6.Socket Server
33.6.1.Listen for Socket Request in Thread
33.6.2.Use background thread to deal with the Server socket
33.6.3.Accepting a socket connection (simple file-server)
33.6.4.Simple Tcp server: receive data from a client
33.6.5.Tcp server: use StreamWriter and StreamReader to read and write to a client
33.6.6.GUI based Tcp Server
33.6.7.Tcp server based on Thread
33.6.8.ThreadPool based Tcp server
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.