到 Google 资讯主页   
EasyJF首页   资料   源码   软件    论坛   网站    
   使用帮助    
    该信息为本站MyRSS系统缓存内容,部分图片及附件有可能无法正常使用.easyjf.com未知无关,不对该信息负责.通过http://www.javafan.net//article/20040313180333967.html访问该信息的原始内容.
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   
用java编写客户-服务器程序的若干心得
作者:马文晓     来源:未知  发布时间:2006-02-22 17:47:06.14

  (1) 我们在DOS命令行上用jview启动服务器server.class时,一般无法知道jview server.class的实例是否已在运行,利用c语言操作上锁文件的_sopen及内部涉及系统调用的_locking函数,可解决此问题:
  #include < io.h >
  #include < fcntl.h >
  #include < process.h >
  #include < share.h >
  #include < stdio.h >
  #include < sys/locking.h >
  #include < sys/stat.h >
  #include < sys/types.h >
  void main(){
   int f,g;
   f=_sopen("c:\\f0",_O_CREAT|_O_TRUNC,
    _SH_DENYNO,_S_IWRITE);
   if ((g=_locking(f,_LK_NBLCK,1))==0){
    system("jview server.class");exit(0);
   }
   else
    printf("jview server已启动");
  }
  先用建立方式打开"c:\f0",这一步总能成功,然后调用_locking,试图上锁其第1个字节,因系统调用期间不允许进程的切换,故这一步不会导致并发问题,返回值g是可靠的.若无实例正在运行,则上锁成功,可用system启动jview server.class,完成任务后,用exit释放所有资源并退出;若有实例在运行,则上锁失败,显示"jview server已启动"后退出.
  (2) 客户端的client.java用TCP/IP协议的传输层socket接口与server连接时,若此时 server未运行,则Socket s=new Socket(InetAddress.getByName("server_ip"),port) 将无限等待.利用定时线程,可将无限等待变为等待3秒钟:
  import java.net.*;
  import java.io.*;
  class timer extends Thread{
   public void run(){
    try{sleep(3000); // 3秒钟
     System.out.println("server未启");
     System.exit(1);
    }catch(Exception E){}
   }
  }
  public class client {
   public static void main (String arg[]){
    timer tm=new timer();
    tm.start();
    try {
     Socket so=new Socket(InetAddress.
      getByName("server_ip"),port);
     tm.stop();
     ...
    }
    catch(Exception E){}
   }
  }
  (3) 用Socket结合DataInputStream类,OutputStream类在客户-服务器之间传输大于 9200字节时,结果不可靠,这时可在客户进程,服务器进程中,用exec创建负责传输的同步的子进程child,在child中每次只同步传输2048个字节,child终止时,会刷新传输缓存,释放相关资源,实践证明结果是可靠的:
  public static void main (String arg[])
  { /*客户,服务器进程*/
  String cmd[]=new String[1];
  cmd[0]="jview child_client";
  // cmd[0]="jview child_server";
  try {Runtime.getRuntime().exec(cmd).
  waitFor();...}
  ...
  }
  public class child_client
  { /* 客户端child子进程 */
   public static void main(String[] arg) {
    try{
     int i,j,k;
     byte buf,z[]=new byte[1];
     FileInputStream f=new FileInputStream("transport_file");
     buf=new byte[f.available()];
     i=f.read(buf);
     f.close();
     Socket so=new Socket(InetAddress.
     getByName("server_ip"),port);
     DataInputStream is=new DataInputStream(so.getInputStream());
     OutputStream os=so.getOutputStream();
     for(j=0;j!=i;j+=k) {
      if ((k=(i-j)) >2048) k=2048;
      os.write(buf,j,k);
      is.read(z);
     }
     is.close();os.close();so.close();
    }
   }
  }
  public class child_server
  {
   /* 服务器端child子进程 */
   public static void main(String[] arg) {
    try{
     int i,j,k;
     byte buf,z[]=new byte[1];
     buf=new byte[new
     file("transport_file").length()];
     Socket so=new ServerSocket(port).accept();
     DataInputStream is=new DataInputStream(so.getInputStream());
     OutputStream os=so.getOutputStream();
     for(j=0;j!=i;j+=k) {
      if ((k=(i-j)) >2048) k=2048;
      k=is.read(buf,j,k);
      os.write(z); }
     os.close();is.close();so.close();
    }
   }
  }
  (4) 用String str=new String(buf)从字节数组buf构造字符串变量str,然后用 System.out.println(str)显示str,当str含长度大于2400字节的汉字或特殊 字符的行时,可能因MalformedException例外而不能完全显示str.这时可用FileOutputStream 类的write方法将buf写入文件f1,然后用dos的"type f1"显示文件。

 
相关文章
 
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   


EasyJF.com 2006 隐私政策 使用EasyJF前必读