Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 60
Like Tree1Likes

Thread: A simple connection between 2 computers

  1. #21
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    The read method returns the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
    So I need to run a loop while the returned value not equal to -1.
    But what to do with the array size?The read method ask for a byte array as a parameter so I have to declare it before.
    And it's wierd cause the error don't even come from there .. all the erros are output errors and line 24 is the out.write line.

    edit:
    wierd, it's working
    Java Code:
    byte[]ar = new byte[9999];
    while(in.read(ar)!=-1);
    System.out.println(ar);
    So I got only 2 more questions now:
    What to do with the array size?
    How do I execute my exe from this byte array?writing it to a file exe suffix?I can't test it right now cause my array got many values that shouldn't be there.
    All the default values(zeros) the fill all the empty indexes.
    Last edited by tnrh1; 01-28-2012 at 12:41 AM.

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    What to do with the array size?
    Back in post#12 I suggested a header record. You could send the size of the byte array as an int before sending the byte array.

  3. #23
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    You are a damn smart!
    Ok I got the byte array completely on the client side, now how do I convert it back to exe?
    Norm really thanks for your help.

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    now how do I convert it back to exe?
    You'd do the write step like you did the read step. Create a file with the name you want and write the bytes to the file.

  5. #25
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    Java Code:
    FileOutputStream fos = new FileOutputStream("test.jar");
    fos.write(ar);
    Yes I did, but the jar file won't open "invaild or corrupt jar file".

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    Did the total number of bytes you wrote out to the file match the number in the original file?
    Do you have a compare program to compare the input file to the output file? Are the files the same size?

    For testing, use a file that is easy to look at to see if there any differences. For example a text file containing simple text like:
    abcdefgh.....through yz(26 letters) repeated many times to make a large file.

  7. #27
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    lol it's funny, I did everything you said before you posted it.
    Both of the arrays got the same values and same length.
    I tested it with text files and it did worked and created a new one on the client computer with the correct text in it.
    Looks like when it comes to exe's/jar files it's another story.
    Do you have any idea?

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    Did you compare the .exe files?

    Another idea: write a simple program to create a test file containing byte values from 0 to 255. Then another program to read and verify the file has those values. Send this test file with its 256 different byte values and see if it is received ok. With these programs you could see what byte values are not bring sent correctly.

  9. #29
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    lol the first exe got 11.7 kb and the new exe got 252 bytes.
    I had a mistake, when you print 2 things from diffrent classes the console doesn't changed.
    The first ar(server side) got 12028 bytes and the other ar(client side) got 252 bytes.

    So it has to be something with the write method.
    The api says: "Writes b.length bytes from the specified byte array to this file output stream." which sounds good.

    And I did another test, it copy fine txt files with small length.
    I tried to copy this txt file:
    aaaaaaaaaaaaaaaaaaassssssssssdasdfdghaweragsdrfasb e
    aaaaaaaaaaaaaaaaaaassssssssssdasdfdghaweragsdrfasb e
    aaaaaaaaaaaaaaaaaaassssssssssdasdfdghaweragsdrfasb e
    aaaaaaaaaaaaaaaaaaassssssssssdasdfdghaweragsdrfasb e
    aaaaaaaaaaaaaaaaaaassssssssssdasdfdghaweragsdrfasb e
    aaaaaaaaaaaaaaaaaaassssssssssdasdfdghaweragsdrfasb e
    And the new txt file looked like this:
    ghaweragsdrfasbedrfasbe
    aaaaaaaaaaaaaaaaaaassssssssssdasdfd
    -.-
    I'm not sending the bytes correctly

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    Ok, now you know where to look for the problem.

  11. #31
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    As usual, one problem leads to another.
    To send an int I can't use this stream so I create a PrintWriter.
    Server:
    Java Code:
    pw.println(ar.length); //pw = PrintWriter
    pw.close();
    		
    out.write(ar); //out = ObjectOutputStream
    out.close();
    And the client need to recieve an int so I created a BufferedReader that will recieve a string and will be casted into int:
    Client:
    Java Code:
    byte[]ar = new byte[Integer.parseInt(br.readLine())];  //br = BufferedReader
    while(in.read(ar) != -1); //in = ObjectInputStream
    It recieve the length perfectly and build the array as it should be.
    But when it comes to passing the values something is going wrong, after debuging I saw that the loop ran 1 time because the first value that the read method return was -1 which means there is nothing to read.
    I printed the array server sided to make sure there is something to read, and there was.

    Ouch =\
    Length: 316
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    I'm confused. Earlier you said:
    I tested it with text files and it did worked and created a new one on the client computer with the correct text in it.

    What's changed?

    Did you read the API doc for the PrintWriter class? Are you trying to use it to send non-text data?

    You need to use classes that send bytes, not text.

  13. #33
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    It did worked but not completely because I used writeByte method to send the array length, and this method is limited to a certain number so my new array had 64 indexes.
    So I created a new stream and sent a string (array length) and casted it into int and it's working fine!
    But now there is a problem in the bytes sending.
    Damn why it's so hard to send an int and a byte array.

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    Have you tried the DataOutputStream?

  15. #35
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    This class has saved my life, and you lead me to her, thank you!
    It's copying txt files 100% every byte is passing to the client even in exe's but when I write the exe I get file is corrupt =\
    The exe's byte are the same 100%.
    Everything work except of exe's.

    Server
    Java Code:
    DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
    byte[]ar = getBytesFromFile(new File("tictactoe.jar"));
    dos.writeInt(ar.length);
    dos.write(ar);
    dos.close();
    Client:
    Java Code:
    DataInputStream dis = new DataInputStream(socket.getInputStream());
    byte[]ar = new byte[dis.readInt()];
    while(dis.read(ar) != -1);
    
    FileOutputStream fos = new FileOutputStream(new File("tictactoe2.jar"));
    fos.write(ar);
    fos.close();
    I will say it again, the bytes are passing! both of the exe's has the same amount of bytes.

  16. #36
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    write the exe I get file is corrupt =\
    The exe's byte are the same 100%.
    I don't understand this. If the files are 100% the same, then what is the OS looking at to determine that one is corrupt?

    Have you tried other binary files, like jar or an image?

  17. #37
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    It's not working with images also.
    Here is a picture to demonstrate it:
    A simple connection between 2 computers-.png

    It's very wierd that txt files are working and jar/exe/png etc.. are not.

  18. #38
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    If the input file and the output file are 100% the same then I don't understand what the problem is?
    Are you sure the files are 100% the same? How are you determining that?

  19. #39
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    When I say that they are 100% the same I speak about the bytes amount in each file as you can see in the picture.
    And another proof is that it copying any weight of txt's perfectlly.
    So I guess that just writing to a file and save it as an exe is incorrect, there must be another way to convert bytes to exe/jar etc ...

  20. #40
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    another way to convert bytes to exe/jar etc ...
    You do NOT convert them. That is the problem.
    You must read and write bytes and not chars. Some bytes are not translatable to chars and end up as ? chars.


    The same number of bytes in two files does not make them 100% the same.
    "ASDF" is not the same as
    "1234"

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Playing sounds on other computers
    By jeffpaulwilson in forum AWT / Swing
    Replies: 0
    Last Post: 09-14-2011, 09:25 PM
  2. Help needed!!!!! choosing network computers
    By apoorv in forum Networking
    Replies: 6
    Last Post: 03-22-2011, 05:33 PM
  3. Need to have multiple computers run same java program
    By thehighlander12 in forum Networking
    Replies: 11
    Last Post: 07-07-2010, 02:30 PM
  4. Career Paths in Computers
    By Lil_Aziz1 in forum New To Java
    Replies: 26
    Last Post: 06-30-2010, 01:59 PM
  5. Sharing a game instance over two computers
    By keiys in forum Java Gaming
    Replies: 0
    Last Post: 04-21-2010, 02:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •