TCP通信包使用代码示例

类别:.NET开发 点击:0 评论:0 推荐:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Jh.Sockets;
using System.IO;

namespace Jh.Test
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class JhSocketTest : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button btnServerStart;
  private System.Windows.Forms.Button btnServerStop;
  private System.Windows.Forms.Button btnClientConnect;
  private System.Windows.Forms.Button btnClientStop;
  private System.Windows.Forms.Button btnServerSend;
  private System.Windows.Forms.Button btnClientSend;
  private System.Windows.Forms.Button btnStopClient;
  private System.Windows.Forms.RichTextBox lbClient;
  private System.Windows.Forms.RichTextBox lbServer;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public JhSocketTest()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
   server = new JhTcpServer(6008);
   serverOb = new MyOb(this);
   server.AddOb(serverOb);
   clientOb = new MyClientOb(this);
   client = new  JhClient(clientOb);
   client.OnConnectFailed += new CommEventHandler(ConnectEvent);
   client.OnConnected += new CommEventHandler(ConnectEvent);
   client.OnClosed += new CommEventHandler(ConnectEvent);
   client.ConnectCheck = true;
  }
  MyClientOb clientOb;
  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {   
   if( disposing )
   {
    server.Exit();
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.btnServerStart = new System.Windows.Forms.Button();
   this.btnServerStop = new System.Windows.Forms.Button();
   this.btnClientConnect = new System.Windows.Forms.Button();
   this.btnClientStop = new System.Windows.Forms.Button();
   this.btnServerSend = new System.Windows.Forms.Button();
   this.btnClientSend = new System.Windows.Forms.Button();
   this.btnStopClient = new System.Windows.Forms.Button();
   this.lbClient = new System.Windows.Forms.RichTextBox();
   this.lbServer = new System.Windows.Forms.RichTextBox();
   this.SuspendLayout();
   //
   // btnServerStart
   //
   this.btnServerStart.Location = new System.Drawing.Point(11, 6);
   this.btnServerStart.Name = "btnServerStart";
   this.btnServerStart.Size = new System.Drawing.Size(116, 23);
   this.btnServerStart.TabIndex = 0;
   this.btnServerStart.Text = "Server Start";
   this.btnServerStart.Click += new System.EventHandler(this.btnServerStart_Click);
   //
   // btnServerStop
   //
   this.btnServerStop.Location = new System.Drawing.Point(135, 7);
   this.btnServerStop.Name = "btnServerStop";
   this.btnServerStop.Size = new System.Drawing.Size(134, 23);
   this.btnServerStop.TabIndex = 1;
   this.btnServerStop.Text = "Server Stop";
   this.btnServerStop.Click += new System.EventHandler(this.btnServerStop_Click);
   //
   // btnClientConnect
   //
   this.btnClientConnect.Location = new System.Drawing.Point(12, 36);
   this.btnClientConnect.Name = "btnClientConnect";
   this.btnClientConnect.Size = new System.Drawing.Size(112, 23);
   this.btnClientConnect.TabIndex = 2;
   this.btnClientConnect.Text = "Client Connect";
   this.btnClientConnect.Click += new System.EventHandler(this.btnClientConnect_Click);
   //
   // btnClientStop
   //
   this.btnClientStop.Location = new System.Drawing.Point(136, 37);
   this.btnClientStop.Name = "btnClientStop";
   this.btnClientStop.Size = new System.Drawing.Size(135, 23);
   this.btnClientStop.TabIndex = 3;
   this.btnClientStop.Text = "Client Stop";
   this.btnClientStop.Click += new System.EventHandler(this.btnClientStop_Click);
   //
   // btnServerSend
   //
   this.btnServerSend.Location = new System.Drawing.Point(284, 94);
   this.btnServerSend.Name = "btnServerSend";
   this.btnServerSend.Size = new System.Drawing.Size(170, 23);
   this.btnServerSend.TabIndex = 8;
   this.btnServerSend.Text = "Server Send";
   this.btnServerSend.Click += new System.EventHandler(this.btnServerSend_Click);
   //
   // btnClientSend
   //
   this.btnClientSend.Location = new System.Drawing.Point(278, 253);
   this.btnClientSend.Name = "btnClientSend";
   this.btnClientSend.Size = new System.Drawing.Size(184, 23);
   this.btnClientSend.TabIndex = 9;
   this.btnClientSend.Text = "Client Send";
   this.btnClientSend.Click += new System.EventHandler(this.btnClientSend_Click);
   //
   // btnStopClient
   //
   this.btnStopClient.Location = new System.Drawing.Point(273, 9);
   this.btnStopClient.Name = "btnStopClient";
   this.btnStopClient.Size = new System.Drawing.Size(134, 23);
   this.btnStopClient.TabIndex = 10;
   this.btnStopClient.Text = "Stop Client";
   this.btnStopClient.Click += new System.EventHandler(this.btnStopClient_Click);
   //
   // lbClient
   //
   this.lbClient.Location = new System.Drawing.Point(10, 128);
   this.lbClient.Name = "lbClient";
   this.lbClient.Size = new System.Drawing.Size(548, 75);
   this.lbClient.TabIndex = 11;
   this.lbClient.Text = "richTextBox1";
   //
   // lbServer
   //
   this.lbServer.Location = new System.Drawing.Point(11, 281);
   this.lbServer.Name = "lbServer";
   this.lbServer.Size = new System.Drawing.Size(546, 75);
   this.lbServer.TabIndex = 12;
   this.lbServer.Text = "richTextBox2";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(637, 424);
   this.Controls.Add(this.lbServer);
   this.Controls.Add(this.lbClient);
   this.Controls.Add(this.btnStopClient);
   this.Controls.Add(this.btnClientSend);
   this.Controls.Add(this.btnServerSend);
   this.Controls.Add(this.btnClientStop);
   this.Controls.Add(this.btnClientConnect);
   this.Controls.Add(this.btnServerStop);
   this.Controls.Add(this.btnServerStart);
   this.Name = "Form1";
   this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion


  JhTcpServer server = null;
  JhClient client = null;
  MyOb serverOb;
  internal class MyOb : Observer
  {
   public MyOb(JhSocketTest form)
   {
    wnd = form;
   }
   JhSocketTest wnd;
   public override bool OnDeal(CommData cd)
   {
    StreamReader r = new StreamReader(cd.Data,System.Text.Encoding.GetEncoding("GB18030"));//);//
    wnd.lbClient.Text = r.ReadToEnd();
    r.Close();
    return false;
   }
  }
  internal class MyClientOb : Observer
  {
   public MyClientOb(JhSocketTest form)
   {
    wnd = form;
   }
   JhSocketTest wnd;
   public override bool OnDeal(CommData cd)
   {
    StreamReader r = new StreamReader(cd.Data,System.Text.Encoding.GetEncoding("GB18030"));//);//
    wnd.lbServer.Text = r.ReadToEnd();
    r.Close();
    return false;
   }
  }
  private void btnServerStart_Click(object sender, System.EventArgs e)
  {
   server.Start();
  }

  private void btnServerStop_Click(object sender, System.EventArgs e)
  {
   server.Exit();
  }

  private void btnClientConnect_Click(object sender, System.EventArgs e)
  {
   client.Connect("localhost",6008);
  }

  void ConnectEvent(object sender,CommEventArgs e)
  {
   lbClient.Text = e.Message;
  }
  private void btnClientStop_Click(object sender, System.EventArgs e)
  {
   client.Close();
  }

  private void btnServerSend_Click(object sender, System.EventArgs e)
  {
   server.Send(lbClient.Text);
  }

  private void btnClientSend_Click(object sender, System.EventArgs e)
  {
   client.Send(lbServer.Text);
  }

  private void btnStopClient_Click(object sender, System.EventArgs e)
  {
   server.StopClient(0);
  }
 }
}

本文地址:http://com.8s8s.com/it/it41876.htm