如何关闭此TCP连接? [英] How do to close this TCP connection ?

查看:216
本文介绍了如何关闭此TCP连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,但我似乎无法过去退出{enter}。请有人指出我需要做什么吗?

I have the following code, but I do not seem to be able to past quit{enter} to it. please could someone point out what i need to do ?

过去两天我一直在谷歌,我似乎无法找到答案。

I have spent the last two days google, and I do not seem to be able to find the answer .

       static void Main(string[] args)
        {
            var server = "telehack.com";
            var port = 23;
            String path = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Documents\telnetoutput.txt");

            Console.WriteLine("Testing {0} is at end of port {1}", server, port);
            try {
                using (var client = new TcpClient())
                {
                    client.Connect(server, port);
                    using (var stream = client.GetStream())
                    {
                         using (var reader = new StreamReader(stream))
                        {
                            string tcpresponce = reader.ReadLine();

                            if (tcpresponce == null)
                            {
                                failed(tcpresponce);
                            }
                            else
                            {
                                TalkToPABX();
                            }
                        }
                    }
                    
                }



                void TalkToPABX()
                {
                    System.IO.File.Delete(path);
                    Console.WriteLine("SUCCESS..." + Environment.NewLine);
                    Console.WriteLine("connecting to {0} via port {1} " , server , port);
                    TelnetConnection tc = new TelnetConnection(server, port);

                    //// while connected
                    while (tc.IsConnected)
                    {
                        // display server output
                        Console.Write(tc.Read());
                        System.IO.File.AppendAllText(path, tc.Read());
                        

                        // send client input to server
						string prompt = "quit\n";   <====
						Console.WriteLine(prompt);  <====

                    }
                }

                void failed(string tcpresponce)
                {
                    Console.WriteLine("Unable to make connection to the host. Error received reads {0}", tcpresponce);
                    Console.WriteLine("Press ENTER to close this app");
                    Console.ReadLine();
                }
            }
			
            catch (System.Net.Sockets.SocketException msg)
            {
                Console.WriteLine("Whoops. Error reads" + Environment.NewLine + msg);
                Console.ReadLine();
            }


        }




谢谢 


thank you 

推荐答案

我可以看到你正在使用'

I can see you are using '

using (var client = new TcpClient()){}

打开TcpClient,TCPClient实现了IDisposable接口(否则你无法使用由IDisposable实现的使用块),因此当你从'using'块出去时它将关闭TcpClient()实例。  ;

to open the TcpClient, TCPClient has IDisposable interface implemented (or else you are not able to use using block, which was implemented by IDisposable), Therefore when you go out from the 'using' block it will close TcpClient() instance. 


这篇关于如何关闭此TCP连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆