JAVA实现服务器和多用户跨平台的通讯

779 篇文章 0 订阅
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

  随着网络技术的发展,我们的局域网越做越大,里面的服务器客户机数量也很多。在为我们提供了诸多便利的同时,我们发现,由于服务器和客户机的操作平台不同,它们之间的通信是一个麻烦的问题,因为很多现成的通信软件或者源程序都是针对同一平台的。为了解决这个问题,我们采用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 &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.JAVAJAVAc 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地址),就能实现跨平台通讯了。

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值