日期:2014-05-19  浏览次数:20650 次

Sock通迅,如何自动查找服务器端。
在做Sock通迅的时候,需要服务器开启Service,然后客户端指定IP之后才可以进行连接。
有没有办法在不知道服务器IP的情况下,程序自动搜索开启Service的电脑,与之建立连接呢?

------解决方案--------------------
多线程扫描网段

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace 多线程扫描网段
{
public partial class Form1 : Form
{
private DateTime startTime;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int min = (int)this.numericUpDown4.Value,max = (int)this.numericUpDown5.Value;
if (min> = max) { MessageBox.Show( "网段输入错误! "); return; }
startTime = DateTime.Now;
string mask = this.numericUpDown1.Value.ToString() + ". " + this.numericUpDown2.Value.ToString() + ". " + this.numericUpDown3.Value.ToString();
this.progressBar1.Minimum = min;
this.progressBar1.Maximum = max;
Thread[] threads=new Thread[max-min +1];
while (min <= max)
{
Scan NewThreadScan = new Scan();
NewThreadScan.IP = mask + ". " + min.ToString();
NewThreadScan.GetHostInfo = new HostInfo(newHostInfo);
threads[min - 1] = new Thread(new ThreadStart(NewThreadScan.start));
threads[min - 1].Start();
min++;
}
}
public void newHostInfo(string HostIP, string NewHostName)
{
this.setListBox(HostIP, NewHostName);
}


private void setListBox(string HostIp, string NewHostName)
{
if (listBox1.InvokeRequired && label4.InvokeRequired && progressBar1.InvokeRequired)
{
HostInfo b = new HostInfo(setListBox);
Invoke(b, new object[] { HostIp, NewHostName });
}
else
{
lock (listBox1)
{

listBox1.Items.Add(HostIp + " " + NewHostName);
if (progressBar1.Value != progressBar1.Maximum)
{
progressBar1.Value++;
}
else
{
MessageBox.Show( "成功完成检测! ");
DateTime endTime = DateTime.Now;
TimeSpan timeSp = endTime - startTime;
label4.Text = timeSp.Seconds.ToString() + "秒 ";
progressBar1.Value = progressBar1.Minimum;

}
}
}


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace 多线程扫描网段
{


class Scan
{
private string _IP;
public string _HostName;
public HostInfo GetHostInfo;
public string IP
{
get { return _IP; }
set { _IP = value; }
}

public Scan()
{

}