C#模拟实现DHCP服务器

上传人:ji****72 文档编号:37537279 上传时间:2018-04-18 格式:DOC 页数:11 大小:107KB
返回 下载 相关 举报
C#模拟实现DHCP服务器_第1页
第1页 / 共11页
C#模拟实现DHCP服务器_第2页
第2页 / 共11页
C#模拟实现DHCP服务器_第3页
第3页 / 共11页
C#模拟实现DHCP服务器_第4页
第4页 / 共11页
C#模拟实现DHCP服务器_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《C#模拟实现DHCP服务器》由会员分享,可在线阅读,更多相关《C#模拟实现DHCP服务器(11页珍藏版)》请在金锄头文库上搜索。

1、使用使用 TcpClient 不完备实现不完备实现 DHCP 服务器端服务器端namespace DHCPserver / / 客户端申请组件/ struct clientComputerInfoprivate string computerMac; /暂时还不知道如何取MAC地址,先用这 个吧public string ComputerMacget return computerMac; set computerMac = value; private string computerName;public string ComputerNameget return computerName;

2、 set computerName = value; public clientComputerInfo(string computerName)puterName = computerName;puterMac = “00-00-00-00-00-00-00-E0“;public void showDhcpClientInfo()Console.WriteLine(“有一个DHCP客户端向你索要地址,它的信息如 下“);Console.WriteLine(“计算机名为:“ + computerName);Console.WriteLine(“计算机mac地址:“ + computerMac)

3、;/ / 数据访问组件/ class sqlDataSourceAccess/放心,实际开发中绝对不会把它硬编码在此,通过反编译可以反回这 个.如果是在ASP.NET中的Web.Config中还可以通过命令行加密.aspnet_regiis -pef .private string sqlConnStr = “Data Source=HELLA-PC;Integrated Security=true;Initial Catalog=DHCPServerIPPool;“;private SqlConnection sqlConn = null;private DataSet sqlDataSet

4、 = null;private SqlCommand sqlCmd = null;/ / 打开一个连接/ / 返回一个连接public SqlConnection getSqlConn()trysqlConn = new SqlConnection(sqlConnStr);sqlConn.Open();return sqlConn;catch (Exception)throw new Exception(“数据库连接发生要异常!“);/ / 关闭一个已打开的连接/ / 已打开的连接对象public void closeSqlConn(SqlConnection sqlConn)if (sqlC

5、onn.State = ConnectionState.Open)sqlConn.Close();/ / 使用存过/ / 存过名/ 存过参数/ public DataSet getDataSetSto(string stor, SqlParameter paras)sqlCmd = new SqlCommand(stor, getSqlConn();sqlCmd.CommandType = CommandType.StoredProcedure;for (int i = 0; i / 返一个sql命令返回的结果集/ / sql命令/ ql命令中的参数/ 此sql命令的结果集public Dat

6、aSet getDataSet(string sql, SqlParameter paras)sqlCmd = new SqlCommand(sql, getSqlConn();for (int i = 0; i “ + AssignIpInfo.ComputerMac + “ 主机名为:“ + AssignIpInfo.ComputerName);sw.Flush();sw.Close();elseFile.AppendAllText(“G:ExceptionAssignIpInfo.txt“, “时间为: “ + DateTime.Now.ToString() + “:分配IP为“ + a

7、ssignIp + “ 分配给了mac为“ + AssignIpInfo.ComputerMac + “ 主机名为:“ + AssignIpInfo.ComputerName);public void writerException(Exception ex)if (!File.Exists(“G:Exceptionexception.txt“)StreamWriter sw = new StreamWriter(File.Create(“G:Exceptionexception.txt“);sw.WriteLine(DateTime.Now.ToString() + “这是第一次发生异 常:

8、“ + ex.Message);sw.Flush();sw.Close();elseFile.AppendAllText(“G:Exceptionexception.txt“, DateTime.Now + “异常信息:“ + ex.Message);class Programstatic sqlDataSourceAccess findIpObj = new sqlDataSourceAccess();static LogInto logInto = new LogInto();static List dhcpClient = new List();static void Main(stri

9、ng args)Console.BackgroundColor = ConsoleColor.Blue;IPAddress ip = IPAddress.Parse(“192.168.1.1“); /DHCP服务器地址IPEndPoint ipAndEnd = new IPEndPoint(ip, 68);TcpListener tcpListener = new TcpListener(ipAndEnd);tcpListener.Start();/起来无限监听的线程,该线程下还有两儿子线程Thread listenerTh1 = new Thread(listenerClient);list

10、enerTh1.Start(tcpListener);/再来一个广播线程Thread broadCastTh = new Thread(new ThreadStart(broadCastClient);broadCastTh.Start();/再来服务器展示时间线程Thread serverShowTime = new Thread(new ThreadStart(showTimeClient);serverShowTime.Start();/ / 一个时间展示的小线程/ static void showTimeClient()while (true)Console.Title = DateT

11、ime.Now.ToString();Thread.Sleep(1000);/ / 对每个向此服务器索引过ip的客户端广播信息。/ static void broadCastClient()tryStreamWriter broadCast = null;while (true)Console.WriteLine(“果断向客户端广播信息“);string broadCastMess = Console.ReadLine();foreach (TcpClient temp in dhcpClient)broadCast = new StreamWriter(temp.GetStream();br

12、oadCast.WriteLine(broadCastMess);broadCast.Flush();catch (Exception ex) /使用老祖宗比较爽logInto.writerException(ex);static void listenerClient(object obj)TcpListener tcpListener = obj as TcpListener;TcpClient clientTcpclient = null;while (true)clientTcpclient = tcpListener.AcceptTcpClient();dhcpClient.Add(

13、clientTcpclient); /每来一个索取IP的客户 加入此泛型集合 /来一个读线程,来说明有索取IP的客户向我们索要地址来了Thread clientReader = new Thread(toClientReader);clientReader.Start(clientTcpclient.GetStream();/向客户的线程,为索取IP的客户展示选择.注意,此线程采有形 式参数的委托类型Thread clientWriter = new Thread(toClientWriter);clientWriter.Start(clientTcpclient.GetStream();/

14、/ 输出申请IP客户端的详细信息/ / 是委托ParameterizedThreadStart()中的Object参数,以 便与此委托相匹配.private static void toClientReader(object obj)NetworkStream nsC = obj as NetworkStream;/ 对网络络流进行一次包装,方便操作StreamReader serverReader = new StreamReader(nsC);StreamWriter serverWriter = new StreamWriter(nsC);/输出索取IP客户的详细信息,待一下可以写日志c

15、lientComputerInfo dhcpClient = new clientComputerInfo();dhcpClient.ComputerMac = serverReader.ReadLine();dhcpClient.ComputerName = serverReader.ReadLine();dhcpClient.showDhcpClientInfo();trystring optionIp = serverReader.ReadLine();int option = Convert.ToInt32(optionIp);if (option = 1 /这里里在数据库中忘记了 设置“子网“+“网关“+“Dns地址“,记得加上.serverWriter.Flush();/进行分配后进行日志记录logInto.writeAssignIpInfo(dhcpClient, ipDataSet.Tables0.Rows00.ToString();elseserverWriter.WriteLine(“你选择有误!“);serverWriter.Flush();catch (Exception ex)logInto.writerException(ex);/ / 向客户端发放还没有用的IP/ / 是委

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号