socket problem,,,,, plz help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • green
    New Member
    • Oct 2007
    • 7

    socket problem,,,,, plz help

    my problem is

    I have socket between server and a client
    the client sending data to the server, so the server respond to the client with the response
    this my code for receive the response data
    ----------------------------------------------------------
    os.write(getAut henticationRedG reenInfoString( ));
    os.flush();
    Thread.sleep(30 00);
    DataInputStream is = new DataInputStream (mySocket.getIn putStream());
    int rr;
    int nn = is.available();
    if(nn > 0)
    {
    byte[] bb = new byte[100000];
    rr = is.read(bb); // rr is Number of read bytes
    }

    look to the code, u can notice that i have 2 variables nn and rr
    nn is the avialbe bytes in the InbutStream and
    rr is the nubmer of byes did read from the InbutStream.

    when i make a debug , nn = 8192 and rr = 22233 !!!!!!!!!!!!!


    the question is HOW can I read _ALLl _ the received data from the InbutStream

    waiting ur help
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by green
    the question is HOW can I read _ALLl _ the received data from the InbutStream
    You can't know that because you have no control of the sending party. The available()
    method returned 8192 because that seemed to be the size of the underlying buffer
    and it was full of data already. Then you gave it a 100,000 byte size buffer and 22,000
    and then some were filled. That's the amount of bytes your read() call managed to
    read.

    You have to come up with some sort of 'protocol' between the sending party and the
    receiving party, e.g. first the number of bytes to be send is send and then the data is
    pushed over the line. Otherwise your receiving end doesn't know how many bytes are
    being send over the line.

    kind regards,

    Jos

    Comment

    • green
      New Member
      • Oct 2007
      • 7

      #3
      many thanks for u and for ur valuable time
      ---------------------------------------------------------------
      so I tried to get all the data by looping with the InputStream, but I get another problem


      [code=java]
      os.write(getAut henticationRedG reenInfoString( ));
      os.flush();
      Thread.sleep(30 00); // must sleep a while to get the server response
      DataInputStream is = new DataInputStream (mySocket.getIn putStream());
      int i,r;
      byte [] finalByteArray = new byte[70000];
      int finalLength;
      int nn = is.available();
      if(nn > 0)
      {
      byte[] bb = new byte[30000];
      r = is.read(bb);//,0,bb.length);
      System.arraycop y(bb, 0, finalByteArray, 0, r);
      finalLength = r;
      r = is.read(bb);
      if(r >0)
      {
      System.arraycop y(bb, 0, finalByteArray, finalLength , r);
      finalLength += r;
      r = is.read(bb);
      }
      }
      returnString = decryptReturned Response(finalB yteArray);
      System.out.prin tln(returnStrin g);[/Code]

      this code get all the received data..
      but in the second receive I get a socket write error Exception

      so
      plz help me
      Last edited by JosAH; Oct 21 '07, 02:02 PM. Reason: change the tag; Java code is not a poem ;-)

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by green
        many thanks for u and for ur valuable time
        ---------------------------------------------------------------
        so I tried to get all the data by looping with the InputStream, but I get another problem


        [code=java]
        os.write(getAut henticationRedG reenInfoString( ));
        os.flush();
        Thread.sleep(30 00); // must sleep a while to get the server response
        DataInputStream is = new DataInputStream (mySocket.getIn putStream());
        int i,r;
        byte [] finalByteArray = new byte[70000];
        int finalLength;
        int nn = is.available();
        if(nn > 0)
        {
        byte[] bb = new byte[30000];
        r = is.read(bb);//,0,bb.length);
        System.arraycop y(bb, 0, finalByteArray, 0, r);
        finalLength = r;
        r = is.read(bb);
        if(r >0)
        {
        System.arraycop y(bb, 0, finalByteArray, finalLength , r);
        finalLength += r;
        r = is.read(bb);
        }
        }
        returnString = decryptReturned Response(finalB yteArray);
        System.out.prin tln(returnStrin g);[/Code]

        this code get all the received data..
        but in the second receive I get a socket write error Exception

        so
        plz help me
        That code doesn't make sense. As I wrote in my previous reply: your receiver
        can't know if it read all the data. A simple second attempt to read some more
        doesn't solve it either. You'd need a third, fourth etc. attempt and you still
        wouldn't know if you'd read it all. You definitely need a little 'protocol', Make
        your sender send the number of bytes it wants to send first, followed by those
        bytes. Your receiver can anticipate on that: first it receives a number indicating
        the number of bytes it should read afterwards.

        kind regards,

        Jos

        Comment

        Working...