C#模拟实现DHCP服务器.doc

上传人:hw****26 文档编号:3550425 上传时间:2019-06-04 格式:DOC 页数:11 大小:107KB
下载 相关 举报
C#模拟实现DHCP服务器.doc_第1页
第1页 / 共11页
C#模拟实现DHCP服务器.doc_第2页
第2页 / 共11页
C#模拟实现DHCP服务器.doc_第3页
第3页 / 共11页
C#模拟实现DHCP服务器.doc_第4页
第4页 / 共11页
C#模拟实现DHCP服务器.doc_第5页
第5页 / 共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; set computer

2、Name = 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 = null;priv

4、ate 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 (sqlConn.State

5、= 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 DataSet getD

6、ataSet(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为“ + assignIp +

7、“ 分配给了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() + “这是第一次发生异常:“ + ex.Mess

8、age);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(string args)Con

9、sole.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);listenerTh1.Sta

10、rt(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 = DateTime.Now.ToS

11、tring();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();broadCast.Wri

12、teLine(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(clientTcpcl

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

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

15、Info 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 /这里里在数据库中忘记了设置“ 子网 “+“网关“+“

16、Dns地址“,记得加上.serverWriter.Flush();/进行分配后进行日志记录logInto.writeAssignIpInfo(dhcpClient, ipDataSet.Tables0.Rows00.ToString();elseserverWriter.WriteLine(“你选择有误!“);serverWriter.Flush();catch (Exception ex)logInto.writerException(ex);/ / 向客户端发放还没有用的IP/ / 是委托ParameterizedThreadStart()中的Object参数,以便与此委托相匹配.priv

17、ate static void toClientWriter(object obj)NetworkStream nsC = obj as NetworkStream;StreamWriter serverWriter = new StreamWriter(nsC);DataSet selectDataSet = findIpObj.getDataSet(“SELECT ID,IP FROM DHCPIpPool WHERE IPStatus=0“, new SqlParameter0);string sendLine = string.Empty;for (int i = 0; i selec

18、tDataSet.Tables.Count; i+) /有多少个表for (int j = 0; j selectDataSet.Tablesi.Rows.Count; j+) /每个表有多少行for (int z = 0; z selectDataSet.Tablesi.Columns.Count; z+) /每个有多少列sendLine += selectDataSet.Tablesi.Rowsjz.ToString() + “ “;serverWriter.WriteLine(sendLine.ToString();serverWriter.Flush();sendLine = “;Th

19、read.Sleep(1);serverWriter.WriteLine(“请选择一个IP 的编号:“ );serverWriter.Flush();/*CREATE PROC selectIPID INT AS SELECT IP FROM DHCPIpPool WHERE ID=ID AND IPStatus=0UPDATE DHCPIpPool SET IPStatus=1 WHERE ID=ID*/使用 TcpClient 不完备实现 DHCP 客户端namespace clientstruct clientComputerInfoprivate string computerMac;

20、 /暂时还不知道如何取MAC地址,先用这个吧public string ComputerMacget return computerMac; set computerMac = value; private string computerName;public string ComputerNameget return computerName; set computerName = value; public clientComputerInfo(string computerName)puterName = computerName;puterMac = “00-00-00-00-00-0

21、0-00-E0“;public void showDhcpClientInfo()Console.WriteLine(“有一个DHCP客户端向你索要地址 “);Console.WriteLine(“计算机名为:“ + computerName);Console.WriteLine(“计算机mac地址:“ + computerMac);class Programstatic void Main(string args)Console.BackgroundColor = ConsoleColor.Red;IPAddress ip = IPAddress.Parse(“192.168.1.1“);

22、/原本是要“ 源:0.0.0.0目:255.255.255.255 端:67“,而且使用的UDP 端口 ,现在是使用TcpClient模拟IPEndPoint ipAndEnd = new IPEndPoint(ip, 68);TcpClient clientConnection = new TcpClient();clientConnection.Connect(ipAndEnd);Console.WriteLine(“连接DHCP服务器成功 !“);StreamReader clientReader = new StreamReader(clientConnection.GetStream

23、();StreamWriter clientWriter = new StreamWriter(clientConnection.GetStream();/向服务器申请,关于自己机器的详细信息,很快走完.Thread sendServerInfoTh = new Thread(new ParameterizedThreadStart(sendServerInfo);sendServerInfoTh.Start(clientWriter);/取得服务器的IP选择列表Thread getServerIpList = new Thread(new ParameterizedThreadStart(getServerIpListClient);getServerIpList.Start(clientReader);clientWriter.WriteLine(Console .ReadLine();clientWriter.Flush();

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

当前位置:首页 > 教育教学资料库 > 精品笔记

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。