⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch8_12.cs

📁 《c#技术内幕代码》
💻 CS
字号:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

class CH8_12
{
   public static void Main(String[] args)
   {
       Encoding ASCII = Encoding.ASCII;
	
       // Create a listener on port 8227
       TCPListener myListener = new TCPListener(8227);

       // start the listener
       myListener.Start();
       Console.WriteLine("Waiting for request");

       // Program blocks on Accept() until a client connects
       Socket mySocket = myListener.Accept();
       Console.WriteLine("Got a request");

       Byte[] RecvBytes = new Byte[256];

       // Receive the page, loop until all bytes are received
       Int32 bytes = mySocket.Receive(RecvBytes, RecvBytes.Length, 0);
       string s = ASCII.GetString(RecvBytes, 0, bytes);
       Console.WriteLine("Socket Request: [{0}]", s );
       
       if ( s == "GetDate" ) 
       {
          // Get current date and time
          DateTime now = DateTime.Now;
          String strDateLine = now.ToShortDateString() + " " + now.ToLongTimeString();

          // Convert to byte array and send
          Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes(strDateLine.ToCharArray());
          mySocket.Send(byteDateLine,byteDateLine.Length,0);
       }
       else
       {
          Console.WriteLine("Unknown command: {0}", s );
       }
       

       myListener.Stop();   
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -