mixturer 2015-05-30 08:24 采纳率: 0%
浏览 1728

我是个新手,请问如何让cilent接收完毕文件后,server停止服务,谢谢

server程序:

 package com.alex;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.alex.dao.BaseDAO;
import com.alex.util.FileIO;
import com.alex.util.MD5Util;

public class Server {

    private int port;
    private String serverpath;



    public Server() {
        BaseDAO base=new BaseDAO();
        port = Integer.parseInt(base.getValue("port"));
        serverpath = base.getValue("serverpath");
    }

    public static void main(String[] args) {
        Server server=new Server();
        server.start();
    }

    public void start(){
        //1建立连接

        ServerSocket serverSocket=null;
        try{
            serverSocket=new ServerSocket(port);
            Socket socket=null;
            while(true){
                socket=serverSocket.accept();
                OutputStream os=socket.getOutputStream();
                BufferedOutputStream bos=new BufferedOutputStream(os);

                InputStream is=socket.getInputStream();
                BufferedInputStream bis=new BufferedInputStream(is);


                //2返送MD5值
                System.out.println("-------------------");
                System.out.println("server发送md5值开始");
                String md5=MD5Util.getHash(serverpath);
                System.out.println("MD5:"+md5);
                System.out.println("server发送md5值结束");
                System.out.println("-------------------");

                byte[] buf=md5.getBytes();
                bos.write(buf);
                bos.flush();

                //发送文件
                if(bis.read()==0){
                    System.out.println("server开始发送文件");
                    FileIO.sendFile(serverpath,bos);

                }else{
                    break;
                }

                socket.close();
            }

            System.out.println("文件验证结束");

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

client程序


package com.alex;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;

import com.alex.dao.BaseDAO;
import com.alex.util.FileIO;
import com.alex.util.MD5Util;

public class Client {

    private String ip;
    private int port;
    private String clientpath;


    public Client() {
        BaseDAO base=new BaseDAO();
        ip = base.getValue("ip");
        port =Integer.parseInt(base.getValue("port"));
        clientpath = base.getValue("clientpath");
    }

    public static void main(String[] args){
        Client client=new Client();
        client.start();
    }

    public void start(){

        Socket socket=null;
        try {
            System.out.println("IP地址:"+ip+"Port号:"+port);
            //1.创建连接
            socket=new Socket(ip,port);
            InputStream is=socket.getInputStream();
            BufferedInputStream bis=new BufferedInputStream(is);

            OutputStream os=socket.getOutputStream();
            BufferedOutputStream bos=new BufferedOutputStream(os);

            //2.获得MD5值
            byte[] buf=new byte[32];
            bis.read(buf);
            String serverMd5=new String(buf);
            System.out.println(serverMd5);
            System.out.println("开始计算client文件MD5");
            try {
                String clientMd5=MD5Util.getHash(clientpath);
                System.out.println("ClientMd5:"+clientMd5);
                //3.比较MD5
                if(serverMd5.equals(clientMd5)){
                    System.out.println("文件已存在,谢谢");
                }else{
                    //4.返回0,等待服务器发送文件过来。
                    bos.write(0);
                    bos.flush();

                    FileIO.saveFile(clientpath, bis);
                }
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }


        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}

  • 写回答

2条回答 默认 最新

  • loseyee 2015-05-30 08:27
    关注

    client 回复接收完毕,server收到并close

    评论

报告相同问题?

悬赏问题

  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振