Page 1 of 3 123 LastLast
Results 1 to 20 of 42
  1. #1
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default send bytes from client to server

    my english not in high level so i sorry if have some mistakes..

    i want to send bytes from client to server(actually, i want to send images)
    here some of the code:
    Server:
    Java Code:
            try
            {
                ServerSocket serverSocket = new ServerSocket(8000);
                Socket socket = serverSocket.accept();
                jta.append("connect\n");            
                
                DataInputStream fromClient = new DataInputStream(socket.getInputStream());
                File file = new File(fromClient.readUTF());
                RandomAccessFile toFile = new RandomAccessFile(file, "rw");
                boolean keepReading = true;
                byte[] bytes = new byte[fromClient.readInt()];   
                int fromI, toI;
                while (keepReading)
                {
                    fromI = fromClient.readInt();
                    toI = fromClient.readInt();
                    fromClient.read(bytes, fromI, toI);
                    keepReading = fromClient.readBoolean();
                }
                toFile.write(bytes);
                toFile.close();
                fromClient.close();
                jta.append("Done");
            }
            catch (IOException ex) {}
    client:
    Java Code:
            try
            {
                Socket socket = new Socket ("localhost", 8000);
                DataOutputStream toServer = new DataOutputStream(socket.getOutputStream());
                File file = new File("**");
                RandomAccessFile fromFile = new RandomAccessFile(file, "r");
                toServer.writeUTF(file.getName());
                toServer.writeInt((int)fromFile.length());
                byte[] bytes = new byte[(int)fromFile.length()];
                int fromI, toI;
                fromFile.read(bytes);
                for (int i = 0; i <= bytes.length; i+= 100)
                { 
                    if (i > bytes.length)
                    {
                        fromI = i - 100;
                        toI = bytes.length-1;
                    }
                    else
                    {
                        fromI = i;
                        toI = i+100;
                    }
                    toServer.writeInt(fromI);
                    toServer.writeInt(toI);
                    toServer.write(bytes, fromI, toI);
                    toServer.writeBoolean(true);
                }
                fromFile.close();
                toServer.close();
            }
            catch (IOException ex) {ex.printStackTrace();}
    i dont send all the bytes in one time because the image can be very big
    i try the program and the problem is that the file that the server create is
    empty, i thought that the bytes dont sent ti the server but i dont know why?

    if i had mistakes in english please fix them..
    Last edited by 0xHexaDecimal; 05-27-2010 at 05:53 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    Have you looked at the value returned by read()? Is is greater than 0?
    Are the values of fromI and toI correct on the server?

    How many times do you loop on the server? vs the client?

    Are you sure what you are reading the same types as you send? And in same order? Long vs Int for file length

  3. #3
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Have you looked at the value returned by read()? Is is greater than 0?
    Are the values of fromI and toI correct on the server?

    How many times do you loop on the server? vs the client?

    Are you sure what you are reading the same types as you send? And in same order? Long vs Int for file length
    no i dont check that..
    i think they alright but mybe that the problem..
    if you saw the code you can saw that i sent a boolean vlaue that help me to know
    if the loop will continue or not...
    yea i sure i reading the same types, yes in the same order, i dont have a choice
    i need to cast the file.length long return to int because the array size need to
    be an int value and dont long value(the compiler "shout" on me that i needed that)

    again i sorry if i had mistakes in english..

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    no i dont check that.
    You should check that everything is what you expect.
    Put some println() statements in the server code to display what is received.

    I see that you changed the writeLong to writeInt.

  5. #5
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    You should check that everything is what you expect.
    Put some println() statements in the server code to display what is received.

    I see that you changed the writeLong to writeInt.
    yea but it doesn't matter if i dont change it..
    in the client i got run errors:
    Java Code:
    java.lang.ArrayIndexOutOfBoundsException
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:87)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
        at java.io.DataOutputStream.write(DataOutputStream.java:90)
        at TryC.<init>(TryC.java:37)
        at TryC.main(TryC.java:48)

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    Ok, now we're making some progress
    What was the size of the array and what was the problem index value?

    What lines of code are at TryC line 37?

  7. #7
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    line 37: toServer.write(bytes, fromI, toI);

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    Ok, some progress but slow.
    What is the size of bytes?
    What is the value of fromI?
    What is the value of toI?

  9. #9
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Ok, some progress but slow.
    What is the size of bytes?
    What is the value of fromI?
    What is the value of toI?

    i try to run the program and all time i print the indexes..
    i try to send a file with size 200b
    in the first time the indexes was from 0 to 100
    after the indexes was from 100 to 200..
    here the problem, but i dont understand why because: toI = bytes.length-1;

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    Can you copy and paste the exact output from the println() statements and NOT edit them?

  11. #11
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Can you copy and paste the exact output from the println() statements and NOT edit them?
    why you think that i will edit them 0__0..
    200
    fromI: 0 toI: 100
    fromI: 100 toI: 200

    the first number is the file size..

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    Read the doc for the write() method. Look at the third parameter especially!

  13. #13
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Read the doc for the write() method. Look at the third parameter especially!
    oops, thank you man.. i was so idiot..
    now there is no error but the file is still empty..

    i understand..

    it worked just for files that their size divided by 100:
    Java Code:
                for (int i = 0; i < bytes.length; i+= 100)
                { 
                    if (i >= bytes.length)
                    {
                        fromI = i - 100;
                        len = bytes.length % 100;
                        conti = false;
                    }
                    else
                    {
                        fromI = i;
                        conti = true;
                    }
                    jta.append("fromI: " + fromI + " len: " + len + "\n");
                    toServer.writeInt(fromI);
                    toServer.writeInt(len);
                    toServer.write(bytes, fromI, len);
                    toServer.writeBoolean(conti);
                }
    Last edited by 0xHexaDecimal; 05-28-2010 at 04:55 PM.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    Add some more debug code to show some of what is being written and being read. For example:
    System.out.println("Writing/reading" + bytes[0] + " " + bytes[1] ...);

    Also on the read side capture and display the number of bytes read:
    int nbrRead = fromClient.read(bytes, fromI, toI);
    Sys....println("read " + nbrRead);

    file is still empty
    Does that mean output file has a length of 0?

  15. #15
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Add some more debug code to show some of what is being written and being read. For example:
    System.out.println("Writing/reading" + bytes[0] + " " + bytes[1] ...);

    Also on the read side capture and display the number of bytes read:
    int nbrRead = fromClient.read(bytes, fromI, toI);
    Sys....println("read " + nbrRead);


    Does that mean output file has a length of 0?
    Java Code:
    1568
    fromI: 0 len: 100
    fromI: 100 len: 100
    fromI: 200 len: 100
    fromI: 300 len: 100
    fromI: 400 len: 100
    fromI: 500 len: 100
    fromI: 600 len: 100
    fromI: 700 len: 100
    fromI: 800 len: 100
    fromI: 900 len: 100
    fromI: 1000 len: 100
    fromI: 1100 len: 100
    fromI: 1200 len: 100
    fromI: 1300 len: 100
    fromI: 1400 len: 100
    fromI: 1500 len: 100
    in the end(fromI 1500) the len need to be 68 but it 100 and because that i
    got an error

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    So add some code to set len to the correct value at the end.

  17. #17
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    nothing!!!!!!!!!1

    edit:
    Java Code:
            try
            {
                Socket socket = new Socket ("localhost", 8000);
                DataOutputStream toServer = new DataOutputStream(socket.getOutputStream());
                File file = new File("C:\\Documents and Settings\\owner\\Desktop\\פיקסל ארט\\M16-part2.png");
                RandomAccessFile fromFile = new RandomAccessFile(file, "r");
                toServer.writeUTF(file.getName());
                toServer.writeInt((int)fromFile.length());
                jta.append(fromFile.length() + "\n");
                byte[] bytes = new byte[(int)fromFile.length()];
                int fromI, len;
                fromFile.read(bytes);
                boolean conti;
                for (int i = 0; i < bytes.length; i+= BYTE2READ)
                { 
                    fromI = i;
                    len = (fromI+BYTE2READ > bytes.length) ? bytes.length%BYTE2READ : BYTE2READ; 
                    conti = i < bytes.length;
                    jta.append("fromI: " + fromI + " len: " + len + "\n");
                    toServer.writeInt(fromI);
                    toServer.writeInt(len);
                    toServer.write(bytes, fromI, len);
                    toServer.writeBoolean(conti);
                }
                fromFile.close();
                toServer.close();
            }
            catch (IOException ex) {ex.printStackTrace();}
    it worked but after that i can't see the image, the bytes are write to the file but
    i dont know why i can't see the image..
    Last edited by 0xHexaDecimal; 05-29-2010 at 10:45 AM.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    i can't see the image.
    What does that mean? Have you compared the files? Are they the same size? Do they have the same contents?

  19. #19
    0xHexaDecimal is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What does that mean? Have you compared the files? Are they the same size? Do they have the same contents?
    i fixed that..
    the problem was toServer.writeBoolean(i < bytes.length);
    it need to be toServer.writeBoolean(i+BYTE2READ < bytes.length);
    now it worked..
    but the image's size need to be less than 1 mega..
    Last edited by 0xHexaDecimal; 05-29-2010 at 03:10 PM.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    image's size need to be less than 1 mega
    The client can use the File class object to test the files length and then do something.

Page 1 of 3 123 LastLast

Similar Threads

  1. send file via client - server model
    By spasavvas in forum Networking
    Replies: 15
    Last Post: 08-13-2010, 11:45 AM
  2. how to send mp3 file from server to client
    By Jigga008 in forum Networking
    Replies: 0
    Last Post: 12-01-2009, 12:02 PM
  3. Server socket - send image to client
    By Hinty in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 07:39 AM
  4. send/read int in a client/server app
    By dim_ath in forum New To Java
    Replies: 2
    Last Post: 01-03-2008, 01:03 PM
  5. how to send .jar files client to server
    By gobinathm in forum Networking
    Replies: 1
    Last Post: 12-25-2007, 04:05 AM

Posting Permissions

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