?? form1.cs
字號:
?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.NetworkInformation;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace NetComm
{
public partial class Form1 : Form
{
public const Int32 conmaxreceivedata = 1024;
public const string conlocalip = "127.0.0.1";
CipDetect ippointdetect=new CipDetect() ;
Thread[] threadLocalnetcards = null;
Thread threadLocalcomputer = null;
public class myudp
{
public IPAddress m_ipaddress;
public Int32 m_port;
public UdpClient m_udp;
public Byte[] m_receivedata;
public IPEndPoint m_remoteipendpoint;
public myudp(IPAddress v_ipaddress,Int32 v_port)
{
m_ipaddress = v_ipaddress;
m_port = v_port;
m_receivedata = new Byte[conmaxreceivedata];
m_remoteipendpoint = new IPEndPoint(v_ipaddress, v_port);
m_udp = new UdpClient(m_remoteipendpoint);
m_udp.EnableBroadcast = true;
m_remoteipendpoint = null;
}
}
public class netcardUdp:myudp
{
public netcardUdp(IPAddress v_ipaddress, Int32 v_port)
: base(v_ipaddress, v_port)
{
}
public void working()
{
while (true)
{
m_receivedata = m_udp.Receive(ref m_remoteipendpoint);
Console.WriteLine(System.Text.Encoding.ASCII.GetString(m_receivedata));///////////////
m_remoteipendpoint.Address = IPAddress.Parse(conlocalip);
m_remoteipendpoint.Port = 21100;
m_udp.Send(m_receivedata, m_receivedata.Length, m_remoteipendpoint);
}
}
}
public class localUdp : myudp
{
public localUdp(IPAddress v_ipaddress, Int32 v_port)
: base(v_ipaddress, v_port)
{
}
public void working()
{
while (true)
{
m_receivedata = m_udp.Receive(ref m_remoteipendpoint);
Console.WriteLine(System.Text.Encoding.ASCII.GetString(m_receivedata));/////////////////
// m_udp.Send(m_receivedata, m_receivedata.Length, m_remoteipendpoint);
}
}
}
public class CnetCard
{
public IPAddress m_ipaddress;
public Int32 m_errorcount;
public Boolean m_statusok;
public Boolean m_master;
public Boolean m_online;
public CnetCard()
{
m_errorcount = 0;
m_statusok = true;
m_master = true ;
m_online = true;
}
}
public class Cnetwork
{
public CnetCard[] m_netcards;
public Boolean m_master;
public Cnetwork()
{
m_master = true;
}
public void SetNetcardCount(Int32 v_count)
{
m_netcards=new CnetCard[v_count];
for (Int32 i = 0; i < v_count; i++)
{
m_netcards[i] = new CnetCard();
}
}
}
public class CipDetect
{
public Cnetwork[] m_networks;
private Ping m_pingsend;
private PingReply m_pingresult;
private const Int32 m_iperrormaxcount = 2;
public CipDetect()
{
m_pingsend=new Ping() ;
}
public void SetnetworkCount(Int32 v_count)
{
m_networks = new Cnetwork[v_count];
for (Int32 i = 0; i < v_count; i++)
{
m_networks[i] = new Cnetwork();
}
}
public void detecting()
{
Int32 i,j;
for (i = 0; i < m_networks.Length; i++)
{
for (j = 0; j < m_networks[i].m_netcards.Length; j++)
{
m_pingresult = m_pingsend.Send(m_networks[i].m_netcards[j].m_ipaddress, 20);
Console.WriteLine("ping" );////////////////////////////
if (m_pingresult.Status == IPStatus.Success)
{
m_networks[i].m_netcards[j].m_statusok = true;
m_networks[i].m_netcards[j].m_errorcount = 0;
}
else
{
m_networks[i].m_netcards[j].m_errorcount += 1;
if (m_networks[i].m_netcards[j].m_errorcount > m_iperrormaxcount)
{
m_networks[i].m_netcards[j].m_statusok = false;
m_networks[i].m_netcards[j].m_errorcount = m_iperrormaxcount;
}
}
}
}
}
}
static void NetChangedCallback(object sender, EventArgs e)
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface n in adapters)
{
Console.WriteLine(" {0} is {1}", n.Name+"UPDown", n.OperationalStatus);/////////////////////
}
}
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
string tempip=null ;
Int32 tempi=0;
IPHostEntry Localnetcards;
Int32 i,j;
Localnetcards = Dns.GetHostEntry(Environment.MachineName);
tempi=Localnetcards.AddressList.Length;
threadLocalnetcards=new Thread[tempi];
for (i = 0; i < Localnetcards.AddressList.Length; i++)
{
tempip =Localnetcards.AddressList[i].ToString();
if (String.Compare(tempip, conlocalip, true) == 0 ? false : true)
{
netcardUdp cudp = new netcardUdp(Localnetcards.AddressList[i],11100);
threadLocalnetcards[i] = new Thread(new ThreadStart(cudp.working));
threadLocalnetcards[i].IsBackground = true;
threadLocalnetcards[i].Start();
}
}
localUdp ludp = new localUdp(IPAddress.Parse(conlocalip), 21100);
threadLocalcomputer = new Thread(new ThreadStart(ludp.working));
threadLocalcomputer.IsBackground = true;
threadLocalcomputer.Start();
NetworkChange.NetworkAvailabilityChanged += new
NetworkAvailabilityChangedEventHandler(NetChangedCallback);
ippointdetect.SetnetworkCount (2);
for (i = 0; i < 2; i++)
{
ippointdetect.m_networks[i].SetNetcardCount(2);
for (j = 0; j < 2; j++)
{
ippointdetect.m_networks[i].m_netcards[j].m_ipaddress = IPAddress.Parse("192.168.1.2");
}
}
timerIP.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
}
private void timerIP_Tick(object sender, EventArgs e)
{
ippointdetect.detecting();
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -