当前位置: 首页>>代码示例>>C#>>正文


C# System.Net.Sockets.TcpClient.Close方法代码示例

本文整理汇总了C#中System.Net.Sockets.TcpClient.Close方法的典型用法代码示例。如果您正苦于以下问题:C# System.Net.Sockets.TcpClient.Close方法的具体用法?C# System.Net.Sockets.TcpClient.Close怎么用?C# System.Net.Sockets.TcpClient.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Sockets.TcpClient的用法示例。


在下文中一共展示了System.Net.Sockets.TcpClient.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TryPrint

        public void TryPrint(string zplCommands)
        {
            try
            {
                // Open connection
                System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
                client.Connect(this.m_IPAddress, this.m_Port);

                // Write ZPL String to connection
                System.IO.StreamWriter writer = new System.IO.StreamWriter(client.GetStream());
                writer.Write(zplCommands);
                writer.Flush();

                // Close Connection
                writer.Close();
                client.Close();
            }
            catch (Exception ex)
            {
                this.m_ErrorCount += 1;
                if(this.m_ErrorCount < 10)
                {
                    System.Threading.Thread.Sleep(5000);
                    this.TryPrint(zplCommands);
                }
                else
                {
                    throw ex;
                }
            }
        }
开发者ID:ericramses,项目名称:YPILIS,代码行数:31,代码来源:ZPLPrinter.cs

示例2: button1_Click

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            System.Text.Encoding enc = System.Text.Encoding.UTF8;

            string host = textBox2.Text.Trim();
            int port = int.Parse(textBox3.Text);
            try
            {
                System.Net.Sockets.TcpClient tcp =
                  new System.Net.Sockets.TcpClient(host, port);

                System.Net.Sockets.NetworkStream ns = tcp.GetStream();

                string sendMsg = textBox1.Text;
                if (sendMsg == "" || textBox1.Text.Length > 140)
                {
                    textBlock1.Text = "ツイートできる条件を満たしてません!!";
                    tcp.Close();
                    return;
                }

                byte[] sendBytes = enc.GetBytes(sendMsg + '\n');
                ns.Write(sendBytes, 0, sendBytes.Length);

                //TCPソケットサーバーからストリームが送られてくる場合に受け取れるように
                if (checkBox1.IsChecked == true) {
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                     byte[] resBytes = new byte[256];
                     int resSize;
                     do
                     {
                         resSize = ns.Read(resBytes, 0, resBytes.Length);
                         if (resSize == 0)
                         {
                             textBlock1.Text = "サーバーが切断しました。";
                             return;
                         }
                         ms.Write(resBytes, 0, resSize);
                     } while (ns.DataAvailable);//TODO:RubyのTCPSocketのputsメソッドに対応していない
                     string resMsg = enc.GetString(ms.ToArray());

                    textBlock1.Text = resMsg;
                    ms.Close();
                }
                ns.Close();
                tcp.Close();
                textBlock1.Text = host + "のmikutterでつぶやきました。";
                textBox1.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(),"Error");
                Environment.Exit(1);
            }
        }
开发者ID:cosmo0920,项目名称:MikuTCPConnect,代码行数:55,代码来源:MainWindow.xaml.cs

示例3: CheckPort

        public static bool CheckPort(string ip, int port, int timeout)
        {
            using (System.Net.Sockets.TcpClient tcp = new System.Net.Sockets.TcpClient())
            {
                IAsyncResult ar = tcp.BeginConnect(ip, port, null, null);
                System.Threading.WaitHandle wh = ar.AsyncWaitHandle;
                try
                {
                    if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(timeout), false))
                    {
                        tcp.Close();
                        throw new TimeoutException();
                    }

                    tcp.EndConnect(ar);

                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
                finally
                {
                    wh.Close();
                }
            }
        }
开发者ID:skt90u,项目名称:skt90u-framework-dotnet,代码行数:28,代码来源:Net.cs

示例4: printButton_Click

        private void printButton_Click(object sender, EventArgs e)
        {
            // Printer IP Address and communication port
            string ipAddress = printerIpText.Text;
            int port = 9100;

            try
            {
                // Open connection
                System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
                client.Connect(ipAddress, port);

                // Write ZPL String to connection
                System.IO.StreamWriter writer = new System.IO.StreamWriter(client.GetStream());
                writer.Write(zplCode);
                writer.Flush();

                // Close Connection
                writer.Close();
                client.Close();
                MessageBox.Show("Print Successful!", "Success");
                this.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex);
                MessageBox.Show("No printer installed corresponds to the IP address given", "No response");
            }
        }
开发者ID:aditya7iyengar,项目名称:Label-Viewer,代码行数:29,代码来源:PrintLabel.cs

示例5: checkAvailability

 public void checkAvailability(object sender, System.ComponentModel.DoWorkEventArgs e) {
     try {
         System.Net.Sockets.TcpClient clnt = new System.Net.Sockets.TcpClient("smtp.gmail.com", 587);
         clnt.Close();
         e.Result = EmailResponse.ServerReachable;
     } catch {
         e.Result = EmailResponse.ServerUnreachable;
     }
 }
开发者ID:sanmadjack,项目名称:Email.CSharp,代码行数:9,代码来源:EmailHandler.cs

示例6: checkAvailability

 private void checkAvailability(object sender, System.ComponentModel.DoWorkEventArgs e)
 {
     try {
         System.Net.Sockets.TcpClient clnt=new System.Net.Sockets.TcpClient("smtp.gmail.com",587);
         clnt.Close();
         email_available = true;
     } catch {
         email_available = false;
     }
 }
开发者ID:elkine,项目名称:MASGAU,代码行数:10,代码来源:EmailHandler.cs

示例7: PerformWhois

        public static string PerformWhois(string WhoisServerHost, int WhoisServerPort, string Host)
        {
            string result="";
            try {
                String strDomain = Host;
                char[] chSplit = {'.'};
                string[] arrDomain = strDomain.Split(chSplit);
                // There may only be exactly one domain name and one suffix
                if (arrDomain.Length != 2) {
                    return "";
                }

                // The suffix may only be 2 or 3 characters long
                int nLength = arrDomain[1].Length;
                if (nLength != 2 && nLength != 3) {
                    return "";
                }

                System.Collections.Hashtable table = new System.Collections.Hashtable();
                table.Add("de", "whois.denic.de");
                table.Add("be", "whois.dns.be");
                table.Add("gov", "whois.nic.gov");
                table.Add("mil", "whois.nic.mil");

                String strServer = WhoisServerHost;
                if (table.ContainsKey(arrDomain[1])) {
                    strServer = table[arrDomain[1]].ToString();
                }
                else if (nLength == 2) {
                    // 2-letter TLD's always default to RIPE in Europe
                    strServer = "whois.ripe.net";
                }

                System.Net.Sockets.TcpClient tcpc = new System.Net.Sockets.TcpClient ();
                tcpc.Connect(strServer, WhoisServerPort);
                String strDomain1 = Host+"\r\n";
                Byte[] arrDomain1 = System.Text.Encoding.ASCII.GetBytes(strDomain1.ToCharArray());
                System.IO.Stream s = tcpc.GetStream();
                s.Write(arrDomain1, 0, strDomain1.Length);
                System.IO.StreamReader sr = new System.IO.StreamReader(tcpc.GetStream(), System.Text.Encoding.ASCII);
                System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
                string strLine = null;
                while (null != (strLine = sr.ReadLine())) {
                    strBuilder.Append(strLine+"\r\n");
                }
                result = strBuilder.ToString();
                tcpc.Close();
            }catch(Exception exc) {
                result="Could not connect to WHOIS server!\r\n"+exc.ToString();
            }
            return result;
        }
开发者ID:nothingmn,项目名称:robchartier-classlibrary,代码行数:52,代码来源:Whois.cs

示例8: ConnectionExists

 //--簡單檢查連線狀況
 public static bool ConnectionExists()
 {
     try
     {
         System.Net.Sockets.TcpClient clnt = new System.Net.Sockets.TcpClient("mis.twse.com.tw", 80);
         clnt.Close();
         return true;
     }
     catch //(System.Exception ex)
     {
         return false;
     }
 }
开发者ID:blueskycloud592,项目名称:StockNews-Collector,代码行数:14,代码来源:Util.cs

示例9: SendBytes

        private void SendBytes(IPEndPoint endpoint, byte[] buffer)
        {
            using (var client = new System.Net.Sockets.TcpClient())
            {
                client.Connect(endpoint);

                var stream = client.GetStream();
                stream.Write(buffer, 0, buffer.Length);
                stream.Flush();

                client.Close();
            }
        }
开发者ID:piers7,项目名称:PiCandy,代码行数:13,代码来源:OpcCommandListenerTests.cs

示例10: _main

        static void _main()
        {
            BlackCore.basic.cParams args = bcore.app.args;

               client = new System.Net.Sockets.TcpClient();

               int wavInDevices = WaveIn.DeviceCount;
               int selWav = 0;
               for (int wavDevice = 0; wavDevice < wavInDevices; wavDevice++)
               {
               WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(wavDevice);
               Console.WriteLine("Device {0}: {1}, {2} channels", wavDevice, deviceInfo.ProductName, deviceInfo.Channels);
               }

               Console.Write("Select device: ");
               selWav = int.Parse(Console.ReadLine());
               Console.WriteLine("Selected device is " + selWav.ToString());

               sshClient = new SshClient(args["host"], args["user"], args["pass"]);
               sshClient.Connect();

               if (sshClient.IsConnected)
               {

               shell = sshClient.CreateShellStream("xterm", 50, 50, 640, 480, 17640);
               Console.WriteLine("Open listening socket...");
               shell.WriteLine("nc -l " + args["port"] + "|pacat --playback");
               System.Threading.Thread.Sleep(2000);

               Console.WriteLine("Try to connect...");
               client.Connect(args["host"], int.Parse(args["port"]));
               if (!client.Connected) return;
               upStream = client.GetStream();

               //====================

               WaveInEvent wavInStream = new WaveInEvent();
               wavInStream.DataAvailable += new EventHandler<WaveInEventArgs>(wavInStream_DataAvailable);
               wavInStream.DeviceNumber = selWav;
               wavInStream.WaveFormat = new WaveFormat(44100, 16, 2);
               wavInStream.StartRecording();
               Console.WriteLine("Working.....");

               Console.ReadKey();
               sshClient.Disconnect();
               client.Close();
               wavInStream.StopRecording();
               wavInStream.Dispose();
               wavInStream = null;
               }
        }
开发者ID:hapylestat,项目名称:StreamAudio,代码行数:51,代码来源:Program.cs

示例11: Test

 //public ActionResult ListUpnp()
 //{
 //    var upnp = new NATUPNPLib.UPnPNATClass();
 //    if (upnp.StaticPortMappingCollection == null) return Content("UPnP Disabled");
 //    var result = upnp.StaticPortMappingCollection.Cast<NATUPNPLib.IStaticPortMapping>()
 //        .Aggregate("", (s, m) => s + m.Description + "<br/>");
 //    return Content(result);
 //}
 //public ActionResult EnableUpnp()
 //{
 //    var upnp = new NATUPNPLib.UPnPNATClass();
 //    if (upnp.StaticPortMappingCollection == null) return Content("UPnP Disabled");
 //    var localIP = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName())
 //        .First(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
 //    upnp.StaticPortMappingCollection.Add(25565, "TCP", 25565, localIP.ToString(), true, "Minecraft");
 //    return Redirect("/Home/ListUpnp");
 //}
 //public ActionResult DisableUpnp()
 //{
 //    var upnp = new NATUPNPLib.UPnPNATClass();
 //    if (upnp.StaticPortMappingCollection == null) return Content("UPnP Disabled");
 //    upnp.StaticPortMappingCollection.Remove(25565, "TCP");
 //    return Redirect("/Home/ListUpnp");
 //}
 public ActionResult Test()
 {
     var client = new System.Net.Sockets.TcpClient();
     try
     {
         client.Connect("localhost", 25565);
         client.Close();
         return Content("Connection OK " + DateTime.Now.Ticks);
     }
     catch (Exception e)
     {
         return Content("Error:<br />" + e.Message);
     }
 }
开发者ID:hassanselim0,项目名称:MinecraftWebToolkit,代码行数:38,代码来源:ConnectionController.cs

示例12: TcpSocketTest

 public static bool TcpSocketTest(string websiteUrl)
 {
     try
     {
         System.Net.Sockets.TcpClient client =
             new System.Net.Sockets.TcpClient(websiteUrl, 80);
         client.Close();
         return true;
     }
     catch (System.Exception ex)
     {
         return false;
     }
 }
开发者ID:MonetInfor,项目名称:MFCToolkit,代码行数:14,代码来源:NetTest.cs

示例13: checkInternetConnection

        private bool checkInternetConnection()
        {
            try
            {
                System.Net.Sockets.TcpClient clnt = new System.Net.Sockets.TcpClient("developer.anscamobile.com", 80);
                clnt.Close();
                return true;

            }
            catch (Exception ex)
            {
                return false; // host not reachable.
            }
        }
开发者ID:nadar71,项目名称:Krea,代码行数:14,代码来源:CoronaAPIPanel.cs

示例14: Scan

        public void Scan(object data)
        {
            System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
            try {

                client.BeginConnect(this.IPAddress, this.Port, new AsyncCallback(AttemptConnect), client);
                int x = 0;
                while (x <= Settings.PortScanTimeoutSeconds) {
                    System.Threading.Thread.Sleep(1000);
                    x++;
                }
                client.Close();
            } catch (Exception e) {
                Terminals.Logging.Log.Info("", e);
            }
        }
开发者ID:DefStevo,项目名称:defstevo-collection,代码行数:16,代码来源:NetworkScanItem.cs

示例15: SendRaw

        public static void SendRaw(string Hostname, int TunnelPort, System.IO.Stream ClientStream)
        {
            System.Net.Sockets.TcpClient tunnelClient = new System.Net.Sockets.TcpClient(Hostname, TunnelPort);
            var tunnelStream = tunnelClient.GetStream();
            var tunnelReadBuffer = new byte[BUFFER_SIZE];

            Task sendRelay = new Task(() => StreamHelper.CopyTo(ClientStream, tunnelStream, BUFFER_SIZE));
            Task receiveRelay = new Task(() => StreamHelper.CopyTo(tunnelStream, ClientStream, BUFFER_SIZE));

            sendRelay.Start();
            receiveRelay.Start();

            Task.WaitAll(sendRelay, receiveRelay);

            if (tunnelStream != null)
                tunnelStream.Close();

            if (tunnelClient != null)
                tunnelClient.Close();
        }
开发者ID:bansalvks,项目名称:Titanium,代码行数:20,代码来源:Tcp.cs


注:本文中的System.Net.Sockets.TcpClient.Close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。