• <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 天書 閱讀(5708) 評論(1)  編輯 收藏 引用

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

            <2008年9月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            常用鏈接

            留言簿(5)

            隨筆檔案

            文章分類

            文章檔案

            好友的Bolg

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久久噜噜噜久久中文字幕色伊伊 | 久久亚洲精品中文字幕| 久久婷婷国产剧情内射白浆| 久久精品免费一区二区| 99麻豆久久久国产精品免费| 久久精品无码专区免费| 亚洲AV乱码久久精品蜜桃| 国产精品美女久久久免费| 亚洲AV成人无码久久精品老人| 久久免费高清视频| 久久精品国产99久久久古代| 色噜噜狠狠先锋影音久久| 亚洲精品乱码久久久久久中文字幕 | 浪潮AV色综合久久天堂| 久久久久久亚洲精品不卡| 久久久久亚洲av无码专区喷水 | 亚洲欧美精品一区久久中文字幕| 狠狠色丁香久久婷婷综合五月| 久久国产成人精品国产成人亚洲| 久久久久亚洲AV无码麻豆| 中文字幕无码久久久| 久久久久免费视频| 国产精品成人99久久久久91gav| 狠狠色丁香久久婷婷综合五月| 久久午夜夜伦鲁鲁片免费无码影视 | 一本久久久久久久| 精品久久777| 成人久久久观看免费毛片| 囯产精品久久久久久久久蜜桃| 日本精品一区二区久久久| 国产精品久久久久9999高清| 亚洲成色www久久网站夜月| 美女久久久久久| 国产精品美女久久久网AV| 久久伊人精品青青草原高清| 国产午夜免费高清久久影院| 亚洲AV日韩精品久久久久久久| 久久久久av无码免费网| 亚洲精品乱码久久久久久按摩| 伊人久久大香线蕉综合影院首页| 亚洲精品无码久久久久久|