如何在windows服务c中设置tcplistener

本文关键字:设置 tcplistener 服务 windows | 更新日期: 2023-09-27 18:29:16

我是windows服务的初学者,我有一个控制台应用程序来在客户端和服务器之间传输文件。现在我想把我的服务器应用程序变成windows服务,用于监听多个客户端。我将控制台应用程序代码放在onstart()中。部署windows服务后,无法工作或侦听。任何智商。。。

如何在windows服务c中设置tcplistener

这是一篇关于windows服务中TCP侦听器的完整文章。虽然它很旧,但可能会有所帮助。

hi我知道这个问题由来已久,我在stackoverflow或其他网站上查看了许多文章,但没有人让我满意。最头疼的一点是:它是一个windows服务,当服务启动时,监听器的while循环不应该被阻塞。

最后,我自己制定了一个解决方案,也许它对系统不友好,但它非常简单,运行良好(我同时打开了3个到该服务器的telnet,它按我的意愿运行):-)

这是代码:

`protected override void OnStart(string[] args)
{
        tcpServerStart(); 
}
private void tcpServerStart()
{
    try
    {
        IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
        //port 5555, or any port number you want
        listener = new TcpListener(ipAddress, 5555);  
        listener.Start();

        var whileThread = new Thread(() =>
        {
            while (true)
            {
                // in order to avoid while loop turn into an infinite loop, 
                // we have to use AcceptTcpClient() to block it.
                TcpClient client = listener.AcceptTcpClient(); 

                // for each connection we just fork a thread to handle it.
                var childThread = new Thread(() =>
                {
                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream(); // not blocking call
                    StreamReader streamreader = new StreamReader(client.GetStream(), Encoding.ASCII);
                    string line = null;
                    // below while loop is your logic code, change it to your needs.
                    // defined "<EOF>" as mine quit message 
                    while ((line = streamreader.ReadLine()) != "<EOF>") 
                    {
                     // WriteToFile is a function of mineto log system status
                        WriteToFile(line); 
                    }
                    stream.Close();
                    client.Close();
                });
                childThread.Start();
            } // end of while(true) loop
        });
        whileThread.Start();
    }
    catch (Exception e)
    {
    }
}  '  

此服务器打开线程,否则从不运行服务

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);
        }
    }