我现在知道一个IP 61.203.487.84 和一个端口 123要怎样使得这个探测的时间很短 不至于让用户等得不耐烦
下面是我的代码!
foreach (DataGridViewRow row in this.Dgvdete.Rows)
{
string ips= row.Cells[0].Value.ToString();
string port= row.Cells[1].Value.ToString();
IPAddress ip = IPAddress.Parse(ips);
DataGridViewImageCell imgcell = (DataGridViewImageCell)row.Cells[2];
imgcell.Value = imageList1.Images[1];
try
{
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(port));
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.SendTimeout = 1;
sock.Connect(point);
imgcell.Value = imageList1.Images[0];
//Execute(string dosCommand, int milliseconds)
}
catch
{
imgcell.Value = imageList1.Images[1];
}
}我现在的问题是当一个端口没有开发的时候!在探测的时候要等一段时间大约是(30多秒)
我该怎样使得它的探测时间在1到2秒!
下面是我的代码!
foreach (DataGridViewRow row in this.Dgvdete.Rows)
{
string ips= row.Cells[0].Value.ToString();
string port= row.Cells[1].Value.ToString();
IPAddress ip = IPAddress.Parse(ips);
DataGridViewImageCell imgcell = (DataGridViewImageCell)row.Cells[2];
imgcell.Value = imageList1.Images[1];
try
{
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(port));
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.SendTimeout = 1;
sock.Connect(point);
imgcell.Value = imageList1.Images[0];
//Execute(string dosCommand, int milliseconds)
}
catch
{
imgcell.Value = imageList1.Images[1];
}
}我现在的问题是当一个端口没有开发的时候!在探测的时候要等一段时间大约是(30多秒)
我该怎样使得它的探测时间在1到2秒!
解决方案 »
- 求助!!!
- 急救!!!!C# 中多个TabControl(3个以上)是如何显示和隐藏的!我想用几个按钮来控制,点一个显示一个,点另外的就换一个!但是要在同一个区域里显示??
- MSHTML发布的问题
- 关于数组的实现(课件,急!!)
- 为何用这种委托方式刷新窗口无效?在线等.....
- vs2005 安装部署的问题
- 隐藏部分窗体,就像PPLive右边频道列表,点下中间线上的小三角形,列表框缩进去了?急,请高手帮忙!!
- Winform 的 datagrid 能不能添加按钮列啊?
- 如何用C# 获取内嵌在html中的xml
- 关于窗体之间数据传递的问题!!!!(在线等待)
- C# post 数据到服务器实现自动登录
- 查询数据库中是否有该数据 若没有 就插入
/// <summary>
/// 启动连接
/// </summary>
private bool Connection(string DeviceIP, int DevicePort)
{
try
{
tcpClientSocket = new TcpClient();
if (!Connect(DeviceIP, DevicePort, 1000))
{
return false;
}
tcpClientSocket.SendTimeout = 3000;
tcpClientSocket.ReceiveTimeout = 3000;
//tcpClientSocket.Connect(DeviceIP, DevicePort);
ns = tcpClientSocket.GetStream();
ns.WriteTimeout = 1000;
strReader = new StreamReader(ns);
return true;
}
catch (Exception ex)
{
return false;
}
} private bool IsConnectionSuccessful = false;
private Exception socketexception;
private ManualResetEvent TimeoutObject = new ManualResetEvent(false); public bool Connect(string serverip, int serverport, int timeoutMSec)
{
TimeoutObject.Reset();
socketexception = null;
tcpClientSocket = new TcpClient(); tcpClientSocket.BeginConnect(serverip, serverport,
new AsyncCallback(CallBackMethod), tcpClientSocket); if (TimeoutObject.WaitOne(timeoutMSec, false))
{
if (IsConnectionSuccessful)
{
return true;
}
else
{
return false;
}
}
else
{
tcpClientSocket.Close();
return false;
}
}
private 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();
}
}以前写的,希望对你有帮助