資源簡介
使用VS中的VC++下面的CLR編寫,主要目的用Socket模擬TCP通信協議,實驗內容如下:i. 實現滑動窗口協議,窗口大小為5
ii. 模擬采用三次握手機制,顯示出ACK、ack、Seq等標識位和參數
iii. 必須采用應答機制、超時計數器技術、幀編號判重技術、重傳技術
iv. 校驗和技術
a) 校驗和s的計算:設要發送n字節,bi為第i個字,s=(b0+b1+…+bn) mod 256
v. 在接收端,設置隨機數,根據隨機數執行相關操作,0代表正常,1代表幀丟失,2代表幀出錯,3代表應答幀丟失(即不發生應答幀)
vi. 必須使用圖形界面,
a) 按批次(發送端一次發送的報文)顯示相關內容
b) 發送端:顯示發送的數據、是否重傳、本次幀序號、接收到的應答幀的序號
c) 接收端:顯示接收到的數據、本次幀序號、本次隨機選擇的出錯情況、發送應答幀的序號、是否重復
要先打開Server端啟動監聽,再打開Client端進行連接,三次握手之后開始通信。
代碼片段和文件信息
#include?“ClientForm.h“
#include?
#include?
#include?
using?namespace?Client;
time_t?Start;//時間開始
time_t?End;//時間結束
[STAThreadAttribute]
int?main(array^args)
{
Application::EnableVisualstyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew?ClientForm());
return?0;
}
void?ClientForm::acceptMessage()?{
isStillReceive?=?true;
isStillSend?=?true;
array^bytes?=?gcnew?array(256);
try
{
//建立遠端Endpoint
Int32?port?=?13500;
// Int32?port?=?Convert::ToInt32(tbPort->Text->Trim());
// IPAddress^ipAddress?=?IPAddress::Parse(tbIP->Text->Trim());
IPAddress^ipAddress?=?IPAddress::Parse(“127.0.0.1“);
IPEndPoint^remoteEP?=?gcnew?IPEndPoint(ipAddress?port);
//建立socket對象
socketAtClient?=?gcnew?Socket(AddressFamily::InterNetwork?SocketType::Stream?ProtocolType::Tcp);
//將socket連接至遠端服務器
socketAtClient->Connect(remoteEP);
if?(socketAtClient->Connected)?{
// isConnectionOK?=?true;
tbSocketConn->Text?=?L“連接服務器成功!“;
}
tbMSGHistory->AppendText(“第一次握手:SYN?=?1??Seq?=?4000??ack?=?0\r\n“);
Thread::Sleep(1000);
tbMSGHistory->AppendText(“第二次握手:SYN?=?1??Seq?=?4500??ack?=?4001??ACK?=?1\r\n“);
Thread::Sleep(1000);
tbMSGHistory->AppendText(“第三次握手:SYN?=????Seq?=?4001??ack?=?4501\r\n“);
tbMSGHistory->AppendText(“完成三次握手\r\n“);
tbMSGHistory->AppendText(“\r\n“);
}
catch?(Exception^)
{
MessageBox::Show(“Exception?accrued?during?connect?server!“?“Exception“);
}
}
void?ClientForm::SendMessage()?{
try
{
while?(isStillSend)//開始循環發送數據
{
SendMSG[2]?=?0;//FIN?=?0
SendMSG[0]?=?Seqnum;
Start?=?clock();
socketAtClient->Send(SendMSG);//發送數據
datanum?=?Seqnum?%?10;
Data[0]?=?datanum;
tbSendData->AppendText(““+?datanum);//顯示本次發送的數據
tbSendNum->AppendText(““+Seqnum);//顯示本次發送的幀序號
if?(flagError?==?0)//是否重傳
{
tbError->AppendText(“否“);
}
else?if?(flagError?==?1)?{
tbError->AppendText(“是“);
flagError?=?0;
}
tbMSGHistory->AppendText(“Send:????幀序號“?+?Seqnum?+?“??數據:?“?+?datanum+“\r\n“);//在消息歷史中顯示本次發送的內容
Seqnum++;
Thread::Sleep(1000);
tbSendData->Clear();//清空本次消息內容
tbSendNum->Clear();//清空本次序號內容
tbError->Clear();//清空本次是否重傳內容
}
}
catch?(Exception^)
{
MessageBox::Show(“Exception?accrued?during?Client“?“Exception“);
}
}
void?ClientForm::ReceiveMessage()?{
try
{
while?(isStillReceive)
{
if?(socketAtClient->Available?>?0)
{
int?byteCount?=?socketAtClient->Receive(ACKMSG);//開始接收數據
End?=?clock();
if?(Seqnum?-?ACKMSG[0]?==?5)//保證滑動窗口為5
{
Seqnum--;
Thread::Sleep(5000);
}
if?(byteCount?>?0)?{
if?(ACKMSG[1]?==?1?||?ACKMSG[1]?==?3?||?difftime(EndStart)?>?1000)//有出錯的情況,需要重傳,只要幀不正?;蛘叱瑫r
{
Seqnum--;
flagError?=?1;
}
else?if(ACKMSG[1]?==?0)
{
tbRecvNum->AppendText(““+AC
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????34304??2018-05-22?22:16??TCP_Socket\Client\.vs\Client\v15\.suo
?????文件???57237504??2018-05-22?22:16??TCP_Socket\Client\.vs\Client\v15\Browse.VC.db
?????文件???11075584??2018-05-15?17:19??TCP_Socket\Client\.vs\Client\v15\ipch\AutoPCH\54b0956dc61d3843\MYFORM.ipch
?????文件???76546048??2018-05-22?21:53??TCP_Socket\Client\.vs\Client\v15\ipch\AutoPCH\c358c61f70b1cb8e\CLIENTFORM.ipch
?????文件???????6934??2018-05-15?17:33??TCP_Socket\Client\Client\Client.vcxproj
?????文件???????1078??2018-05-15?17:20??TCP_Socket\Client\Client\Client.vcxproj.filters
?????文件????????165??2018-05-15?17:17??TCP_Socket\Client\Client\Client.vcxproj.user
?????文件???????3575??2018-05-20?15:59??TCP_Socket\Client\Client\ClientForm.cpp
?????文件??????13827??2018-05-20?15:59??TCP_Socket\Client\Client\ClientForm.h
?????文件???????5817??2018-05-20?14:44??TCP_Socket\Client\Client\ClientForm.resx
?????文件????????229??2018-05-19?19:47??TCP_Socket\Client\Client\Debug\.NETfr
?????文件???????4208??2018-05-19?19:47??TCP_Socket\Client\Client\Debug\.NETfr
?????文件????????180??2018-05-20?14:44??TCP_Socket\Client\Client\Debug\Client.ClientForm.resources
?????文件?????????88??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\Client.log
?????文件???????3294??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\Client.tlog\CL.command.1.tlog
?????文件??????43694??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\Client.tlog\CL.read.1.tlog
?????文件????????996??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\Client.tlog\CL.write.1.tlog
?????文件????????205??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\Client.tlog\Client.lastbuildstate
?????文件??????20834??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\Client.tlog\Client.write.1u.tlog
?????文件???????1210??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\Client.tlog\li
?????文件???????4156??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\Client.tlog\li
?????文件????????496??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\Client.tlog\li
?????文件??????????2??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\Client.tlog\me
?????文件??????????2??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\Client.tlog\me
?????文件??????????0??2018-05-15?17:20??TCP_Socket\Client\Client\Debug\Client.tlog\TZRES.DLL.bi
?????文件????????944??2018-05-20?14:44??TCP_Socket\Client\Client\Debug\Client.vcxproj.GenerateResource.cache
?????文件???????6366??2018-05-15?17:21??TCP_Socket\Client\Client\Debug\Client.vcxprojAssemblyReference.cache
?????文件?????513190??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\ClientForm.obj
?????文件?????454656??2018-05-20?15:59??TCP_Socket\Client\Client\Debug\vc141.pdb
?????文件???????1432??2018-05-15?17:17??TCP_Socket\Client\Client.sln
............此處省略66個文件信息
評論
共有 條評論