1

我目前正在尝试设计一个客户端-服务器应用程序,如下所示:用户连接到服务器,当身份验证正常时,服务器向用户发送一些文件。问题是这些文件被写在一个文件中(用我的方法)。

这是一些代码:

传输文件的函数

public void processFile(int id, DataOutputStream oStream, Socket socket, int tip){
        String fileName;
        if(tip==0){
            fileName="File"+Integer.toString(Intrebari[id])+".txt";
        }else{
            fileName="Image"+Integer.toString(Intrebari[id])+".jpg";
        }
        byte[] buffer=null;
        int bytesRead = 0;
        FileInputStream file=null;

        try {
            file = new FileInputStream(fileName);
            buffer = new byte[socket.getSendBufferSize()];
            while((bytesRead = file.read(buffer))>0)
                {
                oStream.write(buffer,0,bytesRead);
                }
            file.close();
        } catch (IOException ex) {
           System.out.println(ex);
        }

    }

以及选择必须发送哪个文件的功能

private void send(int id) throws IOException {
        os.writeBytes("sendFile" + id+"\n");

        System.out.println("sendFile" + id);
        generatedFiles.processFile(id, os, comunicare, 0);
        if (generatedFiles.Imagini[id] == 1) {
            os.writeBytes("sendImage" + id+"\n");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(clientThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("sendImage" + id);
            generatedFiles.processFile(id, os, comunicare, 1);
        }
    }

我不得不提到的osDataOutputStreamcomunicareSocket类型。

我认为问题在于我writeByteswrite. 谁能帮我解决这个问题?如何让服务器和客户端同时接收文件和消息?

4

1 回答 1

1

I do something like this:

Server -send Command Server -send File Client -confirms file sucessfull

Server -send Command Server -send File Client -confirms file sucessfull ...

like this...supose you have a socket from a client, than you...

socket.getOutputStream.write("FILE: SomeFile.bin, SIZE: 872973493\r\n".getBytes("choose an encoding")) socket.getOutputStream.flush(); (you will only need to flush if you are expecting a server string response like: OK SEND ME THE FILE, IM READY, otherwise no need too) client reads and see that is a file and it has this bytes size, so it starts reading from socket.getInputStream untill it gets the length of file as expected. after this clients confirm he recived the file

then server can send another file, you could do instead of FILE, use IMAGE: or anything you want. You just have to read the message from client side to see if it is a file or image

Here are some functions that might help you:

public static void readInputStreamToFile(InputStream is, FileOutputStream fout,
        long size, int bufferSize) throws Exception
{
    byte[] buffer = new byte[bufferSize];
    long curRead = 0;
    long totalRead = 0;
    long sizeToRead = size;
    while(totalRead < sizeToRead)
    {
        if(totalRead + buffer.length <= sizeToRead)
        {
            curRead = is.read(buffer);
        }
        else
        {
            curRead = is.read(buffer, 0, (int)(sizeToRead - totalRead));
        }
        totalRead = totalRead + curRead;
        fout.write(buffer, 0, (int) curRead);
    }
}


public static void writeFileInputStreamToOutputStream(FileInputStream in, OutputStream out, int bufferSize) throws Exception
{
    byte[] buffer = new byte[bufferSize];
    int count = 0;
    while((count = in.read(buffer)) != -1)
    {
        out.write(buffer, 0, count);
    }
}
于 2011-05-11T19:23:05.283 回答