2 mixturer mixturer 于 2015.05.30 16:24 提问

我是个新手,请问如何让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个回答

hn550044
hn550044   2015.05.30 16:27

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

edouardzyc
edouardzyc   2015.05.30 18:36

你服务端不要 while(true)了呗 接受一次请求就结束了吗

Csdn user default icon
上传中...
上传图片
插入图片