Program.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:1k
源码类别:

Email服务器

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net.Sockets;
  5. namespace TimeServer
  6. {
  7.     class Program
  8.     {
  9.         private const int portNum = 13;
  10.         static void Main(string[] args)
  11.         {
  12.             bool done = false;
  13.             System.Net.IPAddress local=  System.Net.IPAddress.Parse("127.0.0.1");
  14.             TcpListener listener = new TcpListener(local,portNum);
  15.             listener.Start();
  16.             while (!done)
  17.             {
  18.                 Console.Write("Waiting for connection...");
  19.                 TcpClient client = listener.AcceptTcpClient();
  20.                 Console.WriteLine("Connection accepted.");
  21.                 NetworkStream ns = client.GetStream();
  22.                 byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());
  23.                 try
  24.                 {
  25.                     ns.Write(byteTime, 0, byteTime.Length);
  26.                     ns.Close();
  27.                     client.Close();
  28.                 }
  29.                 catch (Exception e)
  30.                 {
  31.                     Console.WriteLine(e.ToString());
  32.                 }
  33.             }
  34.             listener.Stop();
  35.         }
  36.     }
  37. }