DateServer
class DateServer
{
private TcpListener myListener;
private int port = 8001;
public DateServer()
{
try
{
//start listing on the given port
myListener = new TcpListener(port);
myListener.Start();
Console.WriteLine("Server Ready - Listining for new Connections ...");
//start the thread which calls the method SatrtListen
Thread th = new Thread(new ThreadStart(StartListen));
th.Start();
}
catch (Exception e)
{
Console.WriteLine("An Exception Occured while Listing :" + e.ToString());
}
}
public static void Main11(String[] argv)
{
DateServer dts = new DateServer();
}
public void StartListen()
{
while (true)
{
//Accept a new connection
Socket mySocket = myListener.AcceptSocket();
if (mySocket.Connected)
{
Console.WriteLine("Client Connected!!");
//make a byte array and receive data from the client
Byte[] receive = new Byte[64];
int i = mySocket.Receive(receive, receive.Length, 0);
char[] unwanted = { ' ', ' ', ' ' };
string rece = System.Text.Encoding.ASCII.GetString(receive);
Console.WriteLine(rece.TrimEnd(unwanted));
//get the current date/time and convert it to string
DateTime now = DateTime.Now;
String strDateLine = "Server: The Date/Time Now is: " + now.ToShortDateString() + " " + now.ToShortTimeString();
// Convert to byte array and send
Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes(strDateLine.ToCharArray());
mySocket.Send(byteDateLine, byteDateLine.Length, 0);
}
}
}
}
posted on 2011-11-15 22:21 breakpoint 阅读(96) 评论(0) 编辑 收藏 举报