存档

‘技术’ 分类的存档

Windows XP 提示错误“内存或系统资源不足”如何解决?

2009年9月29日 1,660 views 没有评论

以前 bbs 里的一篇老的帖子,现在这里也保留一份。

=========================================

[Q]: 操作系统:Wiondows XP。  

现象:窗口开多了以后,经常出现这样的提示: “内存或系统资源不足。请关闭一些窗口或程序,然后再试。” 

在Google里面一搜就会找到很多的解释,比如下面的: 
http://zhidao.baidu.com/question/9054809.html?fr=qrl3

但是在我的情况中,系统有2G内存,平常使用也就用了1G~1.5G左右, 
只是窗口开的比较多,但是内存绝对是够的! 系统没有中毒,也没有安装什么特别的软件。 

一个很简单的重现这个问题的办法: 

用 “Win键 + E” 不断的打开资源管理器,会发现打开到40多个的时候,就开始出现不正常了。 
这个时候报的错误就是:“内存或系统资源不足。请关闭一些窗口或程序,然后再试。” 
其实这个时候的内存还是绰绰有余的,是双核+2G的机器。

在这里可以找到一篇文章,提到了GDI泄漏的一个bug,但是安装了该hotfix以后,问题依旧。 
http://www.cakewalk.com/Support/kb/kb2005243.asp


[A]:经高人指点,找到了问题的解释和解决方案:

http://weblogs.asp.net/…/max-num-of-open-windows-under-xp-2003-vista-resolved.aspx

注意 XP SP2 的 SharedSection 已经设成1024,3072,512了,只需要增大第二个数字就可以,另外改注册表以后要重启。 

SharedSection 的解释:http://support.microsoft.com/kb/184802/en-us


分类: 技术 标签:

如何给 AdventNet Simulator 增加新的表以及数据

2009年9月28日 524 views 没有评论

AdventNet Simulator 是一个模拟 SNMP Agent 的工具,支持常规的 SNMP Get / GetBulk / Set 等操作,对于网管软件的开发很有帮助。用这个工具,可以做到不需要实际的设备,而只需要设备相关的 SNMP mib 文件就可以模拟出一个“真实”的节点设备。在实际的开发过程中,往往网管和设备是同时开始的,在设备开发还没有结束的情况下,网管就可以用模拟器先模拟出一个设备进行基本的集成测试。

这里假定已经在 AdventNet Simulator 里有了一个模拟出的设备了,并且选择的方式是 XML 方式。那如果 mib 文件发生了改动,这个时候通常的做法是先 Stop Agent,然后在左边的 mib 树上选中要更新的 mib,点击 Unload Mib File,之后再重新加载就可以了。但是这样做带来的一个问题是之前配置的所有的数据可能都会丢掉,需要重新配,比较麻烦。

如果改动的 mib 不是很多的话,也可以直接修改 xml 文件来使模拟的设备支持新的 mib 表。

假设<SimulatorDir>是 AdventNet Simulator 的安装目录,<DeviceName>是模拟的设备的名称,新增的表名字叫 NewMibTable。做法如下:

  1. 进入 <SimulatorDir>/snmpagents/<DeviceName> 目录
  2. 打开 <DeviceName>_netSimMapping.xml,参考该文件中已有的其他表项,添加新增表的定义。
  3. 打开 <DeviceName>_netSimRegister.xml,同2,参考文件中已有的其他项,添加新增增表的定义。
  4. 找一个跟新增的项比较接近的 mib 表,拷贝粘贴重命名,生成 <DeviceName>_NewMibTable.xml,<DeviceName>_NewMibTable1.xml,<DeviceName>_NewMibTable_org.xml,<DeviceName>_NewMibTable1_org.xml,同样修改里面的数据,以适合这个新增表各个字段的类型和数值。

按照上述步骤,做完了之后再重新 Start Agent,然后验证下看这个新增的表格增删改数据有没有问题。如果没有问题,那恭喜你,已经成功了。如果不能增加修改的话怎么办呢?那就耐心点,再检查下上面的一些配置文件是不是都配置的正确,有配的不对的就改掉,应该就好了。

另外,如果想实现其他的一些目的,比如不在模拟器 UI 里面增加数据,而直接修改 XML 文件等等;有了上面的经验,已经也能够想到办法的了。


分类: 技术 标签: ,

Java 语言没有 unsigned 类型

2009年9月24日 2,076 views 没有评论

跟 C 语言不同,Java 中没有 unsigned 类型,这点可能让很多人都有过困扰。

那如果碰到确实需要用到 unsigned 类型的情况怎么办呢? 通常的办法就是用更大一号的类型来处理,比如:

  • 处理 unsigned byte,就用 int;
  • 处理 unsigned int,就用 long;
  • 处理 unsigned long,这个不好办,只能用 BigInteger 了。但是要注意的是,Java 中的 long 是 8 bytes,而 C 语言只有4 bytes,所以碰到这种情况的话,那就等同于上面 unsigned int 的情况了。

另外,Java 中比 C 语言增加了一种不带符号的移位操作: >>> ,固定的用 0 来填充左边高位的空缺,这在一定程度上减轻了没有 unsigned 类型带来的影响。


扩展阅读:这里有篇文章介绍了 Java 中怎么处理 unsigned 类型的。文中提到的一篇对 C /C++/Java 语言创始人的访谈中提到了 Java 中没有定义 unsigned 类型的原因,见最后 Gosling 的部分:

Q: Programmers often talk about the advantages and disadvantages of programming
in a "simple language."  What does that phrase mean to you, and is [C/C++/Java] a
simple language in your view?

Ritchie: C (and the others for that matter) are simple in some ways, though they are
also subtle; other, somewhat similar languages like Pascal are arguably simpler. What
has become clear is that aspects of the environment like libraries that aren't part of
the core language are much bigger and more complicated. The 1999 C standard grew
hugely more in the library part than in the language; the C++ STL and other things
are big; AWT and other things associated with Java are too.

Stroustrup: I see three obvious notions of "simple:" to be easy to learn, to make it
easy to express ideas, and to have a one-to-one correspondence to some form of
math. In those terms, none of the three languages is simple. However, once mastered,
C and C++ make it easy to express quite complex and advanced ideas -- especially
when those ideas have to be expressed under real-world resource constraints.

Gosling: For me as a language designer, which I don't really count myself as these days,
what "simple" really ended up meaning was could I expect J. Random Developer to
hold the spec in his head. That definition says that, for instance, Java isn't -- and
in fact a lot of these languages end up with a lot of corner cases, things that nobody
really understands. Quiz any C developer about unsigned, and pretty soon you discover
that almost no C developers actually understand what goes on with unsigned,
what unsigned arithmetic is. Things like that made C complex.
The language part of Java is, I think, pretty simple. The libraries you have to look up.


分类: 技术 标签:

用 ImageMagick 转换图片大小

2009年9月16日 802 views 没有评论

ImageMagick 是一套很好的图片处理工具包,而且开源免费,使用非常广泛。

贴一个转换图片大小的命令,可以批量的对很多文件进行瘦身,比较实用。

Linux 下:

find . -name '*.JPG' -print -exec convert {} -resize 1024x768 ../resized_1024x768/{} \;

Windows 下:

for %I in (*.jpg) do convert %I -resize 1024x768 ..\resized_1024x768\%I

Windows Cygwin 下:

find . -iname "*.jpg" | xargs -l -i convert -resize 800x600 {} ../resized_800x600/{}


另外,因为镜头的横拍和竖拍的关系,有些图像是侧的,需要90度翻转下,不然就得把脑袋侧过来看了:-)

也有个命令行的小工具 jhead 可以帮助自动的校正图像的翻转,需要注意的是还需要配合 jpegtran,把两个小工具下载放到在 %PATH% 中的某个目录下,然后执行下面的命令就好了。

# Windows / Linux 处理当前目录中的图片
jhead -autorot *.jpg
 
# Windows 下处理当前目录及子目录中的图片
jhead -autorot **\*.jpg
 
# Linux 下处理当前目录及子目录的图片。 注意 JPG 大小写
find ./ -name '*.JPG' -exec jhead -autorot {} {} \;


除了改变图像旋转之外,jhead 还可以通过命令行提取和修改图像的 Exif 信息,具体用法见其主页,这里也提供下打包下载

当然,使用一些其他的工具也是可以实现同样的目的的。方法有很多种,不管黑猫白猫,抓到老鼠的就是好猫。这几个命令的好处是比较方便写批量的脚本。


分类: 技术 标签: ,

在 Java 程序中释放 RMI 端口

2009年9月14日 1,006 views 没有评论

通过 LocateRegistry.createRegistry(port) 创建了一个 rmiregistry 监听 port 所在端口,

如果想要在程序中把这个端口关闭,也就是关闭 RMI registry 服务,可以参考以下的代码。

注:似乎在2003年的时候,这个方法还行不通,到了 JDK6 的版本才解决这个问题的。

参考 Sun bug 4457683bug 4508962, 以及 这个 mail archive


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.rmi.NoSuchObjectException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
 
public class RMITest {
 
	public void testRMIPortRelease() {
		System.out.println("Testing port release");
 
		System.out.println("Acquiring port... ");
		Remote reg = null;
		try {
			reg = LocateRegistry.createRegistry(1102);
		} catch (RemoteException ex) {
			ex.printStackTrace();
		}
 
		System.out.println("releasing port... ");
		try {
			java.rmi.server.UnicastRemoteObject.unexportObject(reg, true);
		} catch (NoSuchObjectException ex1) {
			ex1.printStackTrace();
		}
		reg = null;
	}
 
	public static void main(String[] args) {
		RMITest s = new RMITest();
		s.testRMIPortRelease();
	}
}


分类: 技术 标签: ,

[转] Java Prime number check

2009年9月10日 728 views 没有评论

原文出处: http://ventrix.nsdc.gr/code_folds/2008/10/07/java-prime-number/

Information about the author: http://kospol.nsdc.gr/


========================================================================

This is one approach to check if the given number is a prime number. It can take a very big number as an argument. It uses no threads and has no comments. Think it as version 0.01.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 *      Prime.java
 *
 *      Copyright 2008 Ventrix <ventrix@gmail.com>
 *
 *      http://ventrix.nsdc.gr/code_folds/
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */  
 
import java.math.BigInteger;  
 
public class Prime {  
 
    private static long start;
    private static long end;  
 
    public static void main(String[] argv) {
        boolean isprimen;
        //http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html#isProbablePrime(int)
        //http://primes.utm.edu/lists/small/small.html
        //for i in `seq 1 1000`; do java Prime $i >> primes; done
        //cat primes | grep "is a"
        start = System.currentTimeMillis();
        try {
            BigInteger bigNumber = new BigInteger(argv[0]);
            if (bigNumber.compareTo(new BigInteger("2147483647")) == 1) {
                if (bigNumber.compareTo(new BigInteger("9223372036854775807")) == -1) {
                    Long longNumber = new Long(argv[0]);
                    bigNumber = null;
                    isprimen = isNorPrime(longNumber);
                } else {
                    isprimen = isBigPrime(bigNumber);
                }
            } else {
                bigNumber = null;
                Integer intNumber = new Integer(argv[0]);
                isprimen = isPrime(intNumber);
            }  
 
            if (isprimen) {
                System.out.println(argv[0] + " is a prime!");
            } else {
                System.out.println(argv[0] + " is NOT a prime!");
            }
            end = System.currentTimeMillis();
            System.out.println("Completed in +" + (end - start) + "ms");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }  
 
    private static boolean isBigPrime(BigInteger number) {
        System.out.println("You gave me a BIG number");
        BigInteger[] remain;
        remain = number.divideAndRemainder(new BigInteger(new Integer("2").toString()));
        if (remain[1].compareTo(new BigInteger("0")) == 0) {
            return false;
        }
        remain = number.divideAndRemainder(new BigInteger(new Integer("3").toString()));
        if (remain[1].compareTo(new BigInteger("0")) == 0) {
            return false;
        }
        int y = 2;
        int x = (int) Math.sqrt(number.doubleValue());
        for (int i = 5; i <= x; i += y, y = 6 - y) {
            remain = number.divideAndRemainder(new BigInteger(new Integer(i).toString()));
            if (remain[1].compareTo(new BigInteger("0")) == 0) {
                return false;
            }
        }
        return true;
    }  
 
    private static boolean isNorPrime(Long number) {
        System.out.println("You gave me a normal number");
        if (number % 2 == 0) {
            return false;
        }
        if (number % 3 == 0) {
            return false;
        }
        int y = 2;
        int x = (int) Math.sqrt(number);
        for (int i = 5; i <= x; i += y, y = 6 - y) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }  
 
    private static boolean isPrime(Integer number) {
        System.out.println("You gave me a small number");
        if (number < 2) {
            return false;
        }
        if (number == 2) {
            return true;
        }
        if (number % 2 == 0) {
            return false;
        }
        if (number == 3) {
            return true;
        }
        if (number % 3 == 0) {
            return false;
        }
        int y = 2;
        int x = (int) Math.sqrt(number);
        for (int i = 5; i <= x; i += y, y = 6 - y) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
}//class</ventrix@gmail.com>


分类: 技术 标签:

Windows XP 下 Chrome 浏览器 SOCKS 代理设置

2009年9月10日 1,968 views 没有评论

时间真的过的很快,Google Chrome 浏览器出来也有一年多了。一出来的时候就装了用过,却发现还是有很多待完善的地方,很多方面还是比不上 Firefox,也不是很稳定,除了速度快这个最大的优点以外。

但是最近重新装了 3.0.x 的 DEV 版和 4.0.x BETA 版的 Chrome 以后,真的觉得很不错,浏览器本身的速度和打开网页的速度仍旧很快,甚至更快了,而且一些以前棘手的问题也在一个个的被消灭了,比如这个点击 Tab 会导致到另外一个独立的 Window 的bug,也可以像 Firefox 一样点击 Ctrl + Shift + T 打开之前关闭的 Tab 页了,对 Linux 的支持也越来越好,用了原生的 GTK;MAC 的版本也在稳步前进中,一些新的功能比如插件的支持也在实现了并在不断的完善中,开发文档也发布了,等等等等。所以最近用 Chrome 越来越多,其他的浏览器反而少了。

不过还是有个问题非常困扰,就是不支持 SOCKS 代理。因为在公司上网,由于政策的限制,有些网站是被屏蔽的,幸好有一些代理可以用。IE 下用 PAC 文件代理设置,只要类似于 “SOCKS x.x.x.x:1080” 就可以了,Firefox 下用插件 FoxyProxy,问题都不大。

Chrome 的代理设置用的是跟 IE 一样的系统代理设置,结果发现通过 SOCKS 代理出去的时候,总是报错:

  • 错误 320 (net::ERR_INVALID_RESPONSE): 未知错误。

所以访问那些被屏蔽的网站,都需要特地在开 IE 才行,很是不方便。

后来注意到其他人给 Chrome 提的那个 issue 被解决了,但是试过,Windows 下还是不行,Linux 下倒是可以的了。

再过了半个月,看这个问题就要沉下去了,就也去那里提了自己的问题, 在跟回复的人讨论了之后,才发现了一个解决的办法,其实只要用 “SOCKS5 x.x.x.x:1080” 就可以了,其中一定要写上 5 这个版本。

办法虽然是找到了,但其实还是有两个疑问的:

  1. 为什么 IE 下面不用配置 socks5 的版本但是代理也是用的好好的?
  2. 即使这个方案,也还是不够完善,代理工作的并不是很稳定,时好时坏,坏的时候,点“刷新”,网卡上都抓不到包的,也就是说 Chrome 并没有发出浏览的请求包。

不管怎样,总算是找到了一个让 WIndows 下 SOCKS 代理工作的办法了,大部分时候也都还是可以用的。

真的是希望自己也有能力,可以把第2个问题自己查出来解决掉,呵呵。


分类: 技术 标签: , ,

[转] Wireshark的Pcap文件格式分析

2009年9月7日 1,937 views 没有评论

转自:http://blog.chinaunix.net/u2/82392/showart_1870732.html

前段时间因工作要求,需要对各种数据包进行分析和操作,内容涉及网路协议分析,socket,文件操作等。在此分享下学习和实践的经验。

首先介绍下网络抓包、协议分析的必备软件Ethereal,新版(Wireshark)以下还以 Ethereal代之,目前最新版本已经支持在无线局域网抓包了。Linux和Windows均有对应安装包,它们分别是gcc和VC++编译的。不过 Windows下是基于Winpcap而Linux下则是Libcap。Ethereal作为网路协议分析、学习、开发的敲门软件,其使用技巧及其原理机 制(BPF)网上都有比较详尽的介绍,当初我收集的相关资料随后也会上传,不再多说。下面主要介绍下Ethereal默认的*.pcap文件保存格式。

Pcap文件头24B各字段说明:

Magic:4B:0x1A 2B 3C 4D:用来标示文件的开始
Major:2B,0×02 00:当前文件主要的版本号
Minor:2B,0×04 00当前文件次要的版本号
ThisZone:4B当地的标准时间;全零
SigFigs:4B时间戳的精度;全零
SnapLen:4B最大的存储长度
LinkType:4B链路类型
常用类型:
0 BSD loopback devices, except for later OpenBSD
1            Ethernet, and Linux loopback devices
6            802.5 Token Ring
7            ARCnet
8            SLIP
9            PPP
10           FDDI
100         LLC/SNAP-encapsulated ATM
101         “raw IP”, with no link
102         BSD/OS SLIP
103         BSD/OS PPP
104         Cisco HDLC
105         802.11
108         later OpenBSD loopback devices (with the AF_value in network byte order)
113         special Linux “cooked” capture
114         LocalTalk
 
Packet 包头和Packet数据组成
字段说明:
Timestamp:时间戳高位,精确到seconds
Timestamp:时间戳低位,精确到microseconds
Caplen:当前数据区的长度,即抓取到的数据帧长度,由此可以得到下一个数据帧的位置。
Len:离线数据长度网络中实际数据帧的长度,一般不大于caplen,多数情况下和Caplen数值相等。
Packet 数据:即 Packet(通常就是链路层的数据帧)具体内容,长度就是Caplen,这个长度的后面,就是当前PCAP文件中存放的下一个Packet数据包,也就 是说:PCAP文件里面并没有规定捕获的Packet数据包之间有什么间隔字符串,下一组数据在文件中的起始位置。我们需要靠第一个Packet包确定。 最后,Packet数据部分的格式其实就是标准的网路协议格式了可以任何网络教材上找得到。
 



========================================================

这里再把pcap文件头的结构定义添上:

typedef unsigned long  DWORD;
typedef unsigned short  WORD;
typedef unsigned char  BYTE;
 
typedef struct PCAP_FILE_HEADER
{
 DWORD magic;
 WORD versionMajor;
 WORD versionMinor;
 DWORD thisZone;
 DWORD sigfigs;
 DWORD snapLen;
 DWORD linkType;
}PcapFileHeader, *PPcapFileHeader;


2011-02-17 补充两篇文章:

1)解析Winpcap截获的数据包

2)以太网帧类型速查表

分类: 技术 标签:

Wireshark hub 下抓不到包问题解决

2009年9月7日 1,446 views 1 条评论

在同一个 hub 下接笔记本和机顶盒(STB),需要在笔记本上抓包查 STB 的问题。

如果不用 capture filter ,能抓到经过 hub 的所有的包,包括 http 的。

因为包太多,想只过滤出 http 的包用于分析,于是加 capture filter 为 “port 80“, 结果一个包都抓不到了。

尝试了各种办法,都没有结果。于是到 Wireshark 的 maillist 发帖求助,很快就有了结果

原来因为所接的 Hub 其实是电信的 Adsl 猫,有四个 RJ45 网口, STB 是通过猫拨号上网的,所以 capture filter 需要用 “pppoes && port 80“,其中的 pppoes 不可少!

从刚开始以为是 Wireshark 的一个 bug,到发帖求助,到得到结果,也就是短短几天的时间。

在这个过程中,对网络方面的知识也有了更多一点的了解。开源社区确实很好很强大,真正的牛人专家很多,参与其中确实能学到很多的知识。


分类: 技术 标签: ,

WordPress 时区的问题

2009年8月31日 1,554 views 7 条评论

WordPress 安装好了以后就没有改过时区的设置,今天 baobao 留言,发现显示的留言时间不对,这才注意到设置不对,wp默认的是UTC时区。于是改成了 Shanghai, 但是发现还是差5个小时,看来是web服务器所在的时间跟中国本地的时间不一致导致的,于是选择 Manual的方式,+13。这下可以了。

p.s. 另外注意到一个小问题,在分钟上还差几分钟,本地的时间应该是对的,难道是服务器的时间不准确? :?

顺便贴个搜到的关于时区知识的链接:UTC时间与北京时间的差多久?,讲的很详细,作者也是曾碰到了同样的问题。


更新 2010-05-04:

这里也有一篇讲时区问题的(参考链接),提到了3种解决的办法,值得一看。


分类: 技术 标签: