2013-09-28 27 views
0

我想通過套接字發送一個jar文件到服務器。 現在一切似乎都正常工作,但在套接字的另一側,jar已損壞。這些文件在套接字的兩側具有相同的長度。但是當我嘗試在bukkit中使用該文件時,該文件已損壞。當通過套接字發送Jar文件被破壞

客戶端代碼:

public class Main { 
private Socket connection; 
private ObjectOutputStream outStream; 
private static String serverAddress = ""; // the ip address 
static File fileLoc = new File("C:\\Users\\Tom\\Documents\\Qubeproject\\server\\plugins"); 
static String fileName = "\\WorldEdit.jar"; 
static File file ; 
static InputStream IS; 
static OutputStream OS; 
static byte[] msgByte = new byte[1024]; 


public static void main(String[] arg0){ 
    p("Starting this shit up"); 
    file = new File(fileLoc + fileName) ; 

    try { 
     Socket connection = connection(); 
     IS = connection.getInputStream(); 
     OS = connection.getOutputStream(); 

     OS.write(msg("LOL")); 
     //Authenciation 


     IS.read(msgByte); 
     if(new String(msgByte).trim().equals("OK")){ 
      p("OK"); 



      OS.write(msg(fileName)); 
      //sending fileName 


      IS.read(msgByte); 
      p(new String(msgByte).trim()); 
      //confirmation 



      OS.write(msg("l:" + (file.length()))); 



      byte[] fileByte = new byte[(int)(file.length())]; 

      FileInputStream jis = new FileInputStream(file); 
      int count = 0; 
      while((count = jis.read(fileByte))>0){ 

      OS.write(fileByte,0,count); 
      OS.flush(); 
      } 

      OS.flush(); 

      setByteZero(msgByte); 

      IS.read(msgByte); 
      p(new String(msgByte).trim()); 
      //confirmation 

     }else{ 
      p("Authenciation failed"); 
     } 







    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 
    private static void p (String s){ 
    System.out.println(s); 
} 
    private static byte[] msg(String s){ 
     return s.getBytes(); 
    } 

    private static Socket connection() throws IOException{ 
     Socket socket = new Socket(); 
     InetAddress ip = InetAddress.getByName(serverAddress); 
     InetSocketAddress address = new InetSocketAddress(ip,6969); 
     socket.connect(address,6969); 
     return socket; 
    } 

    private static byte[] setByteZero(byte[] workByte) { 
     for(int i=0;i < workByte.length;i++){ 
      workByte[i] = 0; 
     } 
     return workByte; 

    } 

在bukkit服務器代碼

public class Checkup implements Runnable{ 
Server serverB; 
ServerSocket server; 
Socket client; 
InputStream IS; 
OutputStream OS; 
static File destination; 
byte[] msgByte = new byte[1024]; 
String filename; 
long length; 



Checkup (Server serverm){ 
    serverB = serverm; 
} 




@Override 
public void run() { 

    try{ 
     server = new ServerSocket(6969); 
    }catch(Exception e){ 
    } 
    try{ 
    while(true){ 
      client = server.accept(); 


      IS = client.getInputStream(); 
      OS = client.getOutputStream(); 

      IS.read(msgByte); 


      if(msg(msgByte).equals("LOL")){ 
       OS.write(msg("OK")); 

       IS.read(msgByte); 

       filename = msg(msgByte); 


       OS.write(msg("Q received name :" + filename)); 

       OS.flush(); 

       setByteZero(msgByte); 

       IS.read(msgByte); 

       length = Integer.parseInt(new String(msgByte).trim().replace("l:", "")); 


       OS.write(msg("Q received length :" + length)); 

       byte[] fileByte = new byte[(int)length]; 


       destination = new File("C:\\Users\\Quentin\\Desktop\\DE server\\plugins" + filename); 

       FileOutputStream fos = new FileOutputStream(destination); 

       int count = 0; 



       while((count =IS.read(fileByte))>0){ 



       fos.write(fileByte); 
       fos.flush(); 

       } 
       fos.flush(); 
       fos.close(); 

       OS.write(msg("Q received the jar!Bye")); 
       client.close(); 



      } 






    }  

    }catch (Exception e){ 
     e.printStackTrace(); 
     try { 
      serverB.reload(); 
     } catch (Exception e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
    } 

finally{ 

    }} 




private String msg(byte[] strByte){ 
    return new String(strByte).trim(); 
} 

private static byte[] msg(String s){ 
    return s.getBytes(); 
} 


private static byte[] setByteZero(byte[] workByte) { 
    for(int i=0;i < workByte.length;i++){ 
     workByte[i] = 0; 
    } 
    return workByte; 

} 
+6

你到處忽略InputStream.read'的'的返回值。不要這樣做。 –

+0

這是否與損壞的文件有關?因爲使用fileByte我想我不會忽略返回值,因爲我使用count = IS.read(fileByte)。 – tb96

+3

查看*每次調用*到'IS.read(msgByte)',忽略結果。無論這是你現在看到的問題,總的來說這是一個很大的問題。此外,你應該真正檢查文件中的差異 - 首先獲取正在發送和接收的文件的MD5散列值或其他校驗和。這可能是問題完全不同。老實說,這個代碼還有其他各種問題,但是一次只能解決一個問題...... –

回答

2

@喬恩指出你的問題,在這裏它是拼寫和格式.....

以下代碼將忽略計數(且計數合法也爲0):

  while((count =IS.read(fileByte))>0){ 
      fos.write(fileByte); 
      fos.flush(); 
      } 
      fos.flush(); 
      fos.close(); 

和應該寫成:

  while((count =IS.read(fileByte))>=0){ 
      fos.write(fileByte, 0, count); 
      } 
      fos.flush(); 
      fos.close(); 
+0

感謝您的解決方案爲我做到了! – tb96