当前位置:文档之家› 停等协议

停等协议

1/6 网络实验报告(二) ——停--等协议

一、实验目的: 1.利用VB模拟停--等协议。 2.了解协议内容并模拟传输过程,掌握停--等协议的实现方法。 二、实验环境: 硬件环境:P4 1.6G.128M SDRAM。 系统环境:Windows98操作系统。 开发环境:Microsoft Visual Basic 6.0 中文版。 模拟环境:点到点的面向链接的通信。 三、桢格式: 桢头 数据 桢尾

注: 帧头与帧尾均为字符串“DLE STX”。 对将要发送的数据使用split()函数,以空格为标志进行分割,把数据分成以单词为单位的字符串数组,每次读取一个字符串。 以帧头+空格+数据+空格+帧尾为顺序组帧。分帧时使用split()函数,以空格为标志,把数据帧分解成字符串数组,读出数据,完成组帧。 四、程序流程图:

F T

服务器端(接收端) 客户端(发送端) 开始 开始

开始监听 接受连接请求 连接服务器 准备数据并分桢

显示数据 接收数据并发确认桢 按桢发送数据 数据结束? 结束 2/6

五、编程原理及思路: 由于接收缓冲只能存放一个帧且接收端需要一定的处理时间。为了防止发送快于接收而导致数据丢失,限制发送端在发送一帧后必须停止发送,等待接收端发确认帧。而接收端在收到一个数据帧并发送网络层后,向发送端发一确认帧。仅当接收方确认正确接收后再继续发送下一帧。 六、程序源代码(VB): 注:此程序的接收端和发送端是同一个程序的不同运行模式。 frmMain.frm: Private g_bConfirm As Boolean '发送一帧后设置为False,收到后设置为True

Private Sub cmdReceiverListen_Click() '检查错误 If Val(txtReceiverPort.Text) = 0 Then MsgBox "Please Input Port Number!", vbInformation, "ERROR" Exit Sub End If '开始监听 With sckReceiver .LocalPort = Val(txtReceiverPort.Text) .Listen End With '显示信息 ShowInfo "Listening at : " & sckReceiver.LocalIP & " Port : " & txtReceiverPort.Text cmdReceiverListen.Enabled = False End Sub

Private Sub ShowInfo(ByVal sInfo As String) '显示信息到列表 lstInfo.AddItem (sInfo) lstInfo.ListIndex = lstInfo.ListCount - 1 End Sub

Private Sub cmdSenderConnect_Click() '连接 If cmdSenderConnect.Caption = "&Connect" Then If txtSenderHostIP.Text = "" Or txtSenderPort.Text = "" Then MsgBox "Invalid IP address/port number input.", vbCritical, "ERROR" Exit Sub End If With sckSender .RemoteHost = txtSenderHostIP.Text .RemotePort = Val(txtSenderPort.Text) .Connect 3/6

ShowInfo "Connect To : " & .RemoteHost & " Port : " & .RemotePort End With cmdSenderConnect.Caption = "Dis&connect" cmdSenderSend.Enabled = True Else ShowInfo "Termination Of Connection To : " & sckSender.RemoteHost & " Port : " & sckSender.RemotePort sckSender.Close cmdSenderConnect.Caption = "&Connect" cmdSenderSend.Enabled = False End If End Sub

Private Sub cmdSenderSend_Click() '发送文字 Dim vOut As Variant Dim lLoop As Long g_bConfirm = True vOut = modFrame.ToFrame(txtSenderMessage.Text) ShowInfo "Sending Message To : " & sckSender.RemoteHostIP & " With """ & txtSenderMessage.Text & """" For lLoop = 0 To UBound(vOut) '循环输出帧 While Not g_bConfirm DoEvents '等待 Wend '能够发送 g_bConfirm = False Debug.Print vOut(lLoop) Dim sOut As String sOut = vOut(lLoop) sckSender.SendData sOut Next End Sub

Private Sub Form_Load() tabMain_Click End Sub

Private Sub sckReceiver_ConnectionRequest(ByVal requestID As Long) '得到连接请求 With sckReceiver .Close .Accept requestID 4/6

ShowInfo "Incoming Connection From : " & .RemoteHostIP End With End Sub Private Sub sckReceiver_DataArrival(ByVal bytesTotal As Long) '得到数据 Dim sIn As String '收到字符串 With sckReceiver .GetData sIn, vbString Debug.Print "RECEIVING " & sIn txtReceiverMessage.Text = txtReceiverMessage.Text & " " & modFrame.FromFrame(sIn) ShowInfo "Incoming Message : " & .RemoteHostIP & " [" & sIn & "]" .SendData 1 End With End Sub

Private Sub sckSender_DataArrival(ByVal bytesTotal As Long) '收到数据 '实际上就是收到确认帧的问题 g_bConfirm = True End Sub

Private Sub tabMain_Click() If tabMain.SelectedItem.Caption = "As &Sender" Then fmeSender.Visible = True fmeReceiver.Visible = False Else fmeSender.Visible = False fmeReceiver.Visible = True End If End Sub

Private Sub txtReceiverPort_GotFocus() '得到焦点的时候选定字符 txtReceiverPort.SelLength = Len(txtReceiverPort.Text) End Sub

Private Sub txtReceiverPort_KeyPress(KeyAscii As Integer) '不允许输入数字以外的字符 If KeyAscii <> 8 Then If KeyAscii > 57 Or KeyAscii < 48 Then KeyAscii = 0 End If End If 5/6

End Sub Private Sub txtSenderHostIP_KeyPress(KeyAscii As Integer) '不允许输入数字以外,非“.”号的字符 If Not (KeyAscii = 8 Or KeyAscii = 46) Then If KeyAscii > 57 Or KeyAscii < 48 Then KeyAscii = 0 End If End If End Sub

Private Sub txtSenderPort_GotFocus() '得到焦点后选定字符 txtSenderPort.SelLength = Len(txtSenderPort.Text) End Sub

Private Sub txtSenderPort_KeyPress(KeyAscii As Integer) '不允许输入数字以外的字符 If KeyAscii <> 8 Then If KeyAscii > 57 Or KeyAscii < 48 Then KeyAscii = 0 End If End If End Sub

modFrame.bas: Private Const FRAME_HEAD = "DLE STX" Private Const FRAME_END = "DLE ETX" Public Function FromFrame(ByVal sIn As String) As String '输入帧,输出原始文字 Dim vIn As Variant vIn = Split(sIn, " ") vIn(2) = Replace(vIn(2), "DLEDLE", "DLE") FromFrame = vIn(2) End Function Function ToFrame(ByVal sIn As String) As Variant '将字符串分帧 '输入数据是字符串,输出为从0开始的数组 Dim vFrame As Variant Dim lLoop As Long vFrame = Split(sIn, " ") '以空格为标志,把数据分成以单词为单位的字符串数组 For lLoop = 0 To UBound(vFrame) vFrame(lLoop) = Replace(vFrame(lLoop), "DLE", "DLEDLE") vFrame(lLoop) = FRAME_HEAD & " " & vFrame(lLoop) & " " & FRAME_END

相关主题