C# + TcpListener



  • Good afternoon.

    I wrote a vin. a service that, with TcpListener class, listens to 25 ports, intercepts and textiles. The situation is as follows: After we've accepted one message, the following are no longer intercepted. Maybe I'm blocking the wrong customer session.

    "OnStart"

         Thread listen = new Thread(new ThreadStart(s.Listen));
         listen.Start();
    

    My class SmtpHelper:

    SMTP_Listener = new TcpListener(IPAddress.Any, port);
    SMTP_Listener.Start();
    
    while (true)
    {
                        clientSocket = SMTP_Listener.AcceptSocket();
                        _sessionId = clientSocket.GetHashCode().ToString();
                       _email.sessionId = Convert.ToInt32(_sessionId);
    
                        StartProcessing();
    }
    

    Next, StartProcessing() :

     m_ConnectedIp = ParseIP_from_EndPoint(clientSocket.RemoteEndPoint.ToString());
    m_ConnectedHostName = GetHostName(m_ConnectedIp);

                l.WriteEvent(String.Format("Клиент {0}: m_ConnectedIp = {1}, m_ConnectedHostName = {2}", _sessionId, m_ConnectedIp,
                    m_ConnectedHostName));
    
                _email.ip = m_ConnectedIp;
                _email.port = 25;
    
                if (clientSocket.Connected)
                {
                    l.WriteEvent(">>>Socket connected");
                }
                else
                {
                    l.WriteEvent("<<<Socket NOT connected");
                }
    
                SendData("220 " + System.Net.Dns.GetHostName() + " Service ready\r\n");
    
                //РАБОТА С ВХОДНЫМИ ДАННЫМИ
                while (true)
                {
                    //если есть данные, то считаем их
                    if (clientSocket.Available > 0)
                    {
                        string lastCmd = ReadLine();
                        l.WriteEvent("lastCmd: " + lastCmd);
    
                        //break; // добавил
    
                        //парсим команду
                        ProceedCommand(lastCmd);
                    }
                }
    

    What kind of object do you want to close?

    Thank you!



  • Your work logic has been compromised. You're doing everything in the same flow and if you have two customers on the line, you don't know how the program will lead itself. I'd do it like:

    public class YourMainProgram
    {
        public static void Main()
        {
            //... основная программа 
            new Server(25);
            //... основная программа 
        }
    }
    

    public class StartProcessing
    {
    public StartProcessing(System.Net.Sockets.TcpClient Client)
    {
    /* работа с подключением. Например:
    l.WriteEvent(String.Format("Мы подключили клиента"));
    ......
    l.WriteEvent(String.Format("Мы отключили клиента"));
    Client.Close();
    */
    }
    }
    public class Server
    {
    System.Net.Sockets.TcpListener Listener;
    public Server(int Port)
    {
    Listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, Port);
    Listener.Start();
    while (true)
    {
    System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ClientThread), Listener.AcceptTcpClient());
    }
    }
    public static void ClientThread(Object StateInfo)
    {
    new StartProcessing((System.Net.Sockets.TcpClient)StateInfo);
    }
    }

    It appears that when a client is connected to 25 ports, it is automatically accepted and added to a pool of fluxes (by default, the size of the pool is 1023 flow). And further, in a separate flow, you work with this client. If another client is involved at this time, another independent flow will be created.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2