0

ストリームに問題があります。マルチスレッドのクライアントとサーバーを使用して、ファイルを次々にアップロードまたはダウンロードできるようにしていますが、最初のファイルを取得した後 (サーバーにファイルを送信するか、サーバーからファイルを受信するかは関係ありません)、私は2 番目のファイルを続行できません。

ストリームが閉じ、ファイルを破損せずにストリームを維持する方法がわかりません。'stream'.close およびその他のメソッドを使用しない場合。

私はそれが最良のアイデアではないことを知っています.おそらく完全に受け入れられない.

どこで台無しにしたの?

サーバー部分:

public class ServerCommunicationThread extends Thread
{

   ...

   public ServerCommunicationThread(Socket socket) throws IOException,
         ParserConfigurationException, TransformerException
   {
     ...
   }

   public synchronized void run()
   {
      try
      {
         String clientSelection;
         while ((clientSelection = inFromClient.readLine()) != null)
         {
            switch (clientSelection)
            {
               case "1":
                  receiveFile();
                  break;
               case "2":
                  String outGoingFileName;
                  while ((outGoingFileName = inFromClient.readLine()) != null)
                  {
                     sendFile(outGoingFileName);
                  }
                  break;
               case "3":
               {
                  disconnect();
                  break;
               }
               default:
                  System.out.println("Incorrect command received.");
                  break;
            }
         }
      }
      catch (IOException e)
      {
         // System.out.println(clientSocket + "--->  Action performed");
         e.printStackTrace();
      }
   }

   public synchronized void receiveFile()
   {
      try
      {
         int bytesRead;

         DataInputStream clientData = new DataInputStream(
               clientSocket.getInputStream());

         String fileName = clientData.readUTF();
         OutputStream output = new FileOutputStream((filePath
               + "received_from_client_" + fileName));
         long size = clientData.readLong();
         byte[] buffer = new byte[1024];
         while (size > 0
               && (bytesRead = clientData.read(buffer, 0,
                     (int) Math.min(buffer.length, size))) != -1)
         {
            output.write(buffer, 0, bytesRead);
            size -= bytesRead;
         }

         output.close();
         clientData.close();

         System.out.println("File " + fileName + " received from client.");
      }
      catch (IOException ex)
      {
         System.err.println("Client error. Connection closed.");
      }
   }

   public synchronized void sendFile(String fileName)
   {
      try
      {

         File myFile = new File(fileName);
         byte[] mybytearray = new byte[(int) myFile.length()];

         FileInputStream fileInStream = new FileInputStream(myFile);
         BufferedInputStream bufferedInStream = new BufferedInputStream(
               fileInStream);


         DataInputStream dataInStream = new DataInputStream(bufferedInStream);
         dataInStream.readFully(mybytearray, 0, mybytearray.length);


         OutputStream outStream = clientSocket.getOutputStream();

         DataOutputStream dataOutStream = new DataOutputStream(outStream);
         dataOutStream.writeUTF(myFile.getName());
         dataOutStream.writeLong(mybytearray.length);
         dataOutStream.write(mybytearray, 0, mybytearray.length);
         dataOutStream.flush();
         System.out.println("File " + fileName + " sent to client.");
      }
      catch (Exception e)
      {
         System.err.println("File does not exist!");
      }
   }

}

クライアント:

public class ClientConnectAndSender extends Thread
{
  ...

   public ClientConnectAndSender() throws IOException
   {
      ...
   }

   public synchronized void startSending() throws IOException,
         ParserConfigurationException, TransformerException
   {
      while (ServerRunning)
      {
         outStream = new PrintStream(clientSocket.getOutputStream());

         try
         {
            switch (Integer.parseInt(selectAction()))
            {
               case 1:
                  outStream.println("1");
                  sendFile();
                  break;
               case 2:
                  outStream.println("2");
                  System.err.print("Enter file name: ");
                  fileName = inFromServer.readLine();
                  outStream.println(fileName);
                  receiveFile(fileName);
                  break;
               case 3:
                  outStream.println("3");
                  disconnect();
                  break;
            }
         }
         catch (Exception e)
         {
            System.err.println("not valid input");
         }

         recieverThread = new ClientRecieverThread(inFromServer);
         recieverThread.start();
      }
   }

   public String selectAction() throws IOException
   {
      System.out.println("1. Send file.");
      System.out.println("2. Recieve file.");
      System.out.println("3. End session");
      System.out.print("\nMake selection: ");

      return inFromServer.readLine();
   }

   public synchronized void sendFile()
   {
      try
      {
         System.err.print("Enter file name: ");
         fileName = inFromServer.readLine();

         File myFile = new File(fileName);
         byte[] mybytearray = new byte[(int) myFile.length()];

         FileInputStream fileInStream = new FileInputStream(myFile);
         BufferedInputStream bufferedInStream = new BufferedInputStream(
               fileInStream);
         // bis.read(mybytearray, 0, mybytearray.length);

         DataInputStream dataInStream = new DataInputStream(bufferedInStream);
         dataInStream.readFully(mybytearray, 0, mybytearray.length);

         OutputStream outStream = clientSocket.getOutputStream();

         // Sending file name and file size to the server
         DataOutputStream dataOutStream = new DataOutputStream(outStream);
         dataOutStream.writeUTF(myFile.getName());
         dataOutStream.writeLong(mybytearray.length);
         dataOutStream.write(mybytearray, 0, mybytearray.length);
         dataOutStream.flush();
         System.out.println("File " + fileName + " sent to Server.");
      }
      catch (Exception e)
      {
         System.err.println("File does not exist!");
      }
   }

   public synchronized void receiveFile(String fileName)
   {
      try
      {
         int bytesRead;
         InputStream inputStream = clientSocket.getInputStream();

         DataInputStream clientData = new DataInputStream(inputStream);

         fileName = clientData.readUTF();
         OutputStream outputStream = new FileOutputStream((filePath
               + "received_from_server_" + fileName));
         long size = clientData.readLong();
         byte[] buffer = new byte[1024];
         while (size > 0
               && (bytesRead = clientData.read(buffer, 0,
                     (int) Math.min(buffer.length, size))) != -1)
         {
            outputStream.write(buffer, 0, bytesRead);
            size -= bytesRead;
         }

         // outputStream.flush();
         outputStream.close();
         inputStream.close();
         // try
         // {
         // in.available();
         // output.flush();
         // }
         // catch (Exception e)
         // {
         // e.printStackTrace();
         // }

         System.out.println("File " + fileName + " received from Server.");
      }
      catch (IOException ex)
      {

         ex.printStackTrace();
      }
   }

}
4

1 に答える 1