以键/值格式管理 IP 和端口的想法?
我正在寻找一种良好且快速的方法来管理文件中的 IP 地址和端口。对具有 2 列的数据库表进行排序:IP 和端口,但在文件中,不使用数据库。
它必须支持添加、删除和更新。我不在乎并发性。
I'm looking for a good and fast way to manage IP addresses and ports in a file. Sort of a DB Table that has 2 columns: IP and Port, but in a file, without using a DB.
It has to support adding, deleting and updating. I don't care from concurrency.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
下面,有的来完成你的任务。我试图严格抓住要点,所以也许遗漏了一些东西。
我要创建一个“Record”类,以保留 ip/port 对
...以及一个“DatabaseFile”类来管理数据文件交互。
下面是一个工作示例:
dBase III,我想你。
嗯,这很有趣,谢谢!
编辑1:添加了
Pack()
和惰性Sort()
代码;编辑2:添加了缺少的
IComparable/IComparer
实现Below, some come to complete your task. I tried to go strictly to the point, so maybe something is missing.
I'd to create a "Record" class, to keep ip/port pairs
...And a "DatabaseFile" class to manage datafile interaction.
Below, a working example:
dBase III, I miss you.
Well, that was fun, thank you!
EDIT 1: Added
Pack()
and lazySort()
code;EDIT 2: Added missing
IComparable/IComparer
implementation我个人会选择
192.100.10.1:500:20-21
192.100.10.2:27015-27016:80
其中第一个是 Ip 以及
之后的所有内容>:
是一个端口,我们还可以用-
表示一个范围,如果我们想对此非常疯狂,我们可以引入一个u
来表示端口类型UDP
或TCP
例如:192.100.10.2:27015-27016:80:90u
和
explode() 很容易满足上述要求。
当谈论插入删除和更新时..我们可以简单地创建一个类结构,例如
然后你可以让 main 看起来像
I personally will go for
192.100.10.1:500:20-21
192.100.10.2:27015-27016:80
Where the first is the Ip and every thing after the
:
is a port, We can also represent a range by-
and if we want to be very crazy about it we can introduce au
which will represent the port typeUDP
orTCP
for example:192.100.10.2:27015-27016:80:90u
And
explode()
would work for the above quite easily.When talking about Inserting Deleting and updating.. We can simply create a class structure such as
And then you can have the main to look like
最简单的方法可能是创建一个包含您的 IP 和端口的小类
,然后创建它们的
列表
。然后,您可以使用 XML 序列化和反序列化来读取和写入列表文件。The easiest way is probably to create a small class that contains your IP and port
and then create a
list<IpAddress>
of them. You can then use XML Serialization and Deserialization to read to and write from a file your list..NET BCL 不提供您正在寻找的内容,因为您想要查询文件而不先将其加载到内存中并支持添加/删除。因此,您要么必须推出自己的嵌入式数据库,要么可以简单地使用 SQLite http://www.sqlite 之类的东西.org/
The .NET BCL does not offer what you are looking for as you want to query against a file without loading it into memory first and support add/remove. So you'd either have to roll your own embedded database or you could simply use something like SQLite http://www.sqlite.org/
IP和端口是一对多的关系。我会考虑这样的
\t192.168.1.1\r\n25\r\n26\r\n\t192.168.1.2\r\n2\r\n80\r\n110
其中 \t 是一个制表符, \ r\n 是一个回车符,后跟一个换行符
因此,当您解析时,如果您点击制表符,您就知道该行中从那里到换行符的所有内容都是 IP 地址,然后下一个换行符之间的所有内容都是端口该 IP 地址的编号,直到您点击选项卡,在这种情况下您将使用新的 IP 地址。这既简单又快速,但不那么人类可读。
IP and Port is a one to many relationship. I would consider something like this
\t192.168.1.1\r\n25\r\n26\r\n\t192.168.1.2\r\n2\r\n80\r\n110
where \t is a tab and \r\n is a carriage return followed by a newline
So when you parse, if you hit a tab character, you know everything that's in that line from there to the newline is an IP address, then everything in between the next newlines is a port number for that IP address until you hit a tab, in which case you're on a new IP address. That's simple and fast but not as human readable.
这与IP和端口无关。问题是,据我所知,Windows不允许在文件中间插入或删除字节。
this has nothing to do with IP and ports.. the problem is that, as far as i know, windows does not allow to INSERT or remove bytes in/from the middle of a file..