2007年3月18日 星期日

[Java] Socket



public class SocketServerTest implements java.lang.Runnable{
private int PORT = 52288;
private java.net.ServerSocket server = null;
private boolean shutdown = false;
public void run(){
try{
server = new java.net.ServerSocket(PORT);
try{
while(!shutdown){
java.net.Socket socket = server.accept();
try{
//非 140.113 就關掉
if( (( ((socket.getInetAddress()).getHostAddress()).substring(0,8)).compareTo("140.113.") != 0 ) ){
socket.close();
continue;
}

String datain = null;
java.io.BufferedReader in = new java.io.BufferedReader( new java.io.InputStreamReader( socket.getInputStream(),"UTF-8" ) );
datain = in.readLine();
}catch(java.net.SocketException e){
//socket關掉
}catch(java.lang.NullPointerException e){
//in.readLine()
}finally{
socket.close();
}
}
}catch(java.net.SocketException e){
//socket關掉
}finally{
server.close();
}
}catch(java.net.SocketException e){
//socket關掉
}catch(Exception e){
e.printStackTrace();
}
}
}


沒有留言:

張貼留言