Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1502581
  • 博文数量: 3500
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 43870
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-03 20:31
文章分类

全部博文(3500)

文章存档

2008年(3500)

我的朋友

分类:

2008-05-04 19:29:55

一起学习
---- 随着网络技术的发展,我们的局域网越做越大,里面的服务器客户机数量也很多。在为我们提供了诸多便利的同时,我们发现,由于服务器和客户机的操作平台不同,它们之间的通信是一个麻烦的问题,因为很多现成的通信软件或者源程序都是针对同一平台的。为了解决这个问题,我们采用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( "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 "); } 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() //定义信息传递函数,当输入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解析程序执行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地址),就能实现跨平台通讯了。 下载本文示例代码


JAVA实现服务器和多用户跨平台的通讯JAVA实现服务器和多用户跨平台的通讯JAVA实现服务器和多用户跨平台的通讯JAVA实现服务器和多用户跨平台的通讯JAVA实现服务器和多用户跨平台的通讯JAVA实现服务器和多用户跨平台的通讯JAVA实现服务器和多用户跨平台的通讯JAVA实现服务器和多用户跨平台的通讯JAVA实现服务器和多用户跨平台的通讯JAVA实现服务器和多用户跨平台的通讯JAVA实现服务器和多用户跨平台的通讯JAVA实现服务器和多用户跨平台的通讯
阅读(299) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~