日期:2014-05-18 浏览次数:20941 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace TestConnSql { public partial class Form1 : Form { public Form1() { InitializeComponent(); wait_conn(); } private void wait_conn() { bool NetStatus, net = false; string DataS = "192.168.160.1"; int ty; CheckIpConn conn = new CheckIpConn(); ty = conn.TT; //net = conn.Connected; conn.CheckIp(DataS); //Thread.Sleep(2000); ty = conn.TT; NetStatus = conn.Connected; //net = CheckIpConn.CheckIp(DataS); // MessageBox.Show(net.ToString()); //conn.setvalue(); NetStatus = conn.Connected; if (!NetStatus) MessageBox.Show(conn.Connected.ToString() + ty.ToString()); } } }
using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Threading; using System.Text; using System.Windows.Forms; namespace TestConnSql { public class CheckIpConn { private int _port = 1433; private volatile bool _connect; private IPEndPoint _iPEndPoint; private TcpClient _client; private TimerCallback _timerCallBack; private System.Threading.Timer _timer; private int tt = 5; public int TT { get { return tt; } } public int Port { get { return _port; } set { _port = value; } } public bool Connected { get { return _connect; } } public void setvalue() { _connect = true; } public void CheckIp(string Ip) { _iPEndPoint = new IPEndPoint(IPAddress.Parse(Ip), _port); _timerCallBack = new TimerCallback(CheckConnect); _timer = new System.Threading.Timer(_timerCallBack, null, 10, 1000); _timer.Dispose(); } public void CheckConnect(object o) { try { _client = new TcpClient(); _client.Connect(_iPEndPoint); _connect = true; this._connect = true; tt = 50; //MessageBox.Show("cc" + _connect); _client.Close(); } catch { _connect = false; //MessageBox.Show("cc" + _connect); } } } }