• <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函數(shù)1秒鐘內(nèi)返回結(jié)果
            2010-03-30 08:57 | 555
            用了 非常好  回復(fù)  更多評論
              

            <2008年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(5)

            隨筆檔案

            文章分類

            文章檔案

            好友的Bolg

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久涩综合| 麻豆精品久久精品色综合| 久久久久久久久久免免费精品| 国产精品9999久久久久| 久久久久久国产精品无码超碰| 亚洲精品乱码久久久久久按摩 | 久久精品一区二区三区中文字幕 | 91精品久久久久久无码| 亚洲综合婷婷久久| 久久精品国产99国产精品| 久久精品亚洲男人的天堂| 理论片午午伦夜理片久久| 久久久久久国产精品美女| 亚洲欧洲久久久精品| 亚洲AV无码久久精品蜜桃| 国产精品99久久99久久久| 国产成人久久精品二区三区| 亚洲精品久久久www| 久久久噜噜噜久久中文福利| 久久精品国产99国产电影网| 久久夜色精品国产亚洲av| 国产色综合久久无码有码| 国产成人精品免费久久久久| 久久久久亚洲AV综合波多野结衣 | 国产精品99久久久久久人| 99热成人精品免费久久| 久久久久久久免费视频| WWW婷婷AV久久久影片| 亚洲精品无码久久久| 99久久精品国内| 久久人人爽人人人人爽AV| 久久久久久免费一区二区三区| 色综合合久久天天给综看| 久久96国产精品久久久| 久久WWW免费人成一看片| 91精品免费久久久久久久久| 亚洲精品无码久久久久去q| 久久人人爽人爽人人爽av| 国产成人综合久久综合| 99久久夜色精品国产网站| 久久久91人妻无码精品蜜桃HD|