• <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

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            欧美熟妇另类久久久久久不卡| 久久午夜伦鲁片免费无码| 久久无码一区二区三区少妇| 久久久久国产日韩精品网站| 亚洲AV伊人久久青青草原| 国产午夜精品久久久久免费视| 亚洲国产精品久久久久| 色妞色综合久久夜夜| 久久夜色精品国产欧美乱| 久久99热这里只有精品国产 | …久久精品99久久香蕉国产| 久久综合九色综合97_久久久| 久久久久综合中文字幕| 日韩AV无码久久一区二区 | 国产一区二区久久久| 99国产精品久久久久久久成人热| 久久人妻少妇嫩草AV蜜桃| 久久人人爽人人爽人人AV| 青春久久| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 色偷偷88888欧美精品久久久| 国产日韩久久久精品影院首页| 久久精品99久久香蕉国产色戒| 久久久久女教师免费一区| 久久久久久久综合日本亚洲| 欧美噜噜久久久XXX| 伊人久久大香线焦AV综合影院 | 亚洲国产一成人久久精品| 亚洲国产成人久久精品99| 久久精品视频网| 精品久久久久久国产91| 99久久久精品免费观看国产| 亚洲va中文字幕无码久久不卡 | 久久精品视频免费| 久久不射电影网| 久久91精品国产91久久麻豆| 亚洲AV无码1区2区久久| 久久国产高潮流白浆免费观看| 无码人妻精品一区二区三区久久| 国产美女亚洲精品久久久综合| 欧美亚洲国产精品久久高清|