隨著網(wǎng)絡(luò)技術(shù)的發(fā)展,我們的局域網(wǎng)越做越大,里面的服務(wù)器客戶機(jī)數(shù)量也很多。在為我們提供了諸多便利的同時(shí),我們發(fā)現(xiàn),由于服務(wù)器和客戶機(jī)的操作平臺(tái)不同,它們之間的通信是一個(gè)麻煩的問題,因?yàn)楹芏喱F(xiàn)成的通信軟件或者源程序都是針對(duì)同一平臺(tái)的。為了解決這個(gè)問題,我們采用JAVA編程,成功的實(shí)現(xiàn)了LINUX,WINDOWS NT,WIN98跨平臺(tái)的通訊。
---- 服務(wù)器程序源代碼如下:
//server.java import java.io.*; import sun.net.*; class server extends NetworkServer //定義服務(wù)器類 {DataInputStream net_input; //定義數(shù)據(jù)輸出 PrintStream net_output; //定義數(shù)據(jù)輸入 public static void main(String args[]) { new server();} public server() //運(yùn)行服務(wù)器功能,并把端口設(shè)為1111 { try {startServer(1111);} catch (Exception e) { System.out.println( "Unable to start server."); return; } System.out.println("Waiting for clients..."); } public void serviceRequest() //定義服務(wù)應(yīng)答功能 { 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; //如果客戶機(jī)輸入NULL,中斷服務(wù) 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(); } } 客戶機(jī)程序源代碼: //client.java import java.io.*; import sun.net.*; class client extends NetworkClient //定義客戶機(jī)類 { DataInputStream net_input; PrintStream net_output; public static void main(String args[])//獲得服務(wù)器IP地址和客戶機(jī)名 { 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 //與服務(wù)器鏈接功能 { 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ù),當(dāng)輸入EXIT時(shí),中斷鏈接 { 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.");} } } ---- 把兩個(gè)源程序輸入后,在任一操作平臺(tái)上運(yùn)行javac server.java和javac client.java,分別把它們編譯成class文件。由于java的class文件的跨平臺(tái)性,只要在服務(wù)器上運(yùn)行相應(yīng)的java解析程序執(zhí)行server,在客戶機(jī)上運(yùn)行相應(yīng)的java解析程序執(zhí)行client ,就能實(shí)現(xiàn)客戶機(jī)和服務(wù)器之間的通訊了,而且服務(wù)器允許多用戶接入。以筆者學(xué)校的局域網(wǎng)為例,源程序在WIN98平臺(tái)上用JDK 1.1.5編譯成功,把server.class拷到一臺(tái)LINUX服務(wù)器上,執(zhí)行java server(該服務(wù)器已經(jīng)安裝了JAVA的RPM包),在其他WINNT平臺(tái)上拷入client.class,運(yùn)行jview client 192.168.100.1 NT(192.168.100.1是LINUX服務(wù)器的IP地址),就能實(shí)現(xiàn)跨平臺(tái)通訊了。
|
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!