TCP 实验报告Word格式.docx
- 文档编号:19859358
- 上传时间:2023-01-11
- 格式:DOCX
- 页数:14
- 大小:226.73KB
TCP 实验报告Word格式.docx
《TCP 实验报告Word格式.docx》由会员分享,可在线阅读,更多相关《TCP 实验报告Word格式.docx(14页珍藏版)》请在冰豆网上搜索。
3.程序实验原理及流程图
在TCP/IP协议下,通讯是建立在服务器与客户端之间的。
由客户端想服务器发送连接请求,服务器响应后建立连接,然后才能开始进行通讯。
一下是实现的流程图。
系统工作流程:
4.节目设计
在VS2010的项目下创建基于对话框模式的TCP服务器端和TCP客户端,设置控件并调整相应的属性。
下面是初始化的效果:
服务器端:
客户端:
5.主要函数及其参数说明
服务器端控件:
服务器端代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingStriveEngine;
usingStriveEngine.Core;
usingStriveEngine.Tcp.Server;
usingSystem.Net;
namespaceStriveEngine.SimpleDemoServer
{
publicpartialclassForm1:
Form
{
privateITcpServerEnginetcpServerEngine;
publicForm1()
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
try
//初始化并启动服务端引擎(TCP、文本协议)
this.tcpServerEngine=NetworkEngineFactory.CreateTextTcpServerEngine(int.Parse(this.textBox_port.Text),newDefaultTextContractHelper("
\0"
));
//DefaultTextContractHelper是StriveEngine内置的ITextContractHelper实现。
使用UTF-8对EndToken进行编码。
this.tcpServerEngine.ClientCountChanged+=newCbDelegate<
int>
(tcpServerEngine_ClientCountChanged);
this.tcpServerEngine.ClientConnected+=newCbDelegate<
System.Net.IPEndPoint>
(tcpServerEngine_ClientConnected);
this.tcpServerEngine.ClientDisconnected+=newCbDelegate<
(tcpServerEngine_ClientDisconnected);
this.tcpServerEngine.MessageReceived+=newCbDelegate<
IPEndPoint,byte[]>
(tcpServerEngine_MessageReceived);
this.tcpServerEngine.Initialize();
this.button1.Enabled=false;
this.textBox_port.ReadOnly=true;
this.button2.Enabled=true;
catch(Exceptionee)
MessageBox.Show(ee.Message);
voidtcpServerEngine_MessageReceived(IPEndPointclient,byte[]bMsg)
stringmsg=System.Text.Encoding.UTF8.GetString(bMsg);
//消息使用UTF-8编码
msg=msg.Substring(0,msg.Length-1);
//将结束标记"
剔除
this.ShowClientMsg(client,msg);
voidtcpServerEngine_ClientDisconnected(System.Net.IPEndPointipe)
stringmsg=string.Format("
{0}下线"
ipe);
this.ShowEvent(msg);
voidtcpServerEngine_ClientConnected(System.Net.IPEndPointipe)
{0}上线"
ipe);
}
voidtcpServerEngine_ClientCountChanged(intcount)
this.ShowConnectionCount(count);
privatevoidShowEvent(stringmsg)
if(this.InvokeRequired)
this.BeginInvoke(newCbDelegate<
string>
(this.ShowEvent),msg);
else
this.toolStripLabel_event.Text=msg;
privatevoidShowClientMsg(IPEndPointclient,stringmsg)
IPEndPoint,string>
(this.ShowClientMsg),client,msg);
ListViewItemitem=newListViewItem(newstring[]{DateTime.Now.ToString(),client.ToString(),msg});
this.listView1.Items.Insert(0,item);
privatevoidShowConnectionCount(intclientCount)
(this.ShowConnectionCount),clientCount);
this.toolStripLabel_clientCount.Text="
在线数量:
"
+clientCount.ToString();
privatevoidcomboBox1_DropDown(objectsender,EventArgse)
List<
IPEndPoint>
list=this.tcpServerEngine.GetClientList();
boBox1.DataSource=list;
服务器向在线客户端发送消息时选中客户端:
privatevoidbutton2_Click(objectsender,EventArgse)
IPEndPointclient=(IPEndPoint)boBox1.SelectedItem;
if(client==null)
MessageBox.Show("
没有选中任何在线客户端!
"
);
return;
if(!
this.tcpServerEngine.IsClientOnline(client))
目标客户端不在线!
stringmsg=this.textBox_msg.Text+"
;
//"
表示一个消息的结尾
byte[]bMsg=System.Text.Encoding.UTF8.GetBytes(msg);
//消息使用UTF-8编码
this.tcpServerEngine.SendMessageToClient(client,bMsg);
privatevoidForm1_Load(objectsender,EventArgse)
}
客户端控件:
客户端代码:
usingSystem.Net.Sockets;
usingStriveEngine.Tcp.Passive;
namespaceStriveEngine.SimpleDemoClient
privateITcpPassiveEnginetcpPassiveEngine;
privatevoidbutton3_Click(objectsender,EventArgse)
//初始化并启动客户端引擎(TCP、文本协议)
this.tcpPassiveEngine=NetworkEngineFactory.CreateTextTcpPassiveEngine(this.textBox_IP.Text,int.Parse(this.textBox_port.Text),newDefaultTextContractHelper("
this.tcpPassiveEngine.MessageReceived+=newCbDelegate<
System.Net.IPEndPoint,byte[]>
(tcpPassiveEngine_MessageReceived);
this.tcpPassiveEngine.AutoReconnect=true;
//启动掉线自动重连
this.tcpPassiveEngine.ConnectionInterrupted+=newCbDelegate(tcpPassiveEngine_ConnectionInterrupted);
this.tcpPassiveEngine.ConnectionRebuildSucceed+=newCbDelegate(tcpPassiveEngine_ConnectionRebuildSucceed);
this.tcpPassiveEngine.Initialize();
this.button3.Enabled=false;
连接成功!
voidtcpPassiveEngine_ConnectionRebuildSucceed()
this.BeginInvoke(newCbDelegate(this.tcpPassiveEngine_ConnectionInterrupted));
重连成功。
voidtcpPassiveEngine_ConnectionInterrupted()
this.button2.Enabled=false;
您已经掉线。
voidtcpPassiveEngine_MessageReceived(System.Net.IPEndPointserverIPE,byte[]bMsg)
this.ShowMessage(msg);
privatevoidShowMessage(stringmsg)
(this.ShowMessage),msg);
ListViewItemitem=newListViewItem(newstring[]{DateTime.Now.ToString(),msg});
this.tcpPassiveEngine.SendMessageToServer(bMsg);
6.系统运行演示\
在服务器点击开始监听:
这时候监听的按钮已呈现灰色
在客户端的“服务器地址”中输入本机地址:
127.0.0.1,单击连接按钮。
系统提示连接成功。
服务器端显示出连接成功,并输出请求连接的客户的IP地址。
并激活服务器端的“发送”按钮。
服务器默认向客户端发送“欢迎连接服务器”,点击“发送”按钮发送信息。
然后可以开始正常通讯。
关闭服务器客户端会提示:
您已掉线:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- TCP 实验报告 实验 报告