计算机网络课程设计报告 联系客服

发布时间 : 星期日 文章计算机网络课程设计报告更新完毕开始阅读9c36a50203d8ce2f006623ba

set nf [open pre-vj.nam w]

$ns namtrace-all $nf #打开并关联一个NAM跟踪文件pre-vj.nam,其文件描述字为nf

# Create 2 duplex links #建立两条双向链路

#在节点n0和n1之间创建一个带宽1Mb、时延为50ms,链路类型为尾部丢弃的双向链路。 $ns duplex-link $n0 $n1 1Mb 50ms DropTail

#在节点n1和n2之间创建一个带宽为0.55Mb、时延为50ms,链路类型为尾部丢弃的双向链路。

$ns duplex-link $n1 $n2 0.55Mb 50ms DropTail

# Set Node 1's queue size to 4 #设置节点1的队列大小为4

$ns queue-limit $n1 $n2 4 #设置n1和n2之间的链路队列大小为4

# Orient the nodes; show the queue #确定节点方向,指出队列 $ns duplex-link-op $n0 $n1 orient 10deg #设置n1在n0的10度角位置 $ns duplex-link-op $n1 $n2 orient 10deg #设置n2在n1的10度角位置

$ns duplex-link-op $n1 $n2 queuePos 0.5 #监测n1和n2的队列(用于NAM显示)

#在节点0建立发送方的TCP代理,设置接收方窗口为8

# Create sender TCP agent at Node 0; set receiver advertised window to 8 set tcpSender [new Agent/TCP] #创建一个TCP代理 $tcpSender set slow_start_ $vj_ss #设置慢启动 $tcpSender set colored_rtx_ 0 #设置发送方颜色 $tcpSender set window_ 64 #设置窗口大小 $tcpSender set class_ 0 #设置类

$tcpSender set packetSize_ 1000 #设置分组大小为1000 $tcpSender set window_ 12 #设置窗口大小 #调用ns的attach-agent过程将代理TCP添加到节点n0上 $ns attach-agent $n0 $tcpSender

#为TCP发送建立一个CBR代理,

# Create CBR agent for TCP sender; set packet size to 1000 bytes, at 1 Mbps

#创建一个CBR流量产生器类对象cbr_,用于模拟真实TCP/IP网络中的应用层 set cbr [new Application/Traffic/CBR]

$cbr attach-agent $tcpSender #将cbr_流量产生器添加到tcp代理上 $cbr set packet_size_ 1000 #设置cbr_流量产生器的数据包大小 $cbr set rate_ 1Mbps #设置数据发送速率 $cbr set maxpkts_ 100

# Create receiver TCP agent at Node 0 set tcpReceiver [new Agent/TCPSink]

$ns attach-agent $n2 $tcpReceiver #将tcp接收添加到n2节点上

# Connect the two TCP agents #连接两个tcp代理

$ns connect $tcpSender $tcpReceiver

# Run the simulation #运行这个simulation类

#以上两条命令启用ns的事件调度器,分别在0.1

秒时启动cbr_流量发生器并发送数据包,在20秒停止cbr_流量发生器。 $ns at 0.1 \$ns at 20 \

proc finish {} {

# global ns nf f global ns nf $ns flush-trace

# close $f close $nf

puts \ exec nam out.nam & exit 0 }

$ns run

实验三:编写程序

1、滑动窗口

#include #include #include #define THANKS -1 void main() {

FILE *r_File1; FILE *w_File2; int m_framecount; int frameCount = 0; long currentP = 0; long sentChar = 0; long recvedChar = 0; char s_name[100]; char d_name[100]; char *sp = s_name; char *dp = d_name; int slidingWin; int frameSize;

#声明全局变量 ns、nf、f #关闭f描述字 #关闭nf描述字 #运行模拟器

int dataSize;

bool isEnd = false; struct FRAME{ int s_flag;

int sequenceNo; char data[90]; int n_flag; };

FRAME frame;

frame.s_flag = 126;//set start flag frame.n_flag = 126;//set end flag

memset(frame.data, 0, 91);//use 0 to fill full the member array in structure frame. struct ACK{ int s_flag; int nextSeq; int n_flag; }ack;

//initialize start flag and end flag in structure ack. ack.s_flag = 126; ack.n_flag = 126; ack.nextSeq = NULL;

//ask user to enter file name and size of sliding window. lable1 : cout <<\ cin >> sp;

cout <<\ cin >> dp;

lable2: cout <<\ cin >> slidingWin;

if((slidingWin >7 )| (slidingWin < 2)) {

cout << \ goto lable2; }

lable3: cout<< \ cin >>frameSize;

if((frameSize > 101) | (frameSize < 14))

{ cout << \ goto lable3; }

//use frameSize to decide the size of data array in structor frame. dataSize = frameSize - 12;

//dynamic generate a frame array with user enter's size of sliding window FRAME *pf = new FRAME[slidingWin]; int seqNo = 0;

//strat loop for transmission.

while (ack.nextSeq != THANKS) {

cout << \ //open a source file by read mode.

if((r_File1 = fopen(sp, \ {

cout << \ goto lable1; } else {

cout<<\ cout <

//after open the file, use fseek to resume the last position of a file pointer. //Then start to read from that position. fseek(r_File1,currentP,SEEK_SET); //start loop for create frame array

for (int i = 0; i < slidingWin ; i++)// i is the frame array's index {

frame.sequenceNo = seqNo; if ((seqNo >= 7) == true) {

seqNo = 0;//set sequencce number } else {

seqNo = seqNo +1; }

//This loop is used to fill the characters read from opened file to char array data which //is a memeber of structure frame.

//we have to reseve a byte for \\0 which is used to identify the end of the data array. //that means each time we only read datasize -1 characters to the data array. for (int j = 0; j < dataSize -1; j++)

{ //if it is not end of file read a character from file then save it into data //field in frame structure.

frame.data[j] = fgetc(r_File1);

sentChar++;//calculate how many characters will be sent.*/ if (frame.data[j] == EOF) {

cout<< \ isEnd = true; //sentChar++;