如何设置Socket.ConnectAsync的超时? [英] how do i set a timeout of Socket.ConnectAsync?
问题描述
我一直在阅读有关如何检查所有的监听服务器上的LAN特定端口上,最后我写了一些代码,它的作品,因为我想。结果
我使用的系统。 Threading.Tasks.Parallel连接到所有254 IP地址尽可能快的结果
例如: 192.168.1 1 - 192.168.1 254 <。 / b>
I've been reading about how to check for all listening server on a specific port on LAN and finally I've wrote some code that it works as i want.
I'm using System.Threading.Tasks.Parallel to connect to all 254 IP Addresses as fast as possible
ex : 192.168.1.1 - 192.168.1.254
我需要做的是设置超时为这些连接尝试..因为它需要大约15-20秒打印:连接失败当它失败..所以我如何做到这一点?
What i need is to set a timeout for these connection attempts .. because it takes about 15-20 seconds to print : "Connection Failed " when it fails .. so How do i do that??
下面的客户端代码:
static void Main(string[] args)
{
Console.WriteLine("Connecting to IP addresses has started. \n");
Parallel.For(1, 255, i =>
{
Connect("192.168.1." + i);
});
Console.ReadLine();
}
private static void Connect(string ipAdd)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse(ipAdd), 9990);
e.RemoteEndPoint = ipEnd;
e.UserToken = s;
e.Completed += new EventHandler<SocketAsyncEventArgs>(e_Completed);
Console.WriteLine("Trying to connect to : " + ipEnd);
s.ConnectAsync(e);
}
private static void e_Completed(object sender, SocketAsyncEventArgs e)
{
if (e.ConnectSocket != null)
{
StreamReader sr = new StreamReader(new NetworkStream(e.ConnectSocket));
Console.WriteLine("Connection Established : " + e.RemoteEndPoint + " PC NAME : " + sr.ReadLine());
}
else
{
Console.WriteLine("Connection Failed : " + e.RemoteEndPoint);
}
}
服务器代码:
Server Code:
static void Main(string[] args)
{
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Any,9990);
server.Bind(ep);
server.Listen(100);
Socket client = server.Accept();
NetworkStream stream = new NetworkStream(client);
StreamWriter sw = new StreamWriter(stream)
sw.WriteLine(System.Environment.MachineName);
sw.Flush();
sw.Dispose();
stream.Dispose();
client.Dispose();
server.Dispose();
}
如果有,使得它更好的请告诉我..即时通讯的任何提示或通知使用[.NET 4.0]提前..
if there's any hint or notice that makes it better please tell me .. Im using [.Net 4.0] Sockets TCP
sorry for my bad English and thanks in advance..
推荐答案
对不起我的英语不好,并>我已经想通了一个解决方案。结果
首先添加所有创建套接字类型的列表的SocketAsyncEventArgs 或类型的插槽或结果$ b $然后b使用System.Timers.Timer以关闭所有挂起连接,并且在所述计时器滴答即5秒钟后之后所连接的之一。 (timer.Interval = 5000)
I've figured out a solution.
first add all created sockets to a list of type SocketAsyncEventArgs or type ofSocket or
then use System.Timers.Timer to close all pending connection and the connected one after when the timer ticks which is after 5 seconds. (timer.Interval = 5000).
客户端代码:
//I've changed my console application to Winform
public ServerDiscovery()
{
InitializeComponent();
timer.Elapsed += timer_tick;
}
System.Timers.Timer timer = new System.Timers.Timer(5000);
List<SocketAsyncEventArgs> list = new List<SocketAsyncEventArgs>();
private void btnRefresh_Click(object sender, EventArgs e)
{
timer.Start();
Parallel.For(1, 255, (i, loopState) =>
{
ConnectTo("192.168.1." + i);
});
}
private void ConnectTo(string ipAdd)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
e.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(ipAdd), 9990);
e.UserToken = s;
e.Completed += new EventHandler<SocketAsyncEventArgs>(e_Completed);
list.Add(e); // Add to a list so we dispose all the sockets when the timer ticks.
s.ConnectAsync(e);
}
private void e_Completed(object sender, SocketAsyncEventArgs e)
{
if (e.ConnectSocket != null) //if there's a connection Add its info to a listview
{
StreamReader sr = new StreamReader(new NetworkStream(e.ConnectSocket));
ListViewItem item = new ListViewItem();
item.Text = sr.ReadLine();
item.SubItems.Add(((IPEndPoint)e.RemoteEndPoint).Address.ToString());
item.SubItems.Add("Online");
AddServer(item);
}
}
delegate void AddItem(ListViewItem item);
private void AddServer(ListViewItem item)
{
if (InvokeRequired)
{
Invoke(new AddItem(AddServer), item);
return;
}
listServer.Items.Add(item);
}
private void timer_tick(object sender, EventArgs e)
{
timer.Stop();
foreach (var s in list)
{
((Socket)s.UserToken).Dispose(); //disposing all sockets that's pending or connected.
}
}
这篇关于如何设置Socket.ConnectAsync的超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!