C# socket连接connect函数1秒钟内返回结果

C# socket连接connect函数1秒钟内返回结果

 

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("连接失败");
            }
        }
    }
}

你可能感兴趣的:(C# socket连接connect函数1秒钟内返回结果)