2017-04-11 8 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() 호출이이 코드 스니 j에서 "너무 늦음"입니다. – geneSummons