首页 | 社区 | 博客 | 招聘 | 文章 | 新闻 | 下载 | 读书 | 代码
亲,您未登录哦! 登录 | 注册

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( "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>&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地址),就能实现跨平台通讯了。

本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )

编程爱好者论坛

本栏最新文章