2012年5月10日 星期四

網路程式設計








import java.net.*;

import java.io.*;

public class MyServer {

   public final static int myPort=20; // 使用port 20

   public static void main(String args[]) {
      //ServerSocket ss; 
      //Socket sc;
      PrintWriter op;
      try {
      ServerSocket ss = new ServerSocket(myPort);
      Socket sc = ss.accept(); // 等待建立連線

      }
       catch (IOException e) {
System.err.println(e);
}



/*
try {
   ss = new ServerSocket(myPort);
   try {
     while (true) {
sc = ss.accept(); // 等待建立連線
op = new PrintWriter(sc.getOutputStream());
op.println("Hi! There!");
op.flush(); // 很重要, 不要漏掉
sc.close();
}
   }
   catch (IOException e) {
ss.close();
System.err.println(e);
}
}
catch (IOException e) {
System.err.println(e);
}

*/

}

}













































1 則留言: