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 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站