資源簡介
自己編的程序,利用所學數據鏈路層原理,自己設計一個滑動窗口協議并在仿真環境下編程實現有噪音信道環境下兩站點之間無差錯雙工通信。信道模型為8000bps 全雙工衛星信道,信道傳播時延270 毫秒,信道誤碼率為10-5,信道提供字節流傳輸服務,網絡層分組長度在240~256字節范圍。通過該實驗,進一步鞏固和深刻理解數據鏈路層的誤碼檢測的CRC 校驗技術,以及滑動窗口的工作機理。滑動窗口機制的兩個主要目標:(1) 實現有噪音信道環境下的無差錯傳輸;(2)充分利用傳輸信道的帶寬。在程序能夠穩定運行并成功實現第一個目標之后,運行程序并檢查在信道沒有誤碼和存在誤碼兩種情況下的信道利用率。

代碼片段和文件信息
#include?
#include?
#include?“protocol.h“
#define?MAX_SEQ?15
#define?NR_BUFS?((MAX_SEQ+1)/2)
#define?DATA_TIMER??3000???????//幀超時時間間隔
#define?ACK_TIMER?240??????????//ack的超時間隔
struct?frame?{?
????unsigned?char?kind;
????unsigned?char?ack;
????unsigned?char?seq;
????unsigned?char?data[PKT_LEN];?
????unsigned?int??padding;
};
int?no_nak=1;????????????????????????//標志是否已經發送過nak
static?int?phl_ready?=?0;
unsigned?char?oldest_frame=MAX_SEQ+1;
static?int?between(unsigned?char?aunsigned?char?bunsigned?char?c)??
//窗口函數,判斷幀號(ack號)是否在窗口內
{
???if(((a<=b)&&(b return?1;
else
return?0;
}
static?void?put_frame(unsigned?char?*frame?int?len)//加入校驗和crc
{
????*(unsigned?int?*)(frame?+?len)?=?crc32(frame?len);
????send_frame(frame?len?+?4);
????phl_ready?=?0;
}
static?void?send_data_frame(unsigned?char?fkunsigned?char?frame_nrunsigned?char?frame_expectedunsigned?char?buffer[NR_BUFS][PKT_LEN])
{//發送數據幀,或ack,或nak
????struct?frame?s;
????
????s.kind?=?fk;
????s.seq?=?frame_nr;
????s.ack?=?(frame_expected?+?MAX_SEQ)?%?(MAX_SEQ?+?1);
if(fk==frame_DATA)
{
memcpy(s.data?buffer[frame_nr?%?NR_BUFS]?PKT_LEN);//復制分組到幀內
dbg_frame(“Send?DATA?%d?%d?ID?%d\n“?s.seq?s.ack?*(short?*)s.data);
????????put_frame((unsigned?char?*)&s?3?+?PKT_LEN);???//加入校驗和
start_timer(frame_nr%NR_BUFS?DATA_TIMER);?????//啟動定時器
}
else?if(fk?==?frame_NAK)
{
no_nak=0;?????????????????????????????????????//下一次不再發送nak
????put_frame((unsigned?char?*)&s?3);????????????
}
else?if(fk?==?frame_ACK)
{
dbg_frame(“Send?ACK??%d\n“?s.ack);
????????put_frame((unsigned?char?*)&s?3);
}
phl_ready?=?0;
stop_ack_timer();??????????????????????????//沒有必要啟動ack定時器
}
void?main(int?argc?char?**argv)
{
int?event?arg;
????struct?frame?f;
????int?len?=?0;
????int?i;
static?unsigned?char?ack_expected=0?next_frame_to_send=0;
static?unsigned?char?frame_expected=0?too_far=NR_BUFS;
????static?unsigned?char?nbuffered;
int?arrived[NR_BUFS];
static?unsigned?char?out_buf[NR_BUFS][PKT_LEN]?in_buf[NR_BUFS][PKT_LEN];
????protocol_init(argc?argv);?
????lprintf(“Designed?by?成賀睿&魏娜?build:?“?__DATE__“??“__TIME__“\n“);
for(i?=?0;?i? arrived[i]=0;
????enable_network_layer();
while(1)
{
event?=?wait_for_event(&arg);
switch?(event)
{
case?NETWORK_layer_READY:
nbuffered++;?????????????????????//發送卻未被確認的幀數目++
get_packet(out_buf[next_frame_to_send?%?NR_BUFS]);//得到分組存入緩沖區內
send_data_frame(frame_DATAnext_frame_to_sendframe_expectedout_buf);
next_frame_to_send=(next_frame_to_send?+?1)?%?(?MAX_SEQ?+?1);
break;
case?PHYSICAL_layer_READY:
phl_ready?=?1;
break;
case?frame_RECEIVED:
len?=?recv_frame((unsigned?char?*)&f?sizeof?f);
????????????????if?(len?5?||?crc32((unsigned?char?*)&f?len)?!=?0)
{ //校驗和出錯,就發送nak請求重傳
??? if(no_nak=
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3969??2009-11-28?22:25??Selective\datali
?????文件???????3942??2009-11-28?22:25??Selective\datali
?????文件???????5282??2009-11-28?18:23??Selective\datali
?????文件???????3476??2009-10-19?16:46??Selective\datali
?????文件????????524??2009-10-16?10:23??Selective\datali
?????文件?????176222??2009-11-28?19:00??Selective\datali
?????文件????????518??2009-10-16?12:34??Selective\datali
?????文件?????189268??2009-11-28?19:00??Selective\datali
?????文件??????50176??2009-11-28?19:29??Selective\datali
?????文件??????48640??2009-11-28?19:29??Selective\datali
?????文件?????427008??2009-11-28?19:00??Selective\datali
?????文件????????250??2009-11-28?19:00??Selective\datali
?????文件??????11396??2009-11-28?19:00??Selective\Debug\datali
?????文件?????184028??2009-11-28?19:00??Selective\Debug\datali
?????文件??????41984??2009-11-28?19:00??Selective\Debug\vc60.idb
?????文件??????53248??2009-11-28?19:00??Selective\Debug\vc60.pdb
?????文件????????283??2007-12-02?10:47??Selective\jiangTime.swf
?????文件??????94208??2009-10-19?22:57??Selective\Protocol.dll
?????文件???????1894??2009-11-24?07:15??Selective\protocol.h
?????文件???????5932??2009-10-19?22:57??Selective\Protocol.lib
????..A.SH.??????5120??2009-11-23?23:52??Selective\Thumbs.db
?????目錄??????????0??2009-11-28?19:00??Selective\Debug
?????目錄??????????0??2009-11-28?19:29??Selective
-----------?---------??----------?-----??----
??????????????1307368????????????????????23
評論
共有 條評論