Program.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:1k
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net.Sockets;
- namespace TimeServer
- {
- class Program
- {
- private const int portNum = 13;
- static void Main(string[] args)
- {
- bool done = false;
- System.Net.IPAddress local= System.Net.IPAddress.Parse("127.0.0.1");
- TcpListener listener = new TcpListener(local,portNum);
- listener.Start();
- while (!done)
- {
- Console.Write("Waiting for connection...");
- TcpClient client = listener.AcceptTcpClient();
- Console.WriteLine("Connection accepted.");
- NetworkStream ns = client.GetStream();
- byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());
- try
- {
- ns.Write(byteTime, 0, byteTime.Length);
- ns.Close();
- client.Close();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- listener.Stop();
- }
- }
- }