网站导航免费论文 原创论文 论文搜索 原创论文 网学软件 学术大家 资料中心 会员中心 问题解答 原创论文 大学论文导航 设计下载 最新论文 下载排行 原创论文 论文源代码
返回网学首页
网学联系
最新论文 推荐专题 热门论文 素材专题
当前位置: 网学 > 编程文档 > ASP.net > 正文

webseveice例子(2)

来源:http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 14/02/27

网学网为需要ASP.net的朋友们搜集整理了webseveice例子(2)相关资料,希望对各位网友有所帮助!


  //Then it sends the Current date time to the Client.
  public void StartListen()
  {
   int iStartPos = 0;
   String sRequest;
   String sDirName;
   String sRequestedFile;
   String sErrorMessage;
   String sLocalDir;
   String sMyWebServerRoot = "C:\\MyWebServerRoot\\";
   String sPhysicalFilePath = "";
   String sFormattedMessage = "";
   String sResponse = "";
   
   
   
   while(true)
   {
    //Accept a new connection
    Socket mySocket = myListener.AcceptSocket() ;
    Console.WriteLine ("Socket Type " +  mySocket.SocketType );
    if(mySocket.Connected)
    {
     Console.WriteLine("\nClient Connected!!\n==================\nCLient IP {0}\n",
      mySocket.RemoteEndPoint) ;
    
     //make a byte array and receive data from the client
     Byte[] bReceive = new Byte[1024] ;
     int i = mySocket.Receive(bReceive,bReceive.Length,0) ;

     
     //Convert Byte to String
     string sBuffer = Encoding.ASCII.GetString(bReceive);
  
     
     //At present we will only deal with GET type
     if (sBuffer.Substring(0,3) != "GET" )
     {
      Console.WriteLine("Only Get Method is supported..");
      mySocket.Close();
      return;
     }
     
     // Look for HTTP request
     iStartPos = sBuffer.IndexOf("HTTP",1);

     // Get the HTTP text and version e.g. it will return "HTTP/1.1"
     string sHttpVersion = sBuffer.Substring(iStartPos,8);
        
                  
     // Extract the Requested Type and Requested file/directory
     sRequest = sBuffer.Substring(0,iStartPos - 1);
        
          
     //Replace backslash with Forward Slash, if Any
     sRequest.Replace("file://\\","/");

     //If file name is not supplied add forward slash to indicate
     //that it is a directory and then we will look for the
     //default file name..
     if ((sRequest.IndexOf(".") <1) && (!sRequest.EndsWith("/")))
     {
      sRequest = sRequest + "/";
     }

     //Extract the requested file name
     iStartPos = sRequest.LastIndexOf("/") + 1;
     sRequestedFile = sRequest.Substring(iStartPos);
     
     //Extract The directory Name
     sDirName = sRequest.Substring(sRequest.IndexOf("/"), sRequest.LastIndexOf("/")-3);
     
      
     
     /////////////////////////////////////////////////////////////////////
     // Identify the Physical Directory
     /////////////////////////////////////////////////////////////////////
     if ( sDirName == "/")
      sLocalDir = sMyWebServerRoot;
     else
     {
      //Get the Virtual Directory
      sLocalDir = GetLocalPath(sMyWebServerRoot, sDirName);
     }

     Console.WriteLine("Directory Requested : " +  sLocalDir);
     //If the physical directory does not exists then
     // dispaly the error message
     if (sLocalDir.Length == 0 )
     {
      sErrorMessage = "<H2>Error!! Requested Directory does not exists</H2><Br>";
      //sErrorMessage = sErrorMessage + "Please check data\\Vdirs.Dat";
      //Format The Message
      SendHeader(sHttpVersion,  "", sErrorMessage.Length, " 404 Not Found", ref mySocket);
      //Send to the browser
      SendToBrowser(sErrorMessage, ref mySocket);
      mySocket.Close();
      continue;
     }
     
     /////////////////////////////////////////////////////////////////////
     // Identify the File Name
     /////////////////////////////////////////////////////////////////////
     //If The file name is not supplied then look in the default file list
     if (sRequestedFile.Length == 0 )
     {
      // Get the default filename
      sRequestedFile = GetTheDefaultFileName(sLocalDir);
      if (sRequestedFile == "")
      {
       sErrorMessage = "<H2>Error!! No Default File Name Specified</H2>";
       SendHeader(sHttpVersion,  "", sErrorMessage.Length, " 404 Not Found", ref mySocket);
       SendToBrowser ( sErrorMessage, ref mySocket);
       mySocket.Close();
       return;
      }
     }
     

     /////////////////////////////////////////////////////////////////////
     // Get TheMime Type
     /////////////////////////////////////////////////////////////////////
     
     String sMimeType = GetMimeType(sRequestedFile);

     //Build the physical path
     sPhysicalFilePath = sLocalDir + sRequestedFile;
     Console.WriteLine("File Requested : " +  sPhysicalFilePath);
     
     
     if (File.Exists(sPhysicalFilePath) == false)
     {
     
      sErrorMessage = "<H2>404 Error! File Does Not Exists...</H2>";
      SendHeader(sHttpVersion, "", sErrorMessage.Length, " 404 Not Found", ref mySocket);
      SendToBrowser( sErrorMessage, ref mySocket);
      Console.WriteLine(sFormattedMessage);
     }
    
     else
     {
      int iTotBytes=0;
      sResponse ="";


      FileStream fs = new FileStream(sPhysicalFilePath, FileMode.Open,  FileAccess.Read, FileShare.Read);
      // Create a reader that can read bytes from the FileStream.
      
      BinaryReader reader = new BinaryReader(fs);
      byte[] bytes = new byte[fs.Length];
      int read;
      while((read = reader.Read(bytes, 0, bytes.Length)) != 0)
      {
       // Read from the file and write the data to the network
       sResponse = sResponse + Encoding.ASCII.GetString(bytes,0,read);
       iTotBytes = iTotBytes + read;
      }
      reader.Close();
      fs.Close();
      SendHeader(sHttpVersion,  sMimeType, iTotBytes, " 200 OK", ref mySocket);
      SendToBrowser(bytes, ref mySocket);
      
      //mySocket.Send(bytes, bytes.Length,0);
     }
     mySocket.Close();      
    }
   }
  }
}
}
  
  • 上一篇资讯: webseveice例子
  • 下一篇资讯: 正确认识“Web服务”
  • 网学推荐

    免费论文

    原创论文

    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号