新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → [转帖]TCP通信包使用代码示例 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 4538 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [转帖]TCP通信包使用代码示例 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     Qr 帅哥哟,离线,有人找我吗?
      
      
      威望:9
      等级:博士二年级(版主)
      文章:4392
      积分:29981
      门派:XML.ORG.CN
      注册:2004/5/15

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给Qr发送一个短消息 把Qr加入好友 查看Qr的个人资料 搜索Qr在『 Dot NET,C#,ASP,VB 』的所有贴子 访问Qr的主页 引用回复这个贴子 回复这个贴子 查看Qr的博客楼主
    发贴心情 [转帖]TCP通信包使用代码示例

    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);
    }
    }
    }

    来源不详


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    没人帮忙,那就靠自己,自己才是最好的老师!本人拒绝回答通过站内短消息提出的问题!

    blog:http://Qr.blogger.org.cn

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/2/3 21:54:00
     
     xmzhy 帅哥哟,离线,有人找我吗?狮子座1978-8-18
      
      
      威望:5
      等级:计算机学士学位
      文章:385
      积分:2003
      门派:XML.ORG.CN
      注册:2003/10/22

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给xmzhy发送一个短消息 把xmzhy加入好友 查看xmzhy的个人资料 搜索xmzhy在『 Dot NET,C#,ASP,VB 』的所有贴子 引用回复这个贴子 回复这个贴子 查看xmzhy的博客2
    发贴心情 
    能否提供源代码下载
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/2/4 17:13:00
     
     GoogleAdSense狮子座1978-8-18
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/12 16:55:52

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    62.500ms