简体   繁体   English

C#-C#服务器用作控制台应用程序。 不能用作Web应用程序

[英]C# - C# Server works as Console Application. Does not work as Web Application

I have the following code for a server: 我有以下服务器代码:

namespace Test
{
    public class TCPListener
    {
        public static void Main()
        {
            TcpListener listener = null;
            TcpClient client = null;
            NetworkStream stream = null;
            BinaryWriter writer = null;

            try
            {
                listener = new TcpListener(new IPAddress(new byte[] { 127, 0, 0, 1 }), 5000);
                listener.Start();

                while (true)
                {
                    using (client = listener.AcceptTcpClient())
                    {
                        using (stream = client.GetStream())
                        {    
                            writer = new BinaryWriter(stream);
                            writer.Write("The message was received!");
                        }
                    }
                }
            }
            catch (SocketException)
            {
                //Catch socket exception
            }
        }
    }
}

Now, if I put this code in a console application and use telnet, I receive "The message was received" message on the command prompt. 现在,如果我将此代码放在控制台应用程序中并使用telnet, "The message was received"在命令提示符下会收到"The message was received"消息"The message was received"消息。 Now, I copied and pasted this code (changing the namespace) into a web application as a class. 现在,我将该代码(更改名称空间)作为类复制并粘贴到Web应用程序中。 The web application solution was deployed on port 5000 . Web应用程序解决方案已部署在port 5000 Apart from the server, it also contains a number of pages which the user can browse. 除服务器外,它还包含许多用户可以浏览的页面。

Unfortunately, if I go to telnet and type "telnet 127.0.0.1 5000" , a connection is achieved, however I don't receive anything. 不幸的是,如果我转到telnet并输入"telnet 127.0.0.1 5000" ,则可以建立连接,但是我什么也没收到。 What do you think is the problem? 您认为出了什么问题? How can I solve it? 我该如何解决?

Mathew, 马修,

As aluded to by the emeritus mr skeet, you are only catching half the equation. skeet先生退休后,您只掌握了一半的方程式。 You need to then instantiate the class and call the Main() method. 然后,您需要实例化该类并调用Main()方法。

Here's how you could do that: 这是您可以执行的操作:

TCPListener tcpListener = new TCPListener(); 
tcpListener.Main();

Try that in your main point of setup in your web based solution (controller action or page load code behind). 尝试在基于Web的解决方案中进行设置的主要点(控制器操作或后面的页面加载代码)。 Also, change the method signature on Main (remove the static). 同样,在Main上更改方法签名(删除静态)。

Also, you seem to be calling your class inside itself: 另外,您似乎在内部调用类:

listener = new TcpListener();

That is gonna cause big problems... I would suggest a little refactoring and a rethink about how Main works. 那会引起很大的问题...我建议您进行一些重构,并对Main工作原理进行重新思考。 Your 1st major win would be to rename the example class above away from TCPListener, to something else as there appears to be a collision going on there. 您的第一个主要胜利是将上面的示例类重命名为远离TCPListener,以其他方式命名,因为那里似乎发生了冲突。

[EDIT] To save reading the comments below. [编辑]要保存阅读下面的评论。 The final solution that Matthew took was to go back to the console solution and invoke it from the webapp. Matthew采取的最终解决方案是回到控制台解决方案并从Web应用程序中调用它。 This way he scored a couple of wins: 这样,他就赢得了几场胜利:

  • Existing console app had been tested and worked 现有的控制台应用程序已经过测试和运行
  • Looser coupling of moving parts 活动部件的联轴器松动
  • Extensibility to use in other projects 在其他项目中使用的可扩展性
  • Ability to implement and move onto other time sensitive elements of project 能够实施并转移到项目的其他时间敏感元素

The takeaway here is, "always examine the options and think beyond a single solution to solve a problem" 这里的要点是:“始终检查选项,并思考超越解决问题的单一解决方案”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM