этот сервер открыт, иначе никогда не будет запущена служба
protected override void OnStart(string[] args)
{
TcpServer server=new TcpServer();
server.Start();
}
class TcpServer
{
// clase prar crear un listener
private TcpListener server;
private bool isRunning;
private int port = 13000;
public void Start()
{
// client found.
// create a thread to handle communication
Thread tServer = new Thread(new ParameterizedThreadStart(StartThread));
tServer.Start();
}
public void StartThread(object o)
{
//iniciar configuraciones
Configuracion.init();
// crear un nuevo servidor
server = new TcpListener(IPAddress.Any, port);
//inicializar el servidor
server.Start();
//variable para indicar queesta corriendo el server
isRunning = true;
LoopClients();//thread
}}
public void LoopClients()
{
while (isRunning)
{
// wait for client connection
TcpClient newClient = server.AcceptTcpClient();
// client found.
// create a thread to handle communication
Thread t = new Thread(new ParameterizedThreadStart(HandleClient));
t.Start(newClient);
}
}