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 天书 阅读(5638) 评论(1)  编辑 收藏 引用

FeedBack:
# re: C# socket连接connect函数1秒钟内返回结果
2010-03-30 08:57 | 555
用了 非常好  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2010年2月>
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213

常用链接

留言簿(5)

随笔档案

文章分类

文章档案

好友的Bolg

搜索

  •  

最新评论

阅读排行榜

评论排行榜