Socket Data Reassembling

Hi,
I have some problem of reading data from a Socket. It seems to be that TCP or whatever fragments the data that the server send to a client (my application). Is it possible this or am I wrong on reading with getInputStream.read ???
Thank you for your advice.
Best Regs.
Thierry

Yes, you probably are using the InputStream wrongly.
TCP/IP is packet based transmission protocol. All data sent through TCP/IP will be divded into packages, TCP makes sure that the packages are labeled with index number. If the other side finds packages out of sequence, they are reorderered. If packages are not aknowledged they are resent etc etc.
All this happens without your knowledge, for you the InputStream will give you a steady stream of data, not all at the same time, but as they come more and more data will be available.
As previous poster said, read the tutorial on streams and you should be up and go very soon.
Regards,
Peter Norell

Similar Messages

  • Socket data transfer too long

              hi
              when i start up the 1st WLS for clustering, the following exception message is
              shown constantly:
              <MulticastSocket> Error sending blocked message
              java.io.IOException: A message for a socket data transfer is too long.
              at java.net.PlainDatagramSocketImpl.send(Native Method)
              what could cause the exception and is it a fatal error?
              appreciate if any help can be given.
              thanx ...
              yunyee
              

              hi
              i'm running on AIX 4.3.3.
              the jdk version is 1.2.2
              Viresh Garg <[email protected]> wrote:
              >Could you also post your OS and JDK version.
              >Viresh Garg
              >
              >Yun Yee wrote:
              >
              >> hi Shiva
              >>
              >> i'm running WLS5.1. and i have Commerce Server 3.1 as well.
              >> i have service pack 8.
              >> can the exception be rectified by service packs?
              >>
              >> thanx
              >>
              >> yunyee
              >>
              >> "Shiva" <[email protected]> wrote:
              >> >
              >> >Hi,
              >> >Would help if you could also tell about the WLS version, Service Packs
              >> >and the
              >> >environment you are using.
              >> >
              >> >Shiva.
              >> >
              >> >"Yun Yee" <[email protected]> wrote:
              >> >>
              >> >>hi
              >> >>
              >> >>when i start up the 1st WLS for clustering, the following exception
              >> >message
              >> >>is
              >> >>shown constantly:
              >> >><MulticastSocket> Error sending blocked message
              >> >>java.io.IOException: A message for a socket data transfer is too
              >long.
              >> >> at java.net.PlainDatagramSocketImpl.send(Native Method)
              >> >>
              >> >>what could cause the exception and is it a fatal error?
              >> >>
              >> >>appreciate if any help can be given.
              >> >>thanx ...
              >> >>
              >> >>yunyee
              >> >>
              >> >
              >
              

  • Socket data corruption

    I've a problem with sockets that I can reproduce with this simple code:
    public class Server {
    public static void main(String[] args) throws Exception {
    ServerSocket server = new ServerSocket(8888);
    System.out.println("Listening...");
    Socket socket = server.accept();
    InputStream in = socket.getInputStream();
    byte[] data = new byte[16 * 1024];
    while (true)
    int i = in.read();
    if (i != 202)
    throw new Exception("Error: " + i);
    else
    System.out.println("Ok");
    in.read(data, 0, 16 * 1024);
    public class Client {
    public static void main(String[] args) throws Exception {
    System.out.println("Connecting...");
    Socket socket = new Socket("localhost", 8888);
    OutputStream out = socket.getOutputStream();
    byte[] data = new byte[16 * 1024];
    while (true)
    out.write(202);
    out.write(data, 0, 16 * 1024);
    The "Client" class connects to the socket listening in "Server" class and send a byte and a 16k block in a loop. The "Server" receives the data and verifies the byte (It should be 202). At some point the test fails receiving a "0".
    I'm using JRE version 1.4.2_04-b05 under Windows XP.
    Any suggestions?
    Thanks,
    Juan Wajnerman

    You can't predict what length a read will produce, it
    may return any number of bytes up to the number
    requested. You're right! Thanks... I don't know how I could forget that :-(
    The writes also won't necessarily send
    anything into the network if you don't do a flush.
    Note that out.write(data, 0, 16 * 1024); will write
    16K of whatever is in the buffer.Thank you very much!
    Juan

  • Problems Reading SSL  server socket  data stream using readByte()

    Hi I'm trying to read an SSL server socket stream using readByte(). I need to use readByte() because my program acts an LDAP proxy (receives LDAP messages from an LDAP client then passes them onto an actual LDAP server. It works fine with normal LDAP data streams but once an SSL data stream is introduced, readByte just hangs! Here is my code.....
    help!!! anyone?... anyone?
    1. SSL Socket is first read into  " InputStream input"
    public void     run()
              Authorization     auth = new Authorization();
              try     {
                   InputStream     input     =     client.getInputStream();
                   while     (true)
                   {     StandLdapCommand command;
                        try
                             command = new StandLdapCommand(input);
                             Authorization     t = command.get_auth();
                             if (t != null )
                                  auth = t;
                        catch( SocketException e )
                        {     // If socket error, drop the connection
                             Message.Info( "Client connection closed: " + e );
                             close( e );
                             break;
                        catch( EOFException e )
                        {     // If socket error, drop the connection
                             Message.Info( "Client connection close: " + e );
                             close( e );
                             break;
                        catch( Exception e )
                             //Way too many of these to trace them!
                             Message.Error( "Command not processed due to exception");
                             close( e );
                                            break;
                                            //continue;
                        processor.processBefore(auth,     command);
                                    try
                                      Thread.sleep(40); //yield to other threads
                                    catch(InterruptedException ie) {}
              catch     (Exception e)
                   close(e);
    2 Then data is sent to an intermediate function 
    from this statement in the function above:   command = new StandLdapCommand(input);
         public StandLdapCommand(InputStream     in)     throws IOException
              message     =     LDAPMessage.receive(in);
              analyze();
    Then finally, the read function where it hangs at  "int tag = (int)din.readByte(); "
    public static LDAPMessage receive(InputStream is) throws IOException
        *  LDAP Message Format =
        *      1.  LBER_SEQUENCE                           --  1 byte
        *      2.  Length                                  --  variable length     = 3 + 4 + 5 ....
        *      3.  ID                                      --  variable length
        *      4.  LDAP_REQ_msg                            --  1 byte
        *      5.  Message specific structure              --  variable length
        DataInputStream din = new DataInputStream(is);
        int tag = public static LDAPMessage receive(InputStream is) throws IOException
        *  LDAP Message Format =
        *      1.  LBER_SEQUENCE                           --  1 byte
        *      2.  Length                                  --  variable length     = 3 + 4 + 5 ....
        *      3.  ID                                      --  variable length
        *      4.  LDAP_REQ_msg                            --  1 byte
        *      5.  Message specific structure              --  variable length
        DataInputStream din = new DataInputStream(is);
           int tag = (int)din.readByte();      // sequence tag// sequence tag
        ...

    I suspect you are actually getting an Exception and not tracing the cause properly and then doing a sleep and then getting another Exception. Never ever catch an exception without tracing what it actually is somewhere.
    Also I don't know what the sleep is supposed to be for. You will block in readByte() until something comes in, and that should be enough yielding for anybody. The sleep is just literally a waste of time.

  • Socket data send-receive problem

    I hv J2ME client application and vb.net socket server application communicating over sockets. Client connection is made successfully but cant send data to server. When I exit from client application then server application receives those data. (application works pretty fine on emulator but on real device gives above problem)
    WTK Sample application gives same problem when I tried.
    Any idea abt this problem ? gr8 ful if u have some working code.

    yeah your idea works..
    I hv used flush() method and now it works (Live on Nokia E62) nicely.
    But to keep connection opened during whole application I dont close InputStream/OutputStream connection. I close it only at EXIT of application so that I get optimum performace in transactions.

  • File transfer using non-blocking sockets - data mysteriously  vanish

    Hello,
    I need to pass some big amount of data using sockets. I my appliaction I have noticed that sometimes I didn't get all bytes. To check it out I wrote simple client + server to figure out what is happening. So:
    - I have a sender and receiver application
    - I'm trying to transfer 5MB text file.
    - On receiver side, output file is never larget than 3MB
    - If I put some timeout on sender side (1ms timeout between write operations) everything works fine.
    Could someone tell me what I do wrong? Why data disappears and when? The same file transfered using old sockets goes always fine...
    Thanks in advance!
    Here is complete source for receiver and sender:
    RECEIVER:
    import java.io.FileOutputStream;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Iterator;
    public class ReceiverA {
          * @param args
         public static void main(String[] args) {
              String outputFile = "c:\\outputA.txt", host = "127.0.0.1";          
              int port = 8001, bufferSize = 10240;
              try {
                   ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
                   Selector selector = Selector.open();
                   ServerSocketChannel server = ServerSocketChannel.open();
                   server.configureBlocking(false);
                   server.socket().bind(new InetSocketAddress(host, port));
                   server.register(selector, SelectionKey.OP_ACCEPT);
                   System.out.println("Server started");
                   while(true)
                        selector.select();
                        Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                        while (iterator.hasNext()) {
                             SelectionKey key = (SelectionKey) iterator.next();
                             iterator.remove();
                             if (key.isAcceptable()) {                              
                                  SocketChannel client = server.accept();
                                  client.configureBlocking(false);
                                  client.register(selector, SelectionKey.OP_READ);                              
                                  continue;
                             SocketChannel channel = (SocketChannel) key.channel();
                             int counter = 1;
                             if ( key.isReadable() ) {
                                  FileOutputStream os = new FileOutputStream(outputFile);
                                  int res;
                                  do
                                       buffer.clear();
                                       res = channel.read(buffer);
                                       counter += res;
                                       System.out.println(res);
                                       buffer.flip();
                                       os.write(buffer.array(), 0, buffer.limit());
                                  while( res >= 0 );          
                                  channel.close();
                                  os.close();          
                                  System.out.println("Receiver: " + counter);
                                  return;
              } catch (Exception e) {
                   e.printStackTrace();
    }SENDER:
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.SocketChannel;
    import java.util.Iterator;
    public class SenderA {
         public static void main(String[] args) {
              String inputFile = "c:\\inputA.txt" , host = "127.0.0.1";          
              int port = 8001, bufferSize = 10240;
              try {
                   ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
                   byte[] byteArr = new byte[buffer.capacity()];
                   Selector selector = Selector.open();
                   SocketChannel connectionClient = SocketChannel.open();     
                   connectionClient.configureBlocking(false);
                   connectionClient.connect(new InetSocketAddress(host, port));
                   connectionClient.register(selector, SelectionKey.OP_CONNECT);
                   while(true) {
                        selector.select();
                        Iterator<SelectionKey> iterator = selector.selectedKeys()
                                  .iterator();
                        while (iterator.hasNext()) {
                             SelectionKey key = (SelectionKey) iterator.next();
                             iterator.remove();
                             SocketChannel client = (SocketChannel) key.channel();
                             if (key.isConnectable()) {
                                  if (client.isConnectionPending()) {
                                       System.out.println("Trying to finish connection");
                                       try {
                                            client.finishConnect();
                                       } catch (IOException e) {
                                            e.printStackTrace();
                                  client.register(selector, SelectionKey.OP_WRITE);
                                  continue;
                             if(key.isWritable()) {
                                  FileInputStream is = new FileInputStream(inputFile);
                                  int res;
                                  int counter = 0;
                                  do
                                       buffer.clear();
                                       res = is.read(byteArr, 0, byteArr.length);                                   
                                       System.out.println(res);
                                       if ( res == -1 ) break;
                                       counter += res;
                                       buffer.put(byteArr, 0, Math.min(res, buffer.limit()));
                                       buffer.flip();
                                       client.write(buffer);
                                        * When I remove comment file transfer goes OK!
                                       //Thread.sleep(1);
                                  while( res != -1 );          
                                  client.close();
                                  is.close();          
                                  System.out.println("Receiver: " + counter);
                                  return;
              catch(Exception e) {
                   e.printStackTrace();
    }

    There are at least two problems here.
    A. In the receiver you should test for -1 from the read() immediately, rather than continue with the loop and try to write -1 bytes to the file.
    B. In the sender you are ignoring the return value of client.write(), which can be anything from 0 to the buffer length. If you get 0 you should wait for another OP_WRITE to trigger; if you get a 'short write' you need to retry it until you've got nothing left to write from the current buffer, before you read any more data. This is where the data is vanishing.

  • HTTP Server read available socket data?

    I am trying to read data from my http client, and it appears that nothing is available at the time i call input.available() UNLESS I sleep for 100ms before I grab the data. This is a really bad hack, is there a better way to do this??
            // create a byte array to hold input
            thePanelThread.sleep(100);
            byte[] data = new byte [ input.available() ];
            socket.getInputStream().read( data );

    You know how many bytes are read, there is no need to guess it:
    public int read(byte[] b)
             throws IOException
        Reads some number of bytes from the input stream and stores them into the buffer array b.
    The number of bytes actually read is returned as an integer.
    This method blocks until input data is available, end of file is detected, or an exception is thrown.
    */

  • Binary file transfert over socket : data corupted !

    Hi everyone,
    I am trying to transfert binary files over Socket (java.net) and I get corrupted data... I know the data I write in the Socket is ok and I wonder why I don't get it right at the end.
    Does anyone know about it ?

    Ok i have re-written it without the Packet class and it know works...
    I give my code in case someone would be interested.
    ENCODER :
    public class Encoder {
         // the file to send
         private File file;
          * Constructor of the Encoder class
          * @param path the path to the file to send
         public Encoder(String path){
              this.file = new File(path);
              this.encodeFile();
          * This method contains the connection an file tranfert code
         private void encodeFile(){
              try {
                   // opening file reading stream
                   FileInputStream fis = new FileInputStream(this.file);
                   // connection...
                   Socket sock = new Socket("127.0.0.1", 5698);
                   // opening an output stream
                   BufferedOutputStream bos = new BufferedOutputStream(sock.getOutputStream(), 1024);
                   // start time
                   long start = System.currentTimeMillis();
                   // setting up the buffer
                   byte[] buffer = new byte[1024];
                   /* ---- File Transfert Loop ---- */
                   // reading for the first time
                   int read = fis.read(buffer);
                   while (read > 0){
                        bos.write(buffer);
                        bos.flush();
                        read = fis.read(buffer);
                   /* ----End Of File Transfert ---- */
                   // end time
                   long end = System.currentTimeMillis();
                   // closing streams and connection
                   fis.close();
                   bos.close();
                   sock.close();
                   // display file transfert duration
                   System.out.println("Completed in :" + (end - start) + " ms !");
              } catch (IOException e) {
                   e.printStackTrace();
    DECODER :
    public class Decoder {
         private File destFile;
         public Decoder(String path){
              try {
                   // setting up destination file
                   this.destFile = new File(path);
                   // setting up file writting stream
                   FileOutputStream fos = new FileOutputStream(this.destFile);
                   // setting up connection server
                   ServerSocket serv = new ServerSocket(5698);
                   // accepting client connection request
                   Socket sock = serv.accept();
                   // setting up reading stream for the connection
                   BufferedInputStream bis = new BufferedInputStream(sock.getInputStream());
                   // setting up byte buffer
                   byte[] buffer = new byte[1024];
                   // first reading
                   int read = bis.read(buffer);
                   while (read != -1){
                        // writting buffer content into file
                        for (int i=0; i < read; i++){
                             fos.write(buffer);
                        // reading next bytes
                        read = bis.read(buffer);
                   //closing streams
                   fos.close();
                   bis.close();
              } catch (IOException e) {
                   e.printStackTrace();

  • Socket data progress on "write"

    Hi,
    I have a problem with the Socket determining how much data is already written/sent through a socket connection on Flex (not AIR).
    What I am doing is: first loading an image using the FileReference.load() function and later sending the byteArray to a RESTful server backend using HTTP_PUT. Its working fine that way, but I am now missing a way to figure out how far the "upload" is going.
    The Socket is done as followed:
    request.writeUTFBytes(header); // header is String
    request.writeBytes(data, 0, data.length); // data is the image-ByteArray
    socket.writeBytes(request);
    socket.flush();
    On socket.flush() the data is written to the outputStream, but I don't have a clue to see how much bytes are already written.
    I know I could use the FileReference.upload() function which provides a ProgressEvent, but I can't do the upload via HTTP_POST.
    Does anyone have a clue how to get a progress on a socket write?
    - Daniel

    Yes, this will install entire DataSocket software. This includes: DataSocket Server, Manager, and Help.
    Zvezdana S.

  • Data Bursts: Are socket data overwritten??

    Hi, i am using a binary socket to read some (text) data.
    When I sent only a few data everything seems ok.
    When I sent bursts of data, I seem to be losing the top part
    of the my data.
    Is that possible?
    I am attaching some of code I am using (adapted from the
    Telnet client of adobe source).
    Is the data burst the problem, or am I not reading it
    correctly?
    thanks
    socket = new Socket();
    socket.connect(serverURL, portNumber);
    socket.addEventListener(ProgressEvent.SOCKET_DATA,
    dataHandler);
    private function dataHandler(event:ProgressEvent):void {
    var n:int = socket.bytesAvailable;
    var bArray:ByteArray = new ByteArray();
    socket.readBytes(bArray, 0, n);
    etc...

    I'm working on the same issues myself, but it would seem that
    since your addEventListener() is after your connect, you might miss
    something sometimes.
    My issue, I cannot get any data to pass. As soon as I get
    connected I get disconnected. Even though my server code is still
    active, and running everything on localhost.

  • Recieving Socket Data Question

    How can i recieve a large amout of Data from a socket.getInputStream()??
    I use DataInputStream as my Main Constructor...

    One of the big problems with using Sockets is you have
    to know a lot about the data you are receiving just to
    get the commication to work.
    One thing you might do is check to make sure the
    server application is flush()'ing its output stream
    when it writes to it.
    If you know that your data will 4096 bytes, you can do
    something like this:
    ByteArrayInputStream in = new ByteArrayInputStream(
    socket.getInputStream());hhhmmm.... i think there an error here... on constructing ByteArrayInputStream
    ByteArrayInputStream(byte[] buf); it doesnt seems to recognise inputstream!!! need advice..
    byte[] data = new byte[4096];
    int bytesRead = in.read( data, 0, 4096 );

  • Socket data to sound

    Hi all,
    I am getting data from shoutcast server using socket object.
    How I can pass the data to sound object so that i can play
    Is there any way to play the data from the shoutcast.
    Thanks in advance
    Please help me.

    Have you looked at the shipping examples? There's one called Continuous Sound Output that uses the Basic Function Generator to create a waveform and SO Write to output it on the sound card.

  • How do you transfer different types of data through a data socket (data types: clusters, images and boolean states)

    I am attempting to transfer positional data (on a predetermined route) overlaid on a map and indications of boolean output states to secondary computer through datasocket (LabVIEW 8.6, Datasocket 4.5).  Is it required to compress all these parts into an array which is bundled and transmitted to the secondary computer which then would have to be unbundled and separated out of the array?  Is this the only option, or are there other methods? Also how would we go about these methods?

    Hi Maruti,
    It seems like the way you described would be the way to do it unless you wanted to pass all the data seperately.
    Check out this KB relating to passing clusters through datasocket: http://digital.ni.com/public.nsf/allkb/1085057DB6F930058625672400646805?OpenDocument
    Kind Regards,
    Owen.S
    Applications Engineer
    National Instruments

  • Losing socket data

    If I quickly recieve many messages from a host I start losing some of the messages (I don't see them in the buffer). Below is the code snippet. Am I not reading the buffer correctly? If I substitute "readFully" instead, then I lose ALL incoming messages.
    static byte[] BCreadmessagearray = new byte[2221];
    // Create a socket to the B2 server
    msgcmdsocket = new Socket(InetAddress.getByName ProcessProperties.getB2TcpIpAddress()),5080);
    // Create input stream for the socket
    msgcmdinput = new DataInputStream (new BufferedInputStream(msgcmdsocket.getInputStream()));
    final class ButtonCommand implements Runnable
    public void run()
    while(true)
    try
    //read the socket buffer and put it into BCreadmessagearray buffer.
    int msglength = msgcmdinput.read(BCreadmessagearray,0,BCreadmessagearray.length);

    Interesting, it almost sounds like we have the same problem... my implementation is quite similiar to yours, however I'll only lose a byte here and there... never a huge part of the message or anything.
    I just posted my thread today...
    http://forum.java.sun.com/thread.jsp?forum=4&thread=347250&tstart=0&trange=15
    let me know if you come up with a solution to solve your problem.
    Thanks,
    .kim

  • Data written to socket getting lost?  or perhaps out of order?

    I'm trying to fix a bug in Flashmog. The bug is described in more detail here.
    Basically what is happening is that my Flash client claims that it is calling functions that don't arrive at the server -- or they arrive, but the socket data is out of order and therefore is garbled.  I've stared at the source code for hours and tried a lot of trial-and-error type stuff and added trace statements to see if I can find the problem and I'm not having any luck.
    In particular, there's class I have called RPCSocket that extends the AS3 Socket class so I can serialize data structures before sending them across a socket.  At one point, this RPCSocket class calls super.writeBytes and super.Flush.  It is the point at which I send all data out of my client. The data is binary data in AMF3 format.
              public function executeRPC(serviceName:String, methodName:String, methodParams:Array):void {
                   if (!this.connected) {
                        log.write('RPCSocket.executeRPC failed. ' + methodName + ' attempted on service ' + serviceName + ' while not connected', Log.HIGH);
                        throw new Error('RPCSocket.executeRPC failed. ' + methodName + ' attempted on service ' + serviceName + ' while not connected.');
                        return;
                   var rpc:Array = new Array();
                   rpc[0] = serviceName;
                   rpc[1] = methodName;
                   rpc[2] = methodParams;
                   var serializedRPC:ByteArray = serialize(rpc);
                   if (!serializedRPC) {
                        log.write('RPCSocket.executeRPC failed.  Serialization failed for method ' + methodName + ' on service ' + serviceName, Log.HIGH);
                        dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR, false, false, 'RPCSocket.executeRPC failed.  Serialization failed for method ' + methodName + ' on service ' + serviceName));
                   super.writeUnsignedInt(serializedRPC.length);
                   super.writeBytes(serializedRPC);
                   super.flush();
              } // executeRPC
    Can someone recommend a way for me to store without corruption, conversion, or filtering or translation of any kind *all* of the information sent across this socket? I'd like to write it to a file.  I'm guessing that keep a global ByteArray var and storing the info there might work, but I'm wondering how I might get the contents of that ByteArray into a file so I can inspect it.
    Also, I'm wondering if I might be able to inspect what flash actually sends out on the socket?  I have a sneaking suspicion that data I supply to super.writeBytes may be sent out of order or may not actually get sent across the socket.  This bug I'm talking about only seems to happen under high-stress situations when I'm sending dozens of messages per second across this one socket.

    oops...forgot link to bug description: http://flashmog.net/community/viewtopic.php?f=5&t=549

Maybe you are looking for

  • Posting line item currency different than header curency

    Hi ALL, Is there any way in SAP where I can post line items in a different currency that header currency? for e.g.  Doc header currency - USD Line item 1 Dr A/c -  Amount in USD Line item 2 Cr A/C - Amount in CAD Can you please let me know if it is p

  • Generating WSDL using Integration Directory (Tools-Define Web Service)

    Hi, I have tested the "Define Web Service" tool in Integration Directory - some question; 1) It's a little bit frustrating to change the URL after clicking the "Propose URL" button - is it possible to configure somewhere the right URL http://<srv>:<p

  • Looking for simple PC x86 mono/vga driver

    Is source code available for a simple Monocrome or VGA driver in Solaris 8 on a PC x86 platform

  • Every time isync resets causes loss of data

    I have two computers. 1 running 10.4.11, 1 runnin 10.5.5 I frequently sync the PB with 10.5.5. However when I sinc my PB running 10.4.11 the message appeared that .mac was reset and I am asked to replace the data on my computer or on .mac The same wi

  • Frames in Fireworks

    Hi, thank you for reading. I'm trying to work out the best way to have a small frame that will be place in/over a fireworks graphic that is essentially my index page. I want it to be a scrollable frame with news updates on it. It doesn't even have to