Reading unkown number of bytes

Hi all !
I am trying to read unknown number of bytes from the socket.Meanwhile I need to process the bytes depending on the message type in the header.I get messages like this header,body,header,body header,body header,body.....the message type is specified in the header.Each message type has different body size.So depending on that I have to read the next few bytes from the socket....say 300 or 600 depending the on the message type in the header.I get the bytes continioulsy like this.Since I cannot read byte by byte due performance reason is there any other way to do that.....any help will appreciated
sash

Assuming you have a consistent header size and format, you can just read that in one call. I prefer sticking the size of body in the header. That way I can pluck that value out of the header and use it for the next read.

Similar Messages

  • Reading unknown number of bytes !

    Hi all !
    I am trying to read unknown number of bytes from the socket.Meanwhile I need to process the bytes depending on the message type in the header.I get messages like this header,body,header,body header,body header,body.....the message type is specified in the header.Each message type has different body size.So depending on that I have to read the next few bytes from the socket....say 300 or 600 depending the on the message type in the header.I get the bytes continioulsy like this.Since I cannot read byte by byte due performance reason is there any other way to do that.....any help will appreciated
    sash

    A simple workaround is to use a java.io.PushbackInputStream
    Read enough bytes to get the header, parse the header bytes, and push the remainder back into the stream to be read as the body.

  • TCP Read undifined number of bytes

    Hello LabVIEW RT users
    I'm trying to convert a java application to my LabVIEW RT Smart Camera
    In my camera I have a working TCP server which reads and writes some data to/from a robot. My problem is that in LabVIEW i have to specify who many bytes I'm reciving to get it to work. But in my Java application the BufferedReader took care of this. I can't control what the robot is sending but it could be things like these strings
    GET X
    SET R 1
    SET X 443
    SET X 11
    GET START
    Somone who knows how I best handle this problem? I have tried to change the mode of the TCP read to CRLF but without succes

    If you have 1MB if random binary data, is highly likely that there will be a CRLF embedded somewhere by chance, so that won't work. None of your vague comments and suggestions make any sense so far. This is most likely a very simple problem that you are way overthinking and trying to overengineer. Billions of bytes of variable lenght data are transmitted every second across the world and nobody else seems to have these problems.
    Please be more specific about your application:
    Where is the data coming from?
    Is it also LabVIEW and do you have control over the sending program?
    How easy is it to tell if it is binary or formatted?
    Can you tell from the data pattern once an entire set is received?
    how long are the pauses between datasets?
    What happens to the data after it is received? (stream to disk, scan, parse, etc.)
    LabVIEW Champion . Do more with less code and in less time .

  • Reading Unkown length of bytes

    I am reading bytes through socket, the length of bytes is uknown,
    i am using DataInputStream and using while(inp.available()>0).
    the problem here if a network slow it will go out from the loop.
    I want to read all the data no matter how long it will take. any idea???

    I can know when the other side closes the socket,
    it's easy. but the problem is not with the other
    side. the problem how can i keep reading tell he
    close the socke if there will be a delaysI think you have lost me! Create a loop which keeps reading (using read(byte[] b))from the input stream until EOF is reached.

  • How to get the number of bytes at TCP port

    Hi all,
    How to get the number of bytes to read at the TCp port...as someone had suggested in some forum we do read the number of bytes first and then pass this...
    but we get a problem when we have FF data in this...because then it sends 2 FF data...and cause of this we skip the last data...is there any solution for the same?

    Hi
    In LabVIEW you don't have the same property as in serail port.
    You havn't "Byte at TCPIP port".
    if you developp a protocol, one soltion, is to send the size to read.
    Ingénieur d'Application / Développeur LabVIEW Certifié (CLD)
    Application Engineer / LabVIEW Certified Developer (CLD)

  • Easily read strings while tracking the number of bytes read?

    Hi all,
    I'm after a way to easily parse and read strings from a file, while also being able to check (at any point) how many bytes have currently been read from that file.
    I currently use the following for the string processing:
    BufferedReader in = new BufferedReader(new FileReader(filename));
    String str = null;
    while ((str = in.readLine()) != null)
      // process the string
    }That's all fine, but the files I'm reading can be very large (multi GB) so obviously it can take a while to read them. During this time I pop up a progress bar, and attempt to track the progress of the read. The files I'm working with just now come with a header on the first line that states how many "somethings" (that I happen to be looking for) will appear in the file. I can use that number to set the maximum value for the progress bar, and update its current value as I find them.
    However... I'm also about to start working with files that don't contain this information. I've thought of two ways of knowing how much work has to be done in advance of the read so the maximum value for the progress bar can be set:
    1) Quickly count the number of lines in the file without doing any processing. This works, but can still take some time for large files, even with more efficient reading algorithms.
    2) Use File.length() to set the maximum to be the number of bytes that will read.
    I'd like to use 2), but can't work out a way to use simple String based file parsing (as in the code above), but also be able to know how many bytes have been read so far. Using this code means I don't have to worry about end of line terminators, charset encoding, etc - the Reader does it for me.
    Any suggestions?
    Thanks

    import javax.swing.*;
    Component parent; // might be null, or your JFrame
    String message; // message to display in the progress bar
    BufferedReader br = new BufferedReader(new InputStreamReader(new ProgressMonitorInputStream(parent, message, new FileInputStream(file)), charset));

  • How do I write and read a specified number of bytes using C++?

    I need to send one byte commands followed by 1 or 2 byte data as well as read 1 or 2 byte commands from a RS232 device. How do I ensure that only 1 byte is sent for the commands or 2 bytes of data using VISA?

    Hey BMas05,
    Using VISA in C++ you can strings or bytes depending on which polymorphic version of the write you are using. One of the parameters for the VISA Write is the byte array and the number of bytes. This is if you are using the VISA classes for C++. You might have to have Measurement Studio to get these classes.
    There is a really good example that installs on your computer at C:\Program Files\National Instruments\MeasurementStudio\VC\Examples\Io\Visa\Serial Visa.
    This example shows how you can write just bytes or strings and you can select how many bytes to read back.
    I hope this helps out.
    JoshuaP
    National Instruments

  • ORA-27063: number of bytes read/written is incorrect

    Hello -
    I am getting this error because my filesytem is at 100%:
    ORA-01114: IO error writing block to file 202 (block # 423324)
    ORA-27063: number of bytes read/written is incorrect
    However, when I query the dba_data_files, and v$datafile views, I do not see a reference to file 202. Where can I get this information?
    Thanks,
    Mike

    Mike,
    Looks like you got a solution, however, just FYI, tempfiles are numbered starting w/ db_files+1, so, likely, your value of db_files is 200, the error occurred on your number 2 tempfile.
    -Mark

  • TCP/IP Read - How do I find the number of bytes available

    It appears that this is not directly possible. I am trying to get around
    this by doing a tcp/ip buffered read with a very short timeout (0 or 1ms).
    In theory it seems like I should be able to keep making the read call until
    the exact number of bytes (4 in this case) pops out. It mostly works but
    every so often hangs for some period of time and then resumes getting the 4
    byte packets. Anybody have any idea what is going on?

    The first thing I'm thinking of is that your delay is too short. Also, try to increase the packet length or use Immediate Mode (even CRLF MODE if you're transmitting text).
    Another idea (if your application really needs that and you have LV / Windows) is to use MS Winsock Control (wait for DataArrival event and read BytesReceived).
    Anyway, tune in your communication (packets and delays) taking in account details about your network or modem.
    Good luck!

  • Length specified in network packet payload did not match number of bytes read; the connection has been closed.

     Hello every one
    I am getting this in my event log from time to time . not sure what is that ?
    Is this a hacker trying to send rubbish data to SQL through the port ?
    Any help is appreciated .
    Length specified in network packet payload did not match number of bytes read; the connection has been closed. Please contact the vendor of the client library. [CLIENT: someip]

    Thank you all for the reply  , 
    my server is on port  1533 . Port  1433  is closed  on my server .
    All the requests are coming from my own ip , i feel this is funny . 
    Is my server hacked then ? 
    Or could be my ISP doing any routine tests ? 
    Also is this is hack attempt , what does this hacker think he will benefit from this ? Could this bring the SQL  server down  ?  
    I was thinking about chaning port to something else ,  1344 or something , u think this can be better  ?

  • Creation of Unkown Number of Vectors

    I'm trying to take items from a vector and placing each into their own vectors. How do I go about creating an unkown number of vectors to hold all the items?

    If feasible, try to go with a simple two-dimensional array of booleans, or alternatively a one-dimensional array of BitSets. Alternatively, I have this for you:
    import  java.util.*;
    public class Point  implements Cloneable {
        private int  x;
        private int  y;
        public Point(int x, int y) {
         this.x = x;
         this.y = y;
        } //* Point(int, int)
        public boolean equals(Object o) {
         if(o instanceof Point) {
             Point  other = (Point)o;
             return  (other.x == x) && (other.y == y);
         return  false;
        } //* equals(Object)
        public int hashCode() {
         int  s = x + y;
         return  ((s * (s+1)) >> 1) + y;
        } //* hashCode()
        public String toString() {
         return "(" + x + ", " + y + ")";
        } //* toString()
         * Input: Set of Points
         * Output: List of Sets of Points, each Set representing touching Points
        public static List group(Set points) {
         Set   pts = new HashSet(points);
         List  res = new ArrayList();
         try {
             while(!pts.isEmpty()) {
              Set   group = new HashSet();
              List  roots = new LinkedList();
              Point  root = (Point)pts.iterator().next();
              roots.add(root);
              pts.remove(root);
              do {
                  root = (Point)roots.remove(0);
                  group.add(root.clone());
                  root.x++;
                  if(pts.remove(root))  roots.add(root.clone());
                  root.y++;
                  if(pts.remove(root))  roots.add(root.clone());
                  root.x--;
                  if(pts.remove(root))  roots.add(root.clone());
                  root.x--;
                  if(pts.remove(root))  roots.add(root.clone());
                  root.y--;
                  if(pts.remove(root))  roots.add(root.clone());
                  root.y--;
                  if(pts.remove(root))  roots.add(root.clone());
                  root.x++;
                  if(pts.remove(root))  roots.add(root.clone());
                  root.x++;
                  if(pts.remove(root))  roots.add(root.clone());
              while(!roots.isEmpty());
              res.add(group);
         catch(CloneNotSupportedException e)  {}
         return  res;
        } //* group(Set)
        public static void main(String[] args) {
         //* quick and dirty - I know
         System.out.println(group(new HashSet(Arrays.asList(new Point[] {
             new Point(1, 2), new Point(1, 4), new Point(3, 4), new Point(1, 1),
             new Point(1, 5), new Point(6, 4), new Point(6, 3), new Point(7, 3)        
        } //* main(String[])
    //* CLASS: Point
    //*****************************************************************************

  • Output says "The number of bytes in the file are 0" but the file has bytes

    Dear Java People,
    Why would an output say a file has 0 bytes when upon doing a search for the file in Windows Explorer it say the file has 1 -4 kbytes ?
    for example part of my output was :
    "the number of bytes in TryFile3.java are 0"
    caused by the following lines of code:
    System.out.println("\n" + contents[i] + " is a " +
    (contents.isDirectory() ? "directory" : "file\n") +
    " last modified on " + new Date(contents[i].lastModified())
    + "\nthe number of bytes in TryFile.java are " + myFile.length());
    thank you in advance
    below are the two program classes
    Norman
    import java.io.File;
    import java.io.FilenameFilter;
    import java.util.Date;
    public class TryFile3
       public static void main(String[] args)
           //create an object that is a directory
             File myDir =
            new File("C:\\Documents and Settings\\Gateway User\\jbproject\\stan_ch9p369");
              File myFile = new File(myDir, "TryFile3.java");
            System.out.println("\n" + myDir + (myDir.isDirectory() ? " is" : " is not")
            + " a directory.");
             System.out.println( myDir.getAbsolutePath() +
             (myDir.isDirectory() ? " is" : " is not") + " a directory.");
              System.out.println("The parent of " + myDir.getName() + " is " +
              myDir.getParent());
               //Define a filter for java source files Beginning with the letter 'F'
               FilenameFilter select = new FileListFilter("F", "java");
               //get the contents of the directory
               File[] contents = myDir.listFiles(select);
                //list the contents of the directory
             if(contents != null)
                 System.out.println("\nThe " + contents.length  +
                 " matching item(s) in the directory " + myDir.getName() + " are:\n " );
                 for(int i = 0; i < contents.length; i++)
                   System.out.println("\n" +  contents[i] + " is a " +
                   (contents.isDirectory() ? "directory" : "file\n") +
    " last modified on " + new Date(contents[i].lastModified())
    + "\nthe number of bytes in TryFile3.java are " + myFile.length());
    else {
    System.out.println(myDir.getName() + " is not a directory");
    System.exit(0);
    import java.io.File;
    import java.io.FilenameFilter;
    import java.util.Date;
    public class FileListFilter implements FilenameFilter
    private String name; // file name filter
    private String extension; // File extension filter
    public FileListFilter(String name, String extension)
    this.name = name;
    this.extension = extension;
    // static boolean firstTime = true;
    public boolean accept(File diretory, String filename)
    //the following line of code can be inserted in order to find out who called the method
    // if(firstTime)
    // new Throwable("starting the accept() method").printStackTrace();
    boolean fileOK = true;
    //if there is a name filter specified, check the file name
    if(name != null)
    fileOK &= filename.startsWith(name);
    //if there is an extension filter, check the file extension
    if(extension != null)
    fileOK &= filename.endsWith('.' + extension);
    return fileOK;

    System.out.println("\n" + contents + " is a " +
    (contents.isDirectory() ? "directory" : "file\n") +
    " last modified on " + new Date(contents.lastModified())
    + "\nthe number of bytes in TryFile.java are " + myFile.length());I haven't read any of your italicized code, but perhaps there is a good reason why you have "myFile.length()" and not "contents.length()" in this line of code?

  • How to reduce the number of bytes for a picture

    I want to reduce the number of bytes used for a picture to be sent to a site like Ebay or craigslist. I son't see any options in Iphoto for this purpose.

    You can do it in Preview.
    Open the image in Preview. Under Tools, click Adjust Size.
    Scale it down to the smallest acceptable size (the website should give a suggested resolution).
    Then go to File -> Export, save it as a Jpeg.
    Smaller size image means smaller file size.

  • How can I monitor number of bytes downloaded so I don't go over my plan limit?

    I have a limited number of bytes I can download per day. How can I monitor this on a daily usuage?

    I think that is exactly what i need. I have downloaded and restarted firefox. I entered the user name and password I use to sign on to the internet and get a red x next to the icon and it is not working. any suggestions?

  • Hello , FMS is how to prevent the client into a large number of bytes?

    Hello , FMS how to prevent the client to pass a large number of bytes , such as one person put a 1G file in the argument , I also silly to receive ?Although there Client.setBandwidthLimit ( ) limit his maximum traffic per second , but is there a way , one more than the maximum amount of bytes to disconnect his.I assume that methods to determine the length is also obtained all of his transfer is finished , in order to determine out of it .

    How to limit the size of the parameters of the method.I wrote a method in the main.asc then the client NetConnection.call assignment, but if the client is malicious to upload very large data, how to limit it, I view the document did not find the clues, I hope that those parameters up to100KB.

Maybe you are looking for