.vimrc

November 2, 2009 at 2:30 am (Uncategorized)

"show Line Number
set nu
"automatically indent
set ai
"change tab width from 8 to 2
set shiftwidth=2
"highlight search
set hls
syntax on

Permalink Leave a Comment

A multi-threaded socket-based server

October 18, 2009 at 2:38 pm (java, socket, thread)

The problem is old – How to implement a multi-threaded, socket-based server that will let you read and write to the client (for example a telnet terminal.

There are several problems with constructing such a server:

  1. You have to use threads because otherwise clients will be queued up waiting for a connection.
  2. Many of the publicly available examples (even some in some books) do not work for I/O servers. They are mostly developed around reading from a socket not writing to the socket as well. In our example we give a working read/write code example.
  3. You need to avoid race conditions when starting up a thread at times when connections are coming through thick and fast.

The code below will implement just such a server. It is intended for a head start, so is a template rather than a tutorial on how to write a server in Java. We have decided on port 4444 to listen on in the example, but you need to decide on a suitable port yourself.

The server will run, establishing connections from the port and echoing input received until it receives a line of input that is a full stop only “.”.

The Kieser.net team.
Honeypot: spam@kieser.net

http://www.kieser.net/linux/java_server.html


import java.io.*;
import java.net.*;

/**
 * Title:        Sample Server
 * Description:  This utility will accept input from a socket, posting back to the socket before closing the link.
 * It is intended as a template for coders to base servers on. Please report bugs to brad at kieser.net
 * Copyright:    Copyright (c) 2002
 * Company:      Kieser.net
 * @author B. Kieser
 * @version 1.0
 */
public class sample_server {

private static int port=4444, maxConnections=0;

// Listen for incoming connections and handle them

public static void main(String[] args) {

int i=0;

try{
ServerSocket listener = new ServerSocket(port);
Socket server;

while((i++ < maxConnections) || (maxConnections == 0)){
doComms connection;

server = listener.accept();
doComms conn_c= new doComms(server);
Thread t = new Thread(conn_c);
t.start();
}
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}

}

class doComms implements Runnable {
 private Socket server;
 private String line,input;

 doComms(Socket server) {
 this.server=server;
 }

 public void run () {

 input="";

 try {
 // Get input from the client
 DataInputStream in = new DataInputStream (server.getInputStream());
 PrintStream out = new PrintStream(server.getOutputStream());

 while((line = in.readLine()) != null && !line.equals(".")) {
 input=input + line;
 out.println("I got:" + line);
 }

 // Now write to the client

 System.out.println("Overall message is:" + input);
 out.println("Overall message is:" + input);

 server.close();
 } catch (IOException ioe) {
 System.out.println("IOException on socket listen: " + ioe);
 ioe.printStackTrace();
 }
 }
}

Permalink Leave a Comment

use notepad++ to complie and run java program

October 2, 2009 at 6:43 pm (java) ()

create a new file, make.bat, to directory where you installed notepad++ .
below is the content of the make.bat file:
cd "%1"
javac %2
java %3

turn to notepad++ and press F5. type the command:

$(NPP_DIRECTORY)\make.bat $(CURRENT_DIRECTORY) $(FILE_NAME) $(NAME_PART)

after pressing save button, you can assign a hotkeys to this command.

Permalink Leave a Comment

extraneous `int’ ignored 的問題

September 2, 2009 at 8:25 pm (cpp)

Forgetting the semicolon at the end of a class definition is a syntax error.
i.e

class Test{
XXX
};

Permalink Leave a Comment

make Visio & GSview file to eps

August 17, 2009 at 4:01 pm (Uncategorized)

今天學長要我們利用visio畫圖,然後要想辦法轉成eps以便latex使用。
本來學長是說visio可以存成ps檔,再利用GSview把ps轉成eps。
但是visio 2003已經沒辦法存成ps/eps檔了。我直接改副檔名成ps,結果GSview卻不能讀檔。

解決方法vsd -> ps  ->  eps

  1. 先安裝Adobe Universal PostScript Windows Driver,當安裝程式進行至印表機連接埠選擇時設定為File埠
  2. 用Visio開啟畫好的圖,選擇PostScriptpPrinter當印表機來列印。若沒有點選列印至檔案的話,雖然會自動問你檔案名稱,但是不會幫你設定副檔名,而檔案會存放在vsd檔的同一資料夾下。
  3. 用GSview開起檔案,點[File]->[PS to EPS]。


Permalink Leave a Comment

LaTex: 中文格式

August 7, 2009 at 5:09 pm (LaTex)


\documentclass[12pt,a4paper]{article}
\usepackage{CJKutf8} %舊版的改用CJK
\begin{document}
\begin{CJK}{UTF8}{cwmu} %還有 cwku cwhbu cwyu cwfsu
這是中文範本
\end{CJK}
\end{document}

Permalink Leave a Comment

LaTex: bibliography

August 7, 2009 at 5:04 pm (LaTex)

  1. 利用JabRef建立bib檔
  2. 然後在tex最後加上下面的code。其中的XYZ要改成bib檔的檔名。
  3. 接下來只要在引用的地方加上\cite{bibtexkey}。bibtexkey是在JabRef中設定的。
  4. compile流程:pdflatex->bibtex->pdflatex->pdflatex
\bibliographystyle{plain}
\bibliography{XYZ}

資料來源

Permalink Leave a Comment

ACM 458 – The Decoder

August 4, 2009 at 3:21 pm (Uncategorized) (, )

這幾天開始玩acm,發現用java寫很不方便,java在輸出上有不同的方式,但輸出結果看起來都會一樣。
大家所說的芭樂題,我卻在輸出上被打敗。

這題的重點是在System.out.print與System.out.write的不同。
對於ascii而言,用最原始的InputStream與OutputStream是比較好的方法。
也許是acm在自己的server上測試時,有用到一些特殊字吧。

import java.io.*;
public class Main {
     public static void main(String[] args) throws IOException {
          int notChar = 0;
          while( (notChar = System.in.read()) != -1 ){
               if( notChar == 10 )
                    System.out.print("\n");
               else
                    System.out.write(notChar-7);
          }
     }
}

Permalink Leave a Comment

LaTeX Error: Unknown graphics extension: .eps.

August 3, 2009 at 5:44 pm (Uncategorized) (, )

原因是pdflatex並不能夠識別.eps檔

解決方式為將.eps檔轉換為.pdf檔

使用latex把tex轉換為dvi檔
再把dvi檔轉成pdf檔

Permalink Leave a Comment

usb

July 8, 2009 at 4:21 pm (Uncategorized)

裝置名稱: [J:]USB Mass Storage Device(TDKMedia Trans-It Drive USB Device)

PNP裝置ID: VID = 0718 PID = 0624
裝置序號: 07960D03C895014D
裝置版本: PMAP

裝置類型: 標准USB裝置 – USB2.0高速

晶片製造商: phison(群?)
晶片型號:

產品製造商: TDKMedia
產品型號: Trans-It Drive

Permalink Leave a Comment

Next page »