<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      JAVA完成服務器與多用戶跨平臺的通訊

      [摘要]隨著網絡技術的發展,我們的局域網越做越大,里面的服務器客戶機數量也很多。在為我們提供了諸多便利的同時,我們發現,由于服務器和客戶機的操作平臺不同,它們之間的通信是一個麻煩的問題,因為很多現成的通信軟...
      隨著網絡技術的發展,我們的局域網越做越大,里面的服務器客戶機數量也很多。在為我們提供了諸多便利的同時,我們發現,由于服務器和客戶機的操作平臺不同,它們之間的通信是一個麻煩的問題,因為很多現成的通信軟件或者源程序都是針對同一平臺的。為了解決這個問題,我們采用JAVA編程,成功的實現了LINUX,WINDOWS NT,WIN98跨平臺的通訊。

      ---- 服務器程序源代碼如下:

      //server.java import java.io.*; import sun.net.*; class
      server extends NetworkServer //定義服務器類 {DataInputStream net_input; //定義數據輸出
      PrintStream net_output; //定義數據輸入 public static void main(String args[]) { new
      server();} public server() //運行服務器功能,并把端口設為1111 { try
      {startServer(1111);} catch (Exception e) { System.out.println( &quot;Unable to start
      server.&quot;); return; } System.out.println(&quot;Waiting for clients...&quot;); } public
      void serviceRequest() //定義服務應答功能 { net_input = new
      DataInputStream(clientInput); net_output = System.out; String user = read_net_input();
      System.out.println(user+&quot; connected!&quot;); while(true) { String string;
      if((string=read_net_input( ))==null) break; //如果客戶機輸入NULL,中斷服務
      write_net_output(user+&quot;:&quot;+string); } System.out.println(user+&quot; has
      disconnected!&quot;); } 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>&quot;); }
      System.out.println( &quot;Connecting...&quot;); try {new client(args[0],args[1]);} catch
      (Exception e) { System.out.println( &quot;Unable to create NetworkClient.&quot;); return;
      } } public client (String host,String username) throws IOException //與服務器鏈接功能
      { super(host,1111); if(serverIsOpen()) { System.out.println( &quot;Connected to
      server.&quot;); net_input = new DataInputStream(System.in); net_output = serverOutput;
      net_output.println(username); chat(); } else System.out.println(&quot;Error:Could not
      connect to server.&quot;); } void chat() //定義信息傳遞函數,當輸入EXIT時,中斷鏈接
      { String string; System.out.println( &quot;Type EXIT to exit&quot;); while(true) {
      string=read_net_input(); if(string.equalsIgnoreCase(&quot;EXIT&quot;)) break;
      write_net_output(string); } System.out.println(&quot;Disconnecting...&quot;);
      close_server(); System.out.println(&quot;Done!&quot;); } 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(&quot;Unable
      to close server.&quot;);} } }
      ----
        把兩個源程序輸入后,在任一操作平臺上運行javac server.java和javac client.java,分別把它們編譯成class文件。由于java的class文件的跨平臺性,只要在服務器上運行相應的java解析程序執行server,在客戶機上運行相應的java解析程序執行client ,就能實現客戶機和服務器之間的通訊了,而且服務器允許多用戶接入。以筆者學校的局域網為例,源程序在WIN98平臺上用JDK 1.1.5編譯成功,把server.class拷到一臺LINUX服務器上,執行java server(該服務器已經安裝了JAVA的RPM包),在其他WINNT平臺上拷入client.class,運行jview client 192.168.100.1 NT(192.168.100.1是LINUX服務器的IP地址),就能實現跨平臺通訊了。


      日韩精品一区二区三区高清