2017-04-11 64 views
0

我想實現在java中使用套接字的文件傳輸。我到目前爲止,但我無法將文件發送到服務器的問題。客戶端不寫入文件的內容。我知道我需要創建一個循環,但我無法做到這一點。你能檢查代碼嗎?我看不到問題。在Java中的文件傳輸

客戶端:

import java.io.*; 
import java.net.*; 
// Class Client Socket 
public class ClientSocket 
{ 
    public static void main(String[] args) throws IOException 
    { 
     // show main Menu 
     System.out.println("Basic Client Socket Programming"); 
     // Socket Variables 
     Socket clientSocket = null;    // for sending and receiving of data 
     PrintWriter outputStream = null;  // for sending data 
     BufferedReader inputStream = null;  // for receiving data 
     BufferedInputStream bis; 
     BufferedOutputStream bos; 
     // Create and open socket 
     // Connection to 127.0.0.1, port num=2001 
     try 
     { 
      clientSocket = new Socket("127.0.0.1", 2001); 
      outputStream = new PrintWriter(clientSocket.getOutputStream(), true); 
      inputStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
     } 
     catch (UnknownHostException e) 
     { 
      System.err.println("Don't know about host: 127.0.0.1"); 
      return; 
     } 
     catch (IOException e) 
     { 
      System.err.println("Couldn't get I/O for the connection to: 127.0.0.1"); 
      return; 
     } 
     // check if connection is successful 
     if((clientSocket == null) || (outputStream == null) || (inputStream == null)) 
     { 
      System.err.println("Socket Connection not successfull"); 
      return; 
     } 
     else 
     { 
      System.out.println("Socket Connected"); 
     } 
     //generate MD5 and send data 
     String hash = MD5.md5("Hello"); 
     System.out.println("Hash code generated: "+hash); 
     //outputStream.println("Hello ||" + hash); 
     //creates a new BufferWriter to write the hash value and the text message into the txt file. 
     BufferedWriter out = new BufferedWriter(new FileWriter("hashedCode.txt")); 
     out.write(hash); //writes the hash code to the file 
     out.close(); //close the bufferreader 
     //Send the file to the server 
     bis = new BufferedInputStream(new FileInputStream("hashedCode.txt")); 
     bos = new BufferedOutputStream(clientSocket.getOutputStream()); 
     byte[] byteArray = new byte[8192]; 
     int in; 
     while ((in = bis.read(byteArray)) != -1) 
     { 
      bos.write(byteArray,0,in); 
     } 
     // receiving data 
     String rcvData; 
     rcvData = inputStream.readLine(); 
     System.out.println(rcvData); 
     // close connections 
     try 
     { 
      outputStream.close(); 
      inputStream.close(); 
      bis.close(); 
      bos.close(); 
      clientSocket.close(); 
      System.out.println("Connection closed."); 
     } 
     catch (UnknownHostException e) 
     { 
      System.err.println("Trying to connect to unknown host: " + e); 
     } 
     catch (IOException e) 
     { 
      System.err.println("IOException: " + e); 
     } 
     // exit program 
     return; 
    } 
} 

服務器端

import java.io.*; 
import java.net.*; 
public class serverSocket 
{ 
    public static void main(String[] args) throws IOException 
    { 
     // show main Menu 
     System.out.println("Basic Client Socket Programming"); 
     // Socket Variables 
     ServerSocket serverSocket = null;  // for listen for connection 
     Socket rcvSocket = null;    // for sending n rcving data 
     DataOutputStream outputStream = null; // for sending data 
     DataInputStream inputStream = null;  // for receiving data 
     BufferedInputStream bis; 
     BufferedOutputStream bos; 
     int inInt; 
     byte[] data; 
     // try to open a socket to listen for incoming data 
     // port number must match the one that the client is connecting to 
     try 
     { 
      serverSocket = new ServerSocket(2001); 
     } 
     catch (IOException e) 
     { 
      System.err.println(e); 
     } 
     // creating a socket to rcv incoming data 
     while (true) 
     { 
      try 
      { 
       System.out.println("Listening"); 
       rcvSocket = serverSocket.accept(); 
       System.out.println("Connected"); 
       PrintWriter out = new PrintWriter(rcvSocket.getOutputStream(), true); 
       BufferedReader in = new BufferedReader(new InputStreamReader(rcvSocket.getInputStream())); 
       /* 
       // initiate conversation with client 
       String rcvData = in.readLine(); 
       System.out.println("Rcv Data: " + rcvData); 
       */ 
      byte[] receivedData = new byte[8192]; 
      bis = new BufferedInputStream(rcvSocket.getInputStream()); 
      bos = new BufferedOutputStream(new FileOutputStream("receivedHashed.txt")); 
      while ((inInt = bis.read(receivedData)) != -1) 
      { 
       bos.write(receivedData,0,inInt); 
      } 
      bos.flush(); 
       out.println("File received!"); 
      } 
      catch (IOException e) 
      { 
       System.err.println(e); 
      } 
     } 
    } 
} 
+0

您已經創建了一個循環。這裏沒有UDP,只有TCP。不清楚你在問什麼。 – EJP

回答

0

你只需要刷新你寫出來的緩衝輸出流的字節?

while ((in = bis.read(byteArray)) != -1) 
{ 
    bos.write(byteArray,0,in); 
} 
bos.flush(); 
+0

或者確實*關閉*它。 – EJP

+0

'bos.close()'已在下一個'try'塊中調用,其中所有其他資源也被關閉。但是,如果close()隱式地調用flush(),那麼在該代碼片段中對close()的調用「太遲了」,因爲它也是在輸入流檢查響應數據之後。 – geneSummons