• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 124,  comments - 29,  trackbacks - 0

             

            using System;
            using System.Collections.Generic;
            using System.Text;
            using System.Threading;
            using System.Net.Sockets;
            using System.Net;

            namespace SocketConnTimeOut
            {
                
            class TimeOutSocket
                
            {
                    
            private static bool IsConnectionSuccessful = false;
                    
            private static Exception socketexception;
                    
            private static ManualResetEvent TimeoutObject = new ManualResetEvent(false);

                    
            public static TcpClient Connect(IPEndPoint remoteEndPoint, int timeoutMSec)
                    
            {
                        TimeoutObject.Reset();
                        socketexception 
            = null;

                        
            string serverip = Convert.ToString(remoteEndPoint.Address);
                        
            int serverport = remoteEndPoint.Port;
                        TcpClient tcpclient 
            = new TcpClient();

                        tcpclient.BeginConnect(serverip, serverport, 
            new AsyncCallback(CallBackMethod), tcpclient);

                        
            if (TimeoutObject.WaitOne(timeoutMSec, false))
                        
            {
                            
            if (IsConnectionSuccessful)
                            
            {

                                
            return tcpclient;
                            }

                            
            else
                            
            {
                                
            throw socketexception;
                            }

                        }

                        
            else
                        
            {
                            tcpclient.Close();
                            
            throw new TimeoutException("TimeOut Exception");
                        }

                    }

                    
            private static void CallBackMethod(IAsyncResult asyncresult)
                    
            {
                        
            try
                        
            {
                            IsConnectionSuccessful 
            = false;
                            TcpClient tcpclient 
            = asyncresult.AsyncState as TcpClient;

                            
            if (tcpclient.Client != null)
                            
            {
                                tcpclient.EndConnect(asyncresult);
                                IsConnectionSuccessful 
            = true;
                            }

                        }

                        
            catch (Exception ex)
                        
            {
                            IsConnectionSuccessful 
            = false;
                            socketexception 
            = ex;
                        }

                        
            finally
                        
            {
                            TimeoutObject.Set();
                        }

                    }


                }

            }



            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Text;
            using System.Net.Sockets;
            using System.Net;
            using System.Threading;
            using System.IO;

            using System.Windows.Forms;

            namespace SocketConnTimeOut
            {
                public partial class Form1 : Form
                {
                    public Form1()
                    {
                        InitializeComponent();
                    }

                    private void label2_Click(object sender, EventArgs e)
                    {

                    }
                    string strIP = "";
                    int strPort = 8000;
                    int timeout = 1000;
                    private void btnConn_Click(object sender, EventArgs e)
                    {
                        try
                        {
                            strIP = txtIP.Text;
                            strPort = Convert.ToInt32(txtPort.Text);

                            IPAddress localAddr = IPAddress.Parse(strIP);
                            IPEndPoint remoteEndPoint = new IPEndPoint(localAddr, strPort);
                            TcpClient NetworkClient = TimeOutSocket.Connect(remoteEndPoint, timeout);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("連接失敗");
                        }
                    }
                }
            }

            posted on 2010-02-25 14:57 天書 閱讀(5707) 評論(1)  編輯 收藏 引用

            FeedBack:
            # re: C# socket連接connect函數1秒鐘內返回結果
            2010-03-30 08:57 | 555
            用了 非常好  回復  更多評論
              

            <2008年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(5)

            隨筆檔案

            文章分類

            文章檔案

            好友的Bolg

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            国产偷久久久精品专区| 久久精品国产亚洲5555| 久久天天躁狠狠躁夜夜躁2014| 国产精品久久久久a影院| 久久精品欧美日韩精品| 99久久精品免费看国产一区二区三区| 久久久国产精华液| 久久国产色AV免费观看| 亚洲va久久久久| 狠狠久久亚洲欧美专区| 久久人人爽人人人人爽AV| 91精品国产91热久久久久福利| 国产69精品久久久久APP下载 | 久久99精品久久久久久9蜜桃 | 亚洲第一永久AV网站久久精品男人的天堂AV| 国产精品久久久久久久久久影院| 97热久久免费频精品99| 麻豆精品久久久久久久99蜜桃 | 久久精品人成免费| 女人高潮久久久叫人喷水| 亚洲国产精品婷婷久久| 久久精品国产亚洲AV电影| 国产99久久久国产精品小说| 国产激情久久久久影院小草 | 亚洲AV无码久久精品成人 | 久久99精品国产一区二区三区 | 精品久久久久久无码不卡| 亚洲国产精品久久久久久| 国产成年无码久久久久毛片| 婷婷久久五月天| 亚洲欧美日韩久久精品| 日本精品久久久久影院日本| 久久99精品久久久久久秒播| 久久久久一区二区三区| 亚洲国产精品久久久久婷婷软件| 久久99国产精品一区二区| 99精品久久久久中文字幕| 国产精品岛国久久久久| 久久91精品久久91综合| 2021国产成人精品久久| 狠狠人妻久久久久久综合|