模仿数据链路层停等协议_第1页
模仿数据链路层停等协议_第2页
模仿数据链路层停等协议_第3页
模仿数据链路层停等协议_第4页
模仿数据链路层停等协议_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

对于一些初学网络的人,老师突然叫你编一个模拟停等协议的程序,可能很多人都会茫然,我也如此,但是通过几天的努力还是基本完成了任务,具体如下:此程序运行在.NETFRAMEWORK4.0以上,编译平台VS2010;程序界面如下:具体怎样设计取决与编程的人主要操作过程:方法1:先创建一个VisualStudio解决方案,再在里面添加两个Windows窗口应用程序作为发送方和接收方;方法2:直接创建两个Windows窗口应用程序作为发送方和接收方;程序代码如下:有的控件名称被修改过的所以不要直接复制要先修改控件名称程序有点乱,有的代码无用,但对程序没有多大影响发送方源代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Net.Sockets;//usingSystem.Net;//引用Socket套接字namespace发送方{publicpartialclassfmSender:Form{Socketsocket;//套接字实例stringIP;//保存主机ipintPort;//保存端口IPEndPointiep;intZhenCount;//保存分帧数privatestring[]ZuZhen()//组Á¨¦帧?函¡¥数ºy{try{//stringmessage="";intcount=0;intj=0;ZhenCount=Convert.ToInt32(txtCount.Text);if(txtMsg.Text.Length%ZhenCount==0){count=txtMsg.Text.Length/ZhenCount;}else{count=txtMsg.Text.Length/ZhenCount+1;}string[]msg=newstring[count];for(inti=0;i<txtMsg.Text.Length;i++){if(i==0){msg[j]+=txtMsg.Text[i];}elseif(i%ZhenCount==0){j++;msg[j]+=txtMsg.Text[i];}else{msg[j]+=txtMsg.Text[i];}}//按ã¡ä帧?数ºy分¤?出?来¤¡ä的Ì?字Á?符¤?数ºy组Á¨¦for(intk=1;k<=count;k++){if(k==count){msg[k-1]="DLESTX"+msg[k-1]+"DLEETX"+"0";//以°?0为a结¨¢束º?帧?}else{msg[k-1]="DLESTX"+msg[k-1]+"DLEETX"+k;}}//组Á¨¦帧?for(inti=0;i<msg.Length;i++){if(msg[i]=="DLE"){msg[i].Replace("DLE","DLEDLE");}//message+=msg[i]+"";}//returnmessage;returnmsg;}catch(Exceptionex){MessageBox.Show("组Á¨¦帧?出?错䨪!ê?","系¦Ì统ª3提¬¨¢示º?",MessageBoxButtons.OK,MessageBoxIcon.Information);string[]str=newstring[]{ex.Message};returnstr;}}privatevoidBeginSend()//发¤¡é送¨ª函¡¥数ºy{string[]str=ZuZhen();rtbSendMsg.AppendText("发¤¡é送¨ª信?息¡é:êo"+txtMsg.Text+""+DateTime.Now.ToString()+"\r\n");//下?为a传ä?输º?byte[]byteMessage;//出?现?异°¨¬常¡êfor(inti=0;i<str.Length;i++){socket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);IPAddressserverIp=IPAddress.Parse(IP);intserverPort=Convert.ToInt32(Port);iep=newIPEndPoint(serverIp,serverPort);socket.Connect(iep);byteMessage=Encoding.ASCII.GetBytes(str[i]);socket.Send(byteMessage);socket.Shutdown(SocketShutdown.Both);socket.Close();}}publicfmSender(){InitializeComponent();}privatevoidtextBox2_TextChanged(objectsender,EventArgse){IP=txtIp.Text;}privatevoidfmSender_Load(objectsender,EventArgse){txtPort.Text="80";IPAddress[]ip=Dns.GetHostAddresses(Dns.GetHostName());txtIp.Text=ip[0].ToString();IP=txtIp.Text;Port=Convert.ToInt32(txtPort.Text);//获?取¨?本À?地Ì?主¡Â机¨²IPtxtCount.Text="2";//初?始º?帧?为a2}privatevoidbtnSend_Click(objectsender,EventArgse){try{BeginSend();txtMsg.Text="";}catch(Exceptionex){Console.WriteLine(ex.Message);}}privatevoidtxtPort_TextChanged(objectsender,EventArgse){Port=Convert.ToInt32(txtPort.Text);}privatevoidtxtCount_TextChanged(objectsender,EventArgse){ZhenCount=Convert.ToInt32(txtCount.Text);}privatevoidbutton1_Click(objectsender,EventArgse){rtbSendMsg.Clear();}}}接收方源代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Net.Sockets;usingSystem.Net;usingSystem.Threading;//引用线程namespace接收方{publicpartialclassForm1:Form{Threadmythread;stringIP;intPort;Socketsocket;IPEndPointiep;stringMessage;publicdelegatevoidmydeleget(Socketsocket);mydelegetmy=null;publicdelegatevoiddeleget1(inti);publicdelegatevoiddeleget2(stringstr);deleget1del1=null;deleget2del2=null;SocketnewSocket;intj=0;privatestringFenZhen(refstringend)//分帧函数{stringstr="";try{string[]msg=Message.Split(newChar[]{''});for(inti=0;i<msg.Length;i++){//richTextBox1.AppendText("第"+i+"帧:"+Zhen[i]+"\r\n");if(msg[i]=="DLEDLE"){msg[i].Replace("DLEDLE","DLE");}}str+=msg[2];end=msg[5];returnstr;}catch(Exceptionex){returnex.Message;}}//publicvoidGo(Socketsocket){inti=0;stringend1="-1";stringstr=null;stringsTime;stringmsg;byte[]byteMessage=newbyte[100];//存放接收到的信息try{while(true){/*newSocket.Receive(byteMessage);sTime=DateTime.Now.ToShortTimeString();msg=sTime+"-"+"信息来自:";Message=Encoding.Default.GetString(byteMessage).Trim(newchar[]{'\0'});//将接收到的信息转化为字符串//msg+=newSocket.RemoteEndPoint.ToString()+""+FenZhen();if(richTextBox1.InvokeRequired){my=newmydeleget(Go);this.Invoke(my,newobject[]{socket});}else{richTextBox1.AppendText("第"+i+"帧:"+Message+"\r\n");}str+=FenZhen(refend1);*/while(end1!="0"){i++;newSocket=socket.Accept();socket.Listen(5);newSocket.Receive(byteMessage);sTime=DateTime.Now.ToShortTimeString();msg=sTime+"-"+"信息来自:";Message=Encoding.Default.GetString(byteMessage).Trim(newchar[]{'\0'});//将接收到的信息转化为字符串//msg+=newSocket.RemoteEndPoint.ToString()+""+FenZhen();if(richTextBox1.InvokeRequired){//my=newmydeleget(Go);//this.Invoke(my,newobject[]{socket});del1=newdeleget1(f1);this.Invoke(del1,newobject[]{i});}else{richTextBox1.AppendText("第"+i+"帧:"+Message+"\r\n");}str+=FenZhen(refend1);}if(i!=0){if(richTextBox2.InvokeRequired){del2=newdeleget2(f2);this.Invoke(del2,newobject[]{str});}else{richTextBox2.AppendText("接收到的信息:"+str+""+DateTime.Now.ToString()+"\r\n");}}i=0;end1="-1";str="";}}catch(Exceptionex){Console.WriteLine(ex.Message);}}publicvoidf1(inti){richTextBox1.AppendText("第"+i+"帧:"+Message+"\r\n");}publicvoidf2(stringstr){richTextBox2.AppendText("接收到的信息:"+str+""+DateTime.Now.ToString()+"\r\n");}//publicvoidBeginListen()//开始侦听{Thread.Sleep(10);socket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);IPAddressip1=IPAddress.Parse(IP);iep=newIPEndPoint(ip1,Port);toolStripLabel1.Text=iep.ToString()+"正在侦听...";socket.Bind(iep);socket.Listen(5);Go(socket);}/*if(richTextBox1.InvokeRequired){my=newmydeleget(BeginListen);this.Invoke(my);}else{richTextBox2.Append

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论