Note that there are some explanatory texts on larger screens.

plurals
  1. POJar File is corrupt after sending over socket
    primarykey
    data
    text
    <p>So i am building a program which needs an auto-updating feature built in to it, as i was finished up and tested it out, it seems when i send the jar file over the socket and write it to the newly made jar file it is missing 5KB (everytime... even when the size changes) size from it and becomes corrupt.</p> <p>Here is my code:</p> <pre><code>package server.update; import java.io.*; import java.net.Socket; public class UpdateThread extends Thread { BufferedInputStream input; //not used BufferedInputStream fileInput; BufferedOutputStream output; public UpdateThread(Socket client) throws IOException { super("UpdateThread"); output = new BufferedOutputStream(client.getOutputStream()); input = new BufferedInputStream(client.getInputStream()); } public void run() { try { File perm = new File(System.getProperty("user.dir")+"/GameClient.jar"); //fileInput = new BufferedInputStream(new FileInputStream(perm)); fileInput = new BufferedInputStream(new FileInputStream(perm)); byte[] buffer = new byte[1024]; int numRead; while((numRead = fileInput.read(buffer)) != -1) output.write(buffer, 0, numRead); fileInput.close(); input.close(); output.close(); this.interrupt(); } catch(Exception e) {e.printStackTrace();} } } </code></pre> <p>This is the class that will wait for a connection from the client and then push the update to them as soon as it connects. File Perm is the jar file that i want to send over and for whatever reason it seems to either miss the last 5 bytes or the client doesn't read the last 5 (i don't know which). Here is the client's class of receiving the information here:</p> <pre><code>public void getUpdate(String ip) throws UnknownHostException, IOException { System.out.println("Connecting to update socket"); update = new Socket(ip,10004); BufferedInputStream is = new BufferedInputStream(update.getInputStream()); BufferedOutputStream os = new BufferedOutputStream(update.getOutputStream()); System.out.println("Cleaning GameClient.jar file"); File updated = new File(System.getProperty("user.dir")+"/GameClient.jar"); if(updated.exists()) updated.delete(); updated.createNewFile(); BufferedOutputStream osf = new BufferedOutputStream(new FileOutputStream(updated)); System.out.println("Writing to GameClient.jar"); byte[] buffer = new byte[1024]; int numRead = 0; while((numRead = is.read(buffer)) != -1) osf.write(buffer, 0, numRead); System.out.println("Finished updating..."); is.close(); os.close(); update.close(); osf.close(); } </code></pre> <p>Any help is appreciated. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload