1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
namespace SaurabhNet
{
using System;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Configuration;
using System.IO;
using DAO.Services;
using entities;
//Import the necessary Namespaces
//Class which shows the implementation of the TCP DateTime server
public class DateServer
{
private TcpListener myListener;
private int port = 4554;
//The constructor which make the TcpListener start listening on the
//given port.
//It also calls a Thread on the method StartListen().
public DateServer()
{
try
{
IPAddress ip = IPAddress.Parse("10.63.36.78");
//start listing on the given port
myListener = new TcpListener(ip, port);
myListener.Start();
Console.WriteLine("Server Ready -Listening for new Connections ");
//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());
}
}
//main entry point of the class
public static void Main(String[] argv)
{
DateServer dts = new DateServer();
}
//This method Accepts new connection and
//First it receives the welcome massage from the client,
//Then it sends the Current date time to the Client.
public void StartListen()
{
while (true)
{
Socket soc = myListener.AcceptSocket();
if (soc.Connected)
{
Console.WriteLine("Client Connected!!");
Console.WriteLine("Connected: {0}", soc.RemoteEndPoint);
try
{
Stream s = new NetworkStream(soc);
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true; // enable automatic flushing
while (true)
{
string name = sr.ReadLine();
if (name == "" || name == null) break;
sw.WriteLine("recu du client" + name);
}
//s.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Disconnected: {0}",
soc.RemoteEndPoint);
soc.Close();
}
}
}
}
} |