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

            FeedBack:
            # re: C# socket連接connect函數(shù)1秒鐘內(nèi)返回結(jié)果
            2010-03-30 08:57 | 555
            用了 非常好  回復(fù)  更多評論
              

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理



            <2013年10月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿(5)

            隨筆檔案

            文章分類

            文章檔案

            好友的Bolg

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久亚洲国产精品123区| 久久最近最新中文字幕大全| 久久午夜电影网| 色诱久久久久综合网ywww| 国产精品99久久久精品无码| 亚洲精品无码久久不卡| 中文成人无码精品久久久不卡| 久久人人爽人人澡人人高潮AV | 日产精品久久久久久久| 久久精品中文字幕一区| 久久受www免费人成_看片中文| 久久久久久久91精品免费观看| 久久只有这里有精品4| 亚洲国产一成人久久精品| 久久精品亚洲精品国产色婷| 1000部精品久久久久久久久| 久久成人国产精品二三区| 久久天天日天天操综合伊人av| 亚洲人成无码网站久久99热国产| 久久久这里有精品| 97久久超碰国产精品2021| 久久免费小视频| 久久久国产亚洲精品| 色妞色综合久久夜夜| 91精品国产高清久久久久久91| 亚洲AV伊人久久青青草原| 青青草原精品99久久精品66| 成人精品一区二区久久| 精品综合久久久久久98| 91麻精品国产91久久久久| 国产精品久久久久久久app | 久久久久一本毛久久久| 狠狠综合久久综合88亚洲| 免费国产99久久久香蕉| 人妻无码αv中文字幕久久琪琪布| 狠狠色丁香久久婷婷综合五月 | 日韩精品无码久久久久久| 久久久久久噜噜精品免费直播| 亚洲国产精品无码久久一线| 岛国搬运www久久| 久久久久久久97|