三. 編寫服務器類Java程序
- // Server.Java
- import Java.io.*;
- import Java.Net.*;
- public class Server extends Thread
- {
- public final static int Default_Port=6543;
- protectd int port;
- protectd ServerSockt listen_socket;
- // 定義出錯例程:如果出現異常錯誤,退出程序。
- Public static void fail(Exception e, String msg)
- {
- System.err.println(msg + ": " + e);
- System.exit(1);
- }
- // 定義並啟動服務器的Socket 例程,監聽客戶機的連接請求。
- public Server(int port)
- {
- if(port == 0) port = Default_Port;
- this.port = port;
- try
- {
- listen_socket = new ServerSocket(port);
- }
- catch(IOException e) fail(e, "Exception creating server socket");
- System.out.println("Server: listening on port" + port);
- This.start();
- }
- /* 下面為服務器監聽線程的主程序。該線程一直循環執行,監聽並接受客戶機發出的連接請求。對每一個連接,均產生一個連接對象與之對應,通過Socket 通道進行通信。*/
- public void run()
- {
- try
- {
- while(true)
- {
- Socket clIEnt_socket = listen_socket.accept();
- Connection c = new Connection(clIEnt_socket);
- }
- }
- catch(IOException e) fail(e,"Exception while listening for connections")
- }
- // 啟動服務器主程序
- public static void main(String args[])
- {
- int port = 0;
- if (args.length == 1)
- {
- try port = Integer.parseInt(args[0]);
- catch(NumberFormatException e) port = 0;
- }
- new Server(port);
- // End of the main
- } // End of Server class
- //以下定義了Connection 類,它是用來處理與客戶機的所有通信的線程。
- class Connection extends Thread
- {
- protected Socket clIEnt;
- protected DataInputStream in;
- protected PrintStream out;
- // 初始化通信流並啟動線程
- public Connection(Socket clIEnt_socket)
- {
- client = clIEnt_socket;
- try
- {
- in = new DataInputStream(clIEnt.getinputStream());
- out = new PrintStream(clIEnt.getOutputStream());
- }
- catch(IOException e)
- {
- try clIEnt.close();
- catch(IOException e2);
- System.err.println("Exception while getting socket streram: " + e);
- Return;
- }
- this.start;
- } // End of Connection method
- // 服務例程:讀出一行文本;反轉文本;返回文本。
- public void run()
- {
- String line;
- StringBuffer revline;
- int len;
- try
- {
- for(;;)
- {
- // Read a line
- line = in.readline();
- if(line == null) break;
- // Reverse the line
- len = line.length();
- revline = new StringBuffer(len);
- for(int i = len-1; i >=0; i--)
- revline.insert(len-1-I;line.charAt(i));
- // Write out the reverse line
- out.println(revline);
- }
- catch(IOException e);
- finally try clIEnt.close();
- catch(IOException e2);
- }
- // End of run method
- }
- // End of Connection class
四. 編寫客戶機類Java 程序
- // ClIEnt.Java
- import Java.io.*;
- import Java.Net.*;
- public class ClIEnt extends
- {
- public static final int Default_Port = 6543;
- // 定義出錯例程
- public static final void usage()
- {
- System.out.println("Usage: Java ClIEnt []");
- System.exit(0);
- }
- public static void main(String args[])
- {
- int port = Default_Port;
- Socket s = null;
- // 解析端口參數
- if ((args.length != 1)&&(args.length != 2 )) usage();
- if (args.length == 1)
- port = Default_Port;
- else
- {
- try port = Integer.parseInt(args[1]);
- catch(NumberFormaatException e) usage();
- }
- try{
- // 產生一個Socket ,通過指定的端口與主機通信。
- s = new Socket(args[0], port);
- // 產生用於發出和接收的文本字符流
- DataInputStream sin = new DataInputStream(s.getInputStream());
- PrintStream sout = new DataInputStream(s.getInputStream());
- // 從控制台讀入字符流
- DataInputStream in = new DataInputStream(System.in);
- // 返回連接的地址和端口
- ystem.out.println("Connected to"+s.getInetAddress()+":"+ s.getPort());
- String line;
- For(;;)
- {
- // 顯示提示符
- System.out.print(" >");
- System.out.flush();
- // 讀入控制台輸入的一行字符
- line = in.readline();
- if (line == null) break;
- // 將接收的文本行送至服務器
- sout.println(line);
- // 從服務器接收一行字符
- line = sin.readline();
- // Check if connection is closed(i.e. for EOF)
- if(line == null)
- {
- System.out.println("Connection closed by server.");
- Break;
- }
- // 在控制台上顯示接收的字符
- System.out.println(line);
- }
- // End of for loop
- }
- // End of try
- catch(IOException e ) System.err.println(e);
- // Always be sure to close the socket
- finally
- {
- try if(s != null) s.close();
- catch(IOException e2);
- }
- } // End of main
- } // End of ClIEnt
運行該客戶機程序時,必須以服務器主機名作為第一個參數,服務器端口號為第二個參數,其中服務器端口號可缺省。