Thursday, June 5, 2008

关于用ObjectInputStream封装socket输入流后程序阻塞的问题

下面的代码运行后在(2)和(3)处会阻塞住

public class Server {
public static void main(String[] args)throws Exception{
ServerSocket ssock=new ServerSocket(3000);
Socket sock=ssock.accept();

ObjectInputStream in=new ObjectInputStream(sock.getInputStream()); //(2)
ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream()); //(1)
while(true){
out.writeObject("s");
out.flush();
}
}
}

public class Client {
public static void main(String[] args)throws Exception{
Socket sock=new Socket("127.0.0.1",3000);
ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream());//(3)
ObjectInputStream in=new ObjectInputStream(sock.getInputStream()); //(4)
while(true){
out.writeObject("s");
System.out.println((String)in.readObject());
}
}
}

而把(1)和(2),(3)和(4)对调后程序运行正常。

原因如下:
使用缺省的serializetion的实现时,一个ObjectOutputStream的构造和一个ObjectInputStream的构造必须一一对应.ObjectOutputStream的构造函数会向输出流中写入一个标识头,而ObjectInputStream会首先读入这个标识头!

...
Read More

Saturday, April 19, 2008

wine强制全屏游戏在窗口模式下运行

wine explorer /desktop=1024x768 whatever.exe

...
Read More

Monday, April 7, 2008

用psyco优化python程序

psyco是python的一个扩展,它通过编译python代码来提升python程序的速度。

使用的方法很简单,在python程序里加上下面两句就可以了:

import psyco
psyco.full()

psyco会在程序运行时编译部分代码(具体怎么编译还不清楚),因此程序的初始化时间会增加,但运行速度会变快。

这里有一篇gentoo-wiki介绍用psyco优化portage速度的:

http://gentoo-wiki.com/TIP_Speed_up_portage_with_Psyco

这篇文章中说,用了psyco以后,portage性能可以提升2-5倍,不过同时也警告说psyco现在还不稳定,可能会带来很多问题,因此不推荐使用。

我试了一下,用了psyco的emerge速度快多了(看来2-5倍不是吹的),有一些不影响使用的小问题,比如没有用sudo的emerge会抛异常、确认删除时选“no”也会抛异常。我猜测这都是因为psyco对try-except语句支持得不好,原来的程序里catch住的异常现在引发了psyco的新异常。

不过这些问题都不影响正常使用。继续观望。

另外,有些别的python程序原生支持psyco,像Frets On Fire,我看了它的源码,它会初试使用psyco。我装了psyco以后再玩Frets On Fire,感觉快了一点,不过没有portage那么明显。

...
Read More

Friday, April 4, 2008

HOWTO XDMCP in Gnome

XDMCP stands for "X Display Manager Control Protocol" and is a network protocol. It allows a way of running X-Terminal on your PC (or Mac) and uses the X Server to provide a client/server interface between display hardware (the mouse, keyboard, and video displays) and the desktop environment while also providing both the windowing infrastructure and a standardized application interface. The X-Terminal can be displayed with an individual window or multiple windows, based on your X window system's software capabilities and setup.


On your X-Server, you must tweak the configuration of your graphical login manager (displaymanager).

for GDM, you need to change the [xdmcp]-section in your configfile:
/etc/X11/gdm/custom.conf

[xdmcp]
Enable=true

Depending on how your gdm.conf/custom.conf is set up, you might also need to add or change the [greeter] section as shown below. Without this, you will not be able to XDMCP in to your server.

[greeter]
Browser=true


How to use XDMCP

on the remote machine you can either use Xnest or simply X:
$ X -ac -query servername :1
or
$ Xnest -ac -query servername :1

where servername is your servername or IP. :1 is the display number. (:0 should be your running X-Server)

To change screen resolution of Xnest, use "geometry" option:
$ Xnest -geometry=1280x800 -ac -query servername :1

...
Read More

Sunday, March 30, 2008

用Python删除Eclipse的旧插件

Eclipse升级插件以后似乎没有办法删除旧版本的插件。

于是我写了个python脚本清除旧插件。

import os
import glob

def removeOldFiles(dir):
  os.chdir(dir)
  files = glob.glob('*')
  files.sort()
  prevfile = files[0]
  for filename in files[1:]:
    if (filename.find(prevfile.partition('_')[0]) == 0):
      os.system('rm -rf ' + prevfile)
    prevfile = filename

# main
removeOldFiles('/usr/lib/eclipse-3.3/plugins')
removeOldFiles('/usr/lib/eclipse-3.3/features')


...
Read More

Friday, March 28, 2008

Java程序和X程序无法调用scim输入的解决办法

最近发现Java程序都无法输入中文,按ctrl+space不弹出scim。

然后发型纯X程序(像xterm)也是一样的症状,而gtk和qt程序则没问题。

上网查了一下,了解到java程序和x程序一样是用xim引擎进行输入的,而qt/gtk是用scim,因此觉得是配置文件写错了。

检查了一下/etc/X11/xinit/xinitrc.d/xinputrc文件,果然把XMODIFIERS一行写成小写@im=scim了,而这是大小写敏感的。现在正确的内容如下:

XMODIFIERS=@im=SCIM
XIM="scim"
XIM_PROGRAM="scim"
XIM_ARGS="-d"
GTK_IM_MODULE="scim"
QT_IM_MODULE="scim"

export XIM XIM_PROGRAM XMODIFIERS GTK_IM_MODULE QT_IM_MODULE

# start xim server
$XIM_PROGRAM $XIM_ARGS &

...
Read More

Tuesday, March 25, 2008

Programmer's Fonts

Programmers have very particular font needs. A font for programming must be monospaced, extremely readable, and must sharply distinguish between similar characters, such as capital O and zero and the number 1, capital I, and lowercase l. In addition, good programming fonts allow you to view more lines of code on screen at once.

You can find many programmer's fonts at this website:
http://www.lowing.org/fonts/

view the sample of the fonts

...
Read More

Thursday, March 20, 2008

HOWTO Highlight C++/Java/TeX source code syntax in LaTeX

There is a greate package which supports syntax highlighting for a huge amount of programming languages (Fortran, C, C++, csh, HTML, Java, Matlab, Mathematica, Pascal, Perl, SQL, XML, Delphi, PHP, VBScript, SAS and even Latex itself - and many more). The usage is simple: 
Load the package: \usepackage{listings} 
Set the language: \lstset{language=TeX} 
Open a lstlisting environment: \begin{lstlisting} 
Include all your programming code 
Close the lstlisting environment: \end{lstlisting} 

If this does not seem to work for your language, even if the language is listed above, try upgrading to the lastest MiKTeX version. The listings package is being updated continuously. 

Here's a more advanced example on the source code highlight: 
 \usepackage{color}
 \usepackage{listings}
 \definecolor{Brown}{cmyk}{0,0.81,1,0.60}
 \definecolor{OliveGreen}{cmyk}{0.64,0,0.95,0.40}
 \definecolor{CadetBlue}{cmyk}{0.62,0.57,0.23,0}
 \begin{document}
 
 \lstset{language=R,frame=ltrb,framesep=5pt,basicstyle=\normalsize,
  keywordstyle=\ttfamily\color{OliveGreen},
 identifierstyle=\ttfamily\color{CadetBlue}\bfseries, 
 commentstyle=\color{Brown},
 stringstyle=\ttfamily,
 showstringspaces=ture}

\begin{lstlisting}
...
\end{lstlisting}

...
Read More

HOWTO Editing Gnome/KDE Menus

Introducion

You know the menu that's shown if you start the kde-menu, gnome-menu, xfce-menu or something else. There's no real name for this kind of menu (it's also often called "root-menu"), but the menu is saved in a Freedesktop-Standard. 

Where are the files saved?

The menus are made out of a "Main file" (.menu file). The file is written in XML, and it contains a kind of links to the Applications (.desktop files). 

The Main file detailing system-wide default menu structure might be located in one of the followings directorys: 
/etc/xdg/menus/ 
User-specific menu edits: ~/.config/menus/ 
If you have not found your file yet, search for it: find / -iname *.menu 

System-wide Menu entries (.desktop files) are stored in many places, for example: 
/usr/share/applications/ 
/usr/share/applink/
Gnome applications:
/usr/share/gnome/apps/
KDE applications: 
/usr/kde/3.5/share/applications/
User-specific applications: 
~/.local/share/applications/

Or search for the files: find / -iname *.desktop

...
Read More

Monday, March 17, 2008

HowTo: Mount Bin/Cue files in Linux

* Method 1: Use CDEmu.
cdemu 0 pro.cue
mount -t iso9660 /dev/cdemu/0 /mnt/temp

* Method 2: Convert to ISO.
Convert BIN/CUE to ISO:
bchunk -v acrobat6pro.bin pro.cue pro

* Just mount the ISO by executing as root.
mount -o loop,ro -t iso9660 .iso
mount -o loop pro01.iso /mnt/temp

* Method 3: Use AcetoneISO2.

...
Read More
 
/* google analytics */