论文无忧网提供:计算机毕业论文范文|计算机毕业设计|计算机毕业论文
栏目导航 ASP Java Web .NET VB6.0 JAVA VC VF DELPHI PB 计算机网络 计算机科学与技术 PHP 安卓APP 其他 C# 代写论文
当前位置: > 计算机 > 计算机科学与技术 >

基于网络的虚拟实验平台-vlp2p通信库(论文+程序)

4.1.     程序主要功能介绍

copyright paper51.com

主机与客户机连接,客户机向主机发送消息或下载实验文本,主机自行决定客户机下载文本的渠道(通过主机下载或从别的客户机下载)。

paper51.com

4.2.     主要代码介绍 内容来自论文无忧网 www.paper51.com

4.3.1         服务器监听过程主要代码 http://www.paper51.com

        //准许连接,发送欢迎信息 内容来自论文无忧网 www.paper51.com

        private void Accept(IAsyncResult iar) { copyright paper51.com

           Socket Remote = (Socket)iar.AsyncState; copyright paper51.com

           clientS[i] = (Socket)Remote.EndAccept(iar);

内容来自www.paper51.com

           //服务器接受由客户端传来的UDP包,用于记录客户端的UDP端口

内容来自论文无忧网 www.paper51.com

           UDPS.ReceiveFrom(Data, ref RemoteEP);

http://www.paper51.com

           //异步发送欢迎消息 http://www.paper51.com

           clientS[i].BeginSend(Message, 0, Message.Length, SocketFlags.None, new AsyncCallback(Send), clientS[i]); }

内容来自www.paper51.com

4.3.2         客户端连接过程主要代码

内容来自www.paper51.com

private voidBTNConnect_Click(object sender, EventArgs e){ 内容来自www.paper51.com

            Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); http://www.paper51.com

           //S1为服务器IP,S2为服务器端口 paper51.com

               P = new IPEndPoint(IPAddress.Parse(NewF.S1.ToString()), Int32.Parse(NewF.S2.ToString()));

内容来自www.paper51.com

           //异步连接开始

copyright paper51.com

           sock.BeginConnect(P, new AsyncCallback(Connected), sock); } 内容来自www.paper51.com

        void Connected(IAsyncResultiar)    { copyright paper51.com

           Client = (Socket)iar.AsyncState;

http://www.paper51.com

               Client.EndConnect(iar); copyright paper51.com

           //向服务器发送一个UDP包,服务器可由此包记录客户端的UDP端口,为P2P打洞作准备 http://www.paper51.com

               UDPClient.SendTo(Data, SUDPIp); 内容来自论文无忧网 www.paper51.com

           //异步接收从服务器传来的欢迎信息,用来检测连接是否成功 copyright paper51.com

                   Client.BeginReceive(Data, 0, Data.Length, SocketFlags.None,new AsyncCallback(Receive),Client); }

内容来自www.paper51.com

4.3.3         下载实验文本(客户端主要代码) http://www.paper51.com

//下载实验

内容来自www.paper51.com

void DownLoad(){

内容来自www.paper51.com

           RCount = Client.Receive(Data, SocketFlags.None);

copyright paper51.com

           string String = Encoding.Unicode.GetString(Data,0, RCount); 内容来自www.paper51.com

           string StringData = String;

paper51.com

           while (StringData[StringData.Length - 1]!= '\0'){ 内容来自www.paper51.com

               RCount = Client.Receive(Data, SocketFlags.None);

paper51.com

               String = Encoding.Unicode.GetString(Data,0, RCount); http://www.paper51.com

               StringData += String; }} copyright paper51.com

4.3.4         下载实验文本(服务器端主要代码) 内容来自www.paper51.com

//异步接收的回调函数 内容来自论文无忧网 www.paper51.com

        private void Receive(IAsyncResult iar){

内容来自www.paper51.com

           Socket client = (Socket)iar.AsyncState;

paper51.com

           string RevData = Encoding.Unicode.GetString(Data, 0, rev); http://www.paper51.com

               //\0为客户端传来的下载实验信号

paper51.com

               if (RevData[0] == '\0') copyright paper51.com

               StreamReader Sreamder = new StreamReader(openFileDialog1.FileName,Encoding.GetEncoding("GB2312"));

copyright paper51.com

                      StringData = Sreamder.ReadToEnd(); http://www.paper51.com

                       client.BeginSend(Data, 0, Data.Length, SocketFlags.None,new AsyncCallback(Send),client);} copyright paper51.com

4.3.5         P2P下载实验文本(客户端主要代码) http://www.paper51.com

           //异步接收函数的回调函数(P2P数据发送者)

内容来自www.paper51.com

void Receive(IAsyncResultiar){ 内容来自www.paper51.com

           Socket remote = (Socket)iar.AsyncState; http://www.paper51.com

               rev = remote.EndReceive(iar);

copyright paper51.com

           string S = Encoding.Unicode.GetString(Data,0, rev); copyright paper51.com

           //\t为服务端传来的p2p发送信号

内容来自论文无忧网 www.paper51.com

           if (S[0] == '\t'){ 内容来自www.paper51.com

           //解析对方客户机的IP和端口号 内容来自论文无忧网 www.paper51.com

                   S = S.Remove(0, 1);

内容来自论文无忧网 www.paper51.com

                   StringBuilder SB = new StringBuilder(); http://www.paper51.com

                   while (S[0] != ':'){

内容来自论文无忧网 www.paper51.com

                       SB.Append(S[0].ToString());

内容来自www.paper51.com

                       S = S.Remove(0, 1);} 内容来自www.paper51.com

                   S = S.Remove(0, 1); copyright paper51.com

        IPEndPoint I = newIPEndPoint(IPAddress.Parse(SB.ToString()),Int32.Parse(S)); paper51.com

                   StreamReader SR = new StreamReader(Path,Encoding.GetEncoding("gb2312")); copyright paper51.com

UDPClient.SendTo(Data, 0, Data.Length, SocketFlags.None, (EndPoint)I);}

copyright paper51.com

void DownLoad(){

http://www.paper51.com

           //\0为下载实验信号

paper51.com

           string M = "\0"; 内容来自论文无忧网 www.paper51.com

           meg = Encoding.Unicode.GetBytes(M); copyright paper51.com

           Client.Send(meg, 0, meg.Length, SocketFlags.None);

内容来自论文无忧网 www.paper51.com

           NS = (FileStream)saveFileDialog1.OpenFile(); 内容来自论文无忧网 www.paper51.com

           StreamWriter SW = new StreamWriter(NS);

内容来自论文无忧网 www.paper51.com

           //接收文件,采用同步阻塞方式

paper51.com

           RCount = Client.Receive(Data, SocketFlags.None); 内容来自www.paper51.com

           string String = Encoding.Unicode.GetString(Data,0, RCount);

copyright paper51.com

           string StringData = String; 内容来自论文无忧网 www.paper51.com

           while (StringData[StringData.Length - 1]!= '\0'){ 内容来自www.paper51.com

               RCount = Client.Receive(Data, SocketFlags.None);

copyright paper51.com

               String = Encoding.Unicode.GetString(Data,0, RCount); paper51.com

               StringData += String; }

内容来自www.paper51.com

           //\t为服务端传来的p2p打洞信号, copyright paper51.com

           //解析对方客户机的IP和端口号

paper51.com

                   StringData = StringData.Remove(0, 1);

内容来自www.paper51.com

                   StringBuilder SB = new StringBuilder(); 内容来自www.paper51.com

                   while (StringData[0] != ':') {

paper51.com

                       SB.Append(StringData[0].ToString());

paper51.com

                       StringData = StringData.Remove(0, 1);

内容来自论文无忧网 www.paper51.com

                   StringData = StringData.Remove(0, 1); 内容来自www.paper51.com

                   IPEndPoint I = newIPEndPoint(IPAddress.Parse(SB.ToString()),Int32.Parse(StringData));

内容来自论文无忧网 www.paper51.com

                   EndPoint E = (EndPoint)I; http://www.paper51.com

                   byte[] B = newbyte[2]; 内容来自论文无忧网 www.paper51.com

           //向P2P信息发送者打洞

paper51.com

                   UDPClient.SendTo(B, 0, B.Length, SocketFlags.None,(EndPoint)I);

paper51.com

           //挂起线程2秒钟,等待数据到达

内容来自www.paper51.com

                   Thread.Sleep(2000);

内容来自www.paper51.com

                           String = Encoding.Unicode.GetString(Data,0, RCount); } 内容来自www.paper51.com

                   SW.WriteLine(String); }

内容来自论文无忧网 www.paper51.com

           Client.BeginReceive(Data, 0, 1024, SocketFlags.None,new AsyncCallback(Receive),Client); } http://www.paper51.com

------分隔线----------------------------
联系方式