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

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?