服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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:"); 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 //与服务器链接功能
{ 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地址),就能实现跨平台通讯了。

扫描关注微信公众号