Good day.

I wrote Vin.service, which with the help of the TcpListener class listens to port 25, intercepts mail and sends SMS. The situation is as follows: after we have received one message, the following are no longer intercepted. Can I close the client session incorrectly?

"OnStart":

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

My SmtpHelper class:

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

Maybe what object should be closed? it turns out that after the first message was received, others are not caught.

Thank!

  • And what does the debugger say? - Pavel Mayorov
  • @PavelMayorov, the question is - did the client implement the connection mechanism to the SMTP correctly? In outline. It turns out that the mi accepted one message, and the following ones are already accepted - Leonard Bertone
  • Look at the debugger where your program is stuck after receiving the first message - Pavel Mayorov

1 answer 1

You have a little broken the logic of work. You do everything in one thread and if two clients connect to you at once, it is not known how the program will behave. I would do it like this:

 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 turns out that when a client connects to port 25, it is automatically accepted and added to the thread pool (by default, the pool size is like 1023 threads). And further, in a separate thread you work with this client. If another client connects at this time, another independent stream will be created for it.

  • Your 'Listener` field is static and initialized in the instance constructor. Don't do that. - Pavel Mayorov
  • Yes) I first registered it in the main class, and then moved it and did not correct it. Changed. - Vadim Pavlovich