隨著網絡技術的發(fā)展,我們的局域網越做越大,里面的服務器客戶機數(shù)量也很多。在為我們提供了諸多便利的同時,我們發(fā)現(xiàn),由于服務器和客戶機的操作平臺不同,它們之間的通信是一個麻煩的問題,因為很多現(xiàn)成的通信軟件或者源程序都是針對同一平臺的。為了解決這個問題,我們采用JAVA編程,成功的實現(xiàn)了LINUX,WINDOWS NT,WIN98跨平臺的通訊。
---- 服務器程序源代碼如下:
//server.java import java.io.*; import sun.net.*; class server extends NetworkServer //定義服務器類 {DataInputStream net_input; //定義數(shù)據(jù)輸出 PrintStream net_output; //定義數(shù)據(jù)輸入 public static void main(String args[]) { new server();} public server() //運行服務器功能,并把端口設為1111 { try {startServer(1111);} catch (Exception e) { System.out.println( "Unable to start server."); return; } System.out.println("Waiting for clients..."); } public void serviceRequest() //定義服務應答功能 { net_input = new DataInputStream(clientInput); net_output = System.out; String user = read_net_input(); System.out.println(user+" connected!"); while(true) { String string; if((string=read_net_input( ))==null) break; //如果客戶機輸入NULL,中斷服務 write_net_output(user+":"+string); } System.out.println(user+" has disconnected!"); } String read_net_input() { try {return net_input.readLine();} catch(IOException e) {return null;} } void write_net_output(String string) { net_output.println(string); net_output.flush(); } } 客戶機程序源代碼: //client.java import java.io.*; import sun.net.*; class client extends NetworkClient //定義客戶機類 { DataInputStream net_input; PrintStream net_output; public static void main(String args[])//獲得服務器IP地址和客戶機名 { if(args.length<2) { System.out.println( "To run,type:\n"); System.out.println( "java client <host> <username>"); } System.out.println( "Connecting..."); try {new client(args[0],args[1]);} catch (Exception e) { System.out.println( "Unable to create NetworkClient."); return; } } public client (String host,String username) throws IOException //與服務器鏈接功能 { super(host,1111); if(serverIsOpen()) { System.out.println( "Connected to server."); net_input = new DataInputStream(System.in); net_output = serverOutput; net_output.println(username); chat(); } else System.out.println("Error:Could not connect to server."); } void chat() //定義信息傳遞函數(shù),當輸入EXIT時,中斷鏈接 { String string; System.out.println( "Type EXIT to exit"); while(true) { string=read_net_input(); if(string.equalsIgnoreCase("EXIT")) break; write_net_output(string); } System.out.println("Disconnecting..."); close_server(); System.out.println("Done!"); } String read_net_input() { try {return net_input.readLine();} catch(IOException e) {return null;} } void write_net_output(String string) { net_output.println(string); net_output.flush(); } void close_server() { try {closeServer();} catch(Exception e) {System.out.println("Unable to close server.");} } } ---- 把兩個源程序輸入后,在任一操作平臺上運行javac server.java和javac client.java,分別把它們編譯成class文件。由于java的class文件的跨平臺性,只要在服務器上運行相應的java解析程序執(zhí)行server,在客戶機上運行相應的java解析程序執(zhí)行client ,就能實現(xiàn)客戶機和服務器之間的通訊了,而且服務器允許多用戶接入。以筆者學校的局域網為例,源程序在WIN98平臺上用JDK 1.1.5編譯成功,把server.class拷到一臺LINUX服務器上,執(zhí)行java server(該服務器已經安裝了JAVA的RPM包),在其他WINNT平臺上拷入client.class,運行jview client 192.168.100.1 NT(192.168.100.1是LINUX服務器的IP地址),就能實現(xiàn)跨平臺通訊了。
|