分享
 
 
 

TCP通信包使用代码示例

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有