¶ÁJAVAÍøÂç±à³Ì¾«½â ·þÎñÆ÷Óë¿Í»§¶ËͨÐÅÒ»
ÎÄÖÐÒýÓÃÁËËïÀÏʦµÄ´úÂ룬²¢×¢Ã÷¡£
import java.io.*;
import java.net.*;
public class EchoServer {
private int port=8888;
private ServerSocket serverSocket;
public EchoServer() throws IOException {
serverSocket = new ServerSocket(port);
System.out.println("·þÎñÆ÷Æô¶¯");
}
public String echo(String msg) {
return "echo:" + msg;
}
private PrintWriter getWriter(Socket socket)throws IOException{
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket)throws IOException{
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public void service() {
while (true) {
Socket socket=null;
try {
socket = serverSocket.accept(); //µÈ´ý¿Í»§Á¬½Ó
System.out.println("New connection accepted "
+socket.getInetAddress() + ":" +socket.getPort());
BufferedReader br =getReader(socket);
PrintWriter pw = getWriter(socket);
String msg = null;
while ((msg = br.readLine()) != null) {
System.out.println(msg);
pw.println(echo(msg));
if (msg.equals("bye")) //Èç¹û¿Í»§·¢Ë͵ÄÏûϢΪ“bye”£¬¾Í½áÊøͨÐÅ
break;
}
}catch (IOException e) {
e.printStackTrace();
}finally {
try{
if(socket!=null)socket.close(); //¶Ï¿ªÁ¬½Ó
}catch (IOException e) {e.printStackTrace();}
}
}
}
public static void main(String args[])throws IOException {
new EchoServer().service();
}
}
/****************************************************
* ×÷ÕߣºËïÎÀÇÙ *
* À´Ô´£º<<JavaÍøÂç±à³Ì¾«½â>> *
* ¼¼ÊõÖ§³ÖÍøÖ·£ºwww.javathinker.org *
**********************
Ïà¹ØÎĵµ£º
Java NIO APIÏê½â
ÔÚJDK
1.4ÒÔÇ°£¬JavaµÄIO²Ù×÷¼¯ÖÐÔÚjava.ioÕâ¸ö°üÖУ¬ÊÇ»ùÓÚÁ÷µÄ×èÈû£¨blocking£©API¡£¶ÔÓÚ´ó¶àÊýÓ¦ÓÃÀ´Ëµ£¬ÕâÑùµÄAPIʹÓúܷ½
±ã£¬È»¶ø£¬Ò»Ð©¶ÔÐÔÄÜÒªÇó½Ï¸ßµÄÓ¦Óã¬ÓÈÆäÊÇ·þÎñ¶ËÓ¦Óã¬ÍùÍùÐèÒªÒ»¸ö¸üΪÓÐЧµÄ·½Ê½À´´¦ÀíIO¡£´ÓJDK 1.4Æð£¬NIO
API×÷Ϊһ¸ö»ùÓÚ»º³åÇø£¬²¢ÄÜÌṩ·Ç×èÈû(non-blo ......
ÏÖÔÚ ËùÓÃJava°æ±¾,DateÀ๹Ôì·½·¨Date(0)µÄʱ¼ä²»ÊÇThu Jan 01 01:00:00 GMT 1970ÁË£¬¶øÊÇThu Jan 01 08:00:00 CST 1970¡££¨ 1970 Äê1 ÔÂ1 ÈÕ8ʱ0·Ö0Ãë ÐÇÆÚ4£©
Ϊʲô Date b1=new Date(623289600000); ²»¿ÉÒÔ£¬ÏÂÃæµÄÈ´¿ÉÒÔÄØ£¿
long a=623289600000L;
Date b1=new Date(a); Ò ......
Thread.currentThread().getName().equals("xxxx") ÕâÌõÓï¾ä·µ»ØÒ»¸ö bool Ð͵Ľá¹û¡£Åжϵ±Ç°ÔËÐеĽø³ÌÊÇ·ñºÍ xxxx µÄÃû×ÖÒ»Ñù¡£
¶¨ÒåÏ̵߳ÄÁ½ÖÖ·½·¨£ºclass a1 extends Thread{ } a1 xiancheng=new a1();
¶þÊÇ£ºÓýӿڻص÷¼¼Êõ¡£ class a1 implements Runnable{ } a1 ......
/**
* ±£ÁôÁ½Î»Ð¡Êý
* @param d
* @return
*/
private static double remainTwoNumbers(double d){
java.math.BigDecimal f1 = new java.math.BigDecimal(d);//СÊýµãºó±£Áô2Î ......
/**
* ´¦Àíʱ¼äÒì³£ Date to String
* Èç¹ûת»»³öÏÖÒì³£»á¸øĬÈÏÖµ00:00
* @param time
* @param sdf
* @return
*/
public static String handleDateParseException(Object time){
String tempTime = "00:00";
try{
......