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

webseveice例子

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

鉴于大家对ASP.net十分关注,我们编辑小组在此为大家搜集整理了“webseveice例子”一文,供大家参考学习!

namespace Imtiaz
{
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading ;

class MyWebServer
{
  private TcpListener myListener ;
  private int port = 5050 ; // Select any free port you wish
  //The constructor which make the TcpListener start listening on the
  //given port. It also calls a Thread on the method StartListen().
  public MyWebServer()
  {
   try
   {
    //start listing on the given port
    myListener = new TcpListener(port) ;
    myListener.Start();
    Console.WriteLine("Web Server Running... Press ^C to Stop...");
    //start the thread which calls the method ''StartListen''
    Thread th = new Thread(new ThreadStart(StartListen));
    th.Start() ;
   }
   catch(Exception e)
   {
    Console.WriteLine("An Exception Occurred while Listening :" +e.ToString());
   }
  }

  /// <summary>
  /// Returns The Default File Name
  /// Input : WebServerRoot Folder
  /// Output: Default File Name
  /// </summary>
  /// <param name="sMyWebServerRoot"></param>
  /// <returns></returns>
  public string GetTheDefaultFileName(string sLocalDirectory)
  {
   StreamReader sr;
   String sLine = "";
   try
   {
    //Open the default.dat to find out the list
    // of default file
    sr = new StreamReader("data\\Default.Dat");
    while ((sLine = sr.ReadLine()) != null)
    {
     //Look for the default file in the web server root folder
     if (File.Exists( sLocalDirectory + sLine) == true)
      break;
    }
   }
   catch(Exception e)
   {
    Console.WriteLine("An Exception Occurred : " + e.ToString());
   }
   if (File.Exists( sLocalDirectory + sLine) == true)
    return sLine;
   else
    return "";
  }

  /// <summary>
  /// This function takes FileName as Input and returns the mime type..
  /// </summary>
  /// <param name="sRequestedFile">To indentify the Mime Type</param>
  /// <returns>Mime Type</returns>
  public string GetMimeType(string sRequestedFile)
  {
   
   StreamReader sr;
   String sLine = "";
   String sMimeType = "";
   String sFileExt = "";
   String sMimeExt = "";
   
   // Convert to lowercase
   sRequestedFile = sRequestedFile.ToLower();
   
   int iStartPos = sRequestedFile.IndexOf(".");
   sFileExt = sRequestedFile.Substring(iStartPos);
   
   try
   {
    //Open the Vdirs.dat to find out the list virtual directories
    sr = new StreamReader("data\\Mime.Dat");
    while ((sLine = sr.ReadLine()) != null)
    {
     sLine.Trim();
     if (sLine.Length > 0)
     {
      //find the separator
      iStartPos = sLine.IndexOf(";");
      
      // Convert to lower case
      sLine = sLine.ToLower();
      
      sMimeExt = sLine.Substring(0,iStartPos);
      sMimeType = sLine.Substring(iStartPos + 1);
      
      if (sMimeExt == sFileExt)
       break;
     }
    }
   }
   catch (Exception e)
   {
    Console.WriteLine("An Exception Occurred : " + e.ToString());
   }
   if (sMimeExt == sFileExt)
    return sMimeType;
   else
    return "";
  }

  /// <summary>
  /// Returns the Physical Path
  /// </summary>
  /// <param name="sMyWebServerRoot">Web Server Root Directory</param>
  /// <param name="sDirName">Virtual Directory </param>
  /// <returns>Physical local Path</returns>
  public string GetLocalPath(string sMyWebServerRoot, string sDirName)
  {
   StreamReader sr;
   String sLine = "";
   String sVirtualDir = "";
   String sRealDir = "";
   int iStartPos = 0;
   

   //Remove extra spaces
   sDirName.Trim();
   
   
   
   // Convert to lowercase
   sMyWebServerRoot = sMyWebServerRoot.ToLower();
   // Convert to lowercase
   sDirName = sDirName.ToLower();
   //Remove the slash
   //sDirName = sDirName.Substring(1, sDirName.Length - 2);

   try
   {
    //Open the Vdirs.dat to find out the list virtual directories
    sr = new StreamReader("data\\VDirs.Dat");
    while ((sLine = sr.ReadLine()) != null)
    {
     //Remove extra Spaces
     sLine.Trim();
     if (sLine.Length > 0)
     {
      //find the separator
      iStartPos = sLine.IndexOf(";");
      
      // Convert to lowercase
      sLine = sLine.ToLower();
      
      sVirtualDir = sLine.Substring(0,iStartPos);
      sRealDir = sLine.Substring(iStartPos + 1);
      
      if (sVirtualDir == sDirName)
      {
       break;
      }
     }
    }
   }
   catch(Exception e)
   {
    Console.WriteLine("An Exception Occurred : " + e.ToString());
   }

   Console.WriteLine("Virtual Dir : " + sVirtualDir);
   Console.WriteLine("Directory   : " + sDirName);
   Console.WriteLine("Physical Dir: " + sRealDir);
   if (sVirtualDir == sDirName)
    return sRealDir;
   else
    return "";
  }
  

  /// <summary>
  /// This function send the Header Information to the client (Browser)
  /// </summary>
  /// <param name="sHttpVersion">HTTP Version</param>
  /// <param name="sMIMEHeader">Mime Type</param>
  /// <param name="iTotBytes">Total Bytes to be sent in the body</param>
  /// <param name="mySocket">Socket reference</param>
  /// <returns></returns>
  public void SendHeader(string sHttpVersion, string sMIMEHeader, int iTotBytes, string sStatusCode, ref Socket mySocket)
  {
   String sBuffer = "";
   
   // if Mime type is not provided set default to text/html
   if (sMIMEHeader.Length == 0 )
   {
    sMIMEHeader = "text/html";  // Default Mime Type is text/html
   }
   sBuffer = sBuffer + sHttpVersion + sStatusCode + "\r\n";
   sBuffer = sBuffer + "Server: cx1193719-b\r\n";
   sBuffer = sBuffer + "Content-Type: " + sMIMEHeader + "\r\n";
   sBuffer = sBuffer + "Accept-Ranges: bytes\r\n";
   sBuffer = sBuffer + "Content-Length: " + iTotBytes + "\r\n\r\n";
   
   Byte[] bSendData = Encoding.ASCII.GetBytes(sBuffer);
   SendToBrowser( bSendData, ref mySocket);
   Console.WriteLine("Total Bytes : " + iTotBytes.ToString());
  }

  /// <summary>
  /// Overloaded Function, takes string, convert to bytes and calls
  /// overloaded sendToBrowserFunction.
  /// </summary>
  /// <param name="sData">The data to be sent to the browser(client)</param>
  /// <param name="mySocket">Socket reference</param>
  public void SendToBrowser(String sData, ref Socket mySocket)
  {
   SendToBrowser (Encoding.ASCII.GetBytes(sData), ref mySocket);
  }

  /// <summary>
  /// Sends data to the browser (client)
  /// </summary>
  /// <param name="bSendData">Byte Array</param>
  /// <param name="mySocket">Socket reference</param>
  public void SendToBrowser(Byte[] bSendData, ref Socket mySocket)
  {
   int numBytes = 0;
   
   try
   {
    if (mySocket.Connected)
    {
     if (( numBytes = mySocket.Send(bSendData, bSendData.Length,0)) == -1)
      Console.WriteLine("Socket Error cannot Send Packet");
     else
     {
      Console.WriteLine("No. of bytes send {0}" , numBytes);
     }
    }
    else
     Console.WriteLine("Connection Dropped....");
   }
   catch (Exception  e)
   {
    Console.WriteLine("Error Occurred : {0} ", e );
       
   }
  }

  // Application Starts Here..
  public static void Main()
  {
   MyWebServer MWS = new MyWebServer();
  }

  //This method Accepts new connection and
  //First it receives the welcome massage from the client,
   
  • 上一篇资讯: WEB服务有待实践的检验
  • 下一篇资讯: webseveice例子(2)
  • 网学推荐

    免费论文

    原创论文

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