資源簡介
滑動窗口協議.zip

代碼片段和文件信息
#include“socket_udp.h“
?
#define?HLEN?3//幀頭部長度
#define?DLEN?8//幀數據長度
#define?ALEN?3//ACK幀長度
#define?SWP_SEND_TIMEOUT?500//定義超時長度為500ms
#define?link?“127.0.0.1“//定義要發送的對象
#define?FLAG_ACK_VALID?‘a‘//定義ACK幀
#define?FLAG_DATA_VALID?‘d‘//定義數據幀
#define?SUCCESS?1;//定義已經成功收到的消息
?
?
static?void?sendSWP(SwpState?*stateMsg?*frame);//發送函數
static?int?deliverSWP(SwpState?*stateMsg?*frame);//接收并返回ACK函數
?
//發送
static?void?semWait(Semaphore?*sendWindowNotFull);//信號量處理(-1)
static?void?store_swp_hdr(SwpHdr?hdrchar?*hbuf);//存儲發送的幀頭部
static?void?msgAddHdr(Msg?*framechar?*hbuf);//將頭部添加到幀上
static?void?msgSaveCopy(char?*msgMsg?*frame);//將消息添加到幀上
static?Event?evSchedule(Msg?*frameint?time);//調用定時器函數
DWORD?WINAPI?swpTimeout(LPVOID?threadtype);//創建定時器線程
static?void?send_socket(char?*addrMsg?*frameint?size);//UDP發送
static?void?mlisten();//監聽數據
static?void?msend();//發送數據
//接收
static?char?*msgStripHdr(Msg?*frameint?length);//獲取幀的頭部
static?void?load_swp_hdr(SwpHdr?*hdrchar?*hbuf);//幀頭部提取字符串
static?bool?swpInWindow(SwpSeqno?AckNumSwpSeqno?LARSwpSeqno?LFS);//判斷收到的幀是否在窗口內
static?void?evCancel(Event?*);//取消超時定時器
static?void?msgDestroy(Msg?*msg);//釋放msg空間
static?void?semSignal(Semaphore?*sendWindowNotFull);//信號量處理(+1)
static?void?prepare_ack(Msg?*msgSwpSeqno?n);//組成ACK消息
?
//全局變量
HANDLE?hlisten;
HANDLE?hsend;
SwpState?*send_state;
Msg?*frame;
HANDLE?Mutex?=?CreateMutex(NULLFALSENULL);
?
void?main(){
//模擬滑動窗口接收和發送過程
//初始化SwpStateMsg數據幀組幀
//啟動兩個線程,模擬收發
//監聽終止
frame=(Msg?*)malloc(11*sizeof(Msg));
//初始化消息
//frame=“message“;//8個字節的數據
//初始化狀態
send_state=(SwpState?*)malloc(sizeof(SwpState));
send_state->LAR=‘0‘;
send_state->LFS=‘0‘;
send_state->NFE=‘0‘;
send_state->sendWindowNotFull=CreateSemaphore(NULLSWSSWSNULL);
if(send_state->sendWindowNotFull==NULL){
printf(“CreateSemaphore?error\n“GetLastError());
exit(0);
}
send_state->hdr.SeqNum=‘0‘;//3個字節頭部
send_state->hdr.AckNum=‘0‘;
send_state->hdr.Flags=‘d‘;
for(int?i=0;i send_state->sendQ[i].msg=(Msg?*)malloc(8*sizeof(Msg));
send_state->sendQ[i].msg=“message“;
send_state->sendQ[i].timeout=NULL;
}
for(i=0;i send_state->recvQ[i].msg=(Msg?*)malloc(8*sizeof(Msg));
send_state->recvQ[i].msg=“message“;
send_state->recvQ[i].recevied=NULL;
}
//初始化UDP函數
WSADATA?wsaData;
????if?(WSAStartup(MAKEWORD(21)&wsaData)){
printf(“Winsock?initializing?fail\n“);
????????WSACleanup();
????????return;
????}
//建立監聽線程
hlisten=CreateThread(NULL0(LPTHREAD_START_ROUTINE)?mlistenNULLCREATE_SUSPENDEDNULL);
ResumeThread(hlisten);
if(hlisten==NULL){
printf(“thread_listen?create?fail\n“);
}
hsend=CreateThread(NULL0(LPTHREAD_START_ROUTINE)?msendNULLCREATE_SUSPENDEDNULL);
ResumeThread(hsend);
if(hsend==NULL){
printf(“thread_send?create?fail\n“);
}
Sleep(10000);
????WSACleanup();
}
static?void?msend(){
printf(“thread_send?started\n“);
while(1){
sendSWP(send_stateframe);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-10-31?19:00??Debug\
?????文件???????32204??2018-10-31?19:00??Debug\socket_udp.obj
?????文件??????148480??2018-10-31?19:00??Debug\vc60.idb
?????文件???????77824??2018-10-31?19:00??Debug\vc60.pdb
?????文件??????176171??2018-10-31?19:00??Debug\xieyi.exe
?????文件??????207796??2018-10-31?19:00??Debug\xieyi.ilk
?????文件?????3596772??2018-10-31?18:05??Debug\xieyi.pch
?????文件??????459776??2018-10-31?19:00??Debug\xieyi.pdb
?????文件????????9025??2018-10-31?18:59??socket_udp.cpp
?????文件????????1065??2018-10-31?18:05??socket_udp.h
?????文件????????4342??2018-10-31?19:17??xieyi.dsp
?????文件?????????516??2018-10-31?18:04??xieyi.dsw
?????文件???????33792??2018-10-31?19:17??xieyi.ncb
?????文件???????48640??2018-10-31?19:17??xieyi.opt
?????文件????????1360??2018-10-31?19:00??xieyi.plg
評論
共有 條評論