Problem receiving data from a Socket.

Started by Melssj5, May 15, 2007, 04:51:38 PM

Previous topic - Next topic

Melssj5

Hi, I am having a problem at receiving some data via a network.

I use the DataInputStream to read the data from a Socket. I always used to ask first for the number of bytes to receive, then create a byte array and store the data on there. The problem is that I need to read the data without asking the number of bytes. I tried reading until getting the -1 from the read method that means the end of the transmition, but the -1 is never read until  the client closes the stream. That would be easy to solve just asking the client to close the stream, but the problem is that the client WONT close any stream because is not programmed on java but on another language that doesnt allow to close the streams.

What I am doing now is this:

1.- creating a byte array of 197 slots.

2.- read on it until the number of bytes read is different from the array length.

3.- store the parts on a file, if the number of bytes is different from the array length then add the new part to the file and finish.

this works but I am afraid that is the number of bytes to read is multiple of the array length then the server will blocked until getting new data. and thats a problem because I need to answer inmediatly with another things.
Nada por ahora

EgonOlsen

Maybe it's possible to define a terminator token (like 0), that indicates that the current sub-stream has ended? Or you may have a look at the nio-package that should provide non-blocking io support. However, i've never used it for this...it looked quite strange to me at first glance and i haven't spend a second.

Melssj5

mmmm the problem is that I cannot ask the client program for any thing like adding another byte for parity, adding token bytes, asking for the number of bytes nor closing the socket at the end of the transmition.

I will check the nio package to check it but is very usefull to  read on that way because not always will be  java connecting java, so it shouldnt ask for streaming close for reaching the end of he transmition. Becausse thats very "java like".

My client is on PocketBuilder so the doenst exist the concept of streams. They use use Socket send and socket receive and nothing else. only the whole socket can be closed.
Nada por ahora

raft

#3
-1 is a special value meaning stream is closed.

so, you should read bytes from stream until they form a reasonable data. for example a blocking method which returns a Data type is something like

ByteArrayOutputStream bos = new ByteArrayOutputStream();
int offset = 0;
Data readFromStream(InputStream in) throws IOException {
    byte[] buffer = new byte[buffersize];
    Data data = null;
    int read = -1;
    while (data == null && read != -1) {
       read = in.read(buffer);
       if (read != -1)
           bos.write(buffer, 0, read);

       // try to construct something meaningful from content
       data = constructData(bos);

       // constructData should use offset, and regularly delete bytes before offset
    }
    return data; // or you may want to throw an exception if data is null
}


edit: added offset

r a f t


Melssj5

The problem is that I cannot wait for having a return of -1 because the client is never closing the transmition. The client program is made on pocket builder and I asked the programmer (actually my boss) to change the client to close the output stream or something and he told me that is not posible to do that on pocket builder.

now I asked him to send me the number of bytes but he is sending on ansi and when I receive then each number came in 2 bytes or something like that and I cant work well with that.

1.- Is there anyway to convert ansi to unicode o something?

2.- If not waiting for having a -1 as a returned value. Is there any other way to recognize the transmition is over. I must receive only one group of bytes from each client.
Nada por ahora

hytparadisee

Quote1.- Is there anyway to convert ansi to unicode o something?

No idea how to do that exactly. You might go in the direction of CharSet or Encoding in java??
Today I finally found a problem to my soluuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuution

raft

i dont understand why you are trying to read till -1. as i suggested you should read some bytes and then construct your data type from them. i dont know what type of data is sent so i cant say how you should construct your data. it's your responsibility. each protocol has a kind of mechanism which determines packet size. a terminator byte for instance, or packet size is sent in header (first bytes)

if you dont need to create a data type out of bytes (which means you are sort of a carrier, transferring bytes somewhere else, to a file for example) just read as much as available bytes (InputStream.available())

btw, there is flaw in the code piece above, i'll fix it now

raft

i re-read the first, i missed the point. sorry, forget about my posts  :-[

eye1

I use appache XML-RPC http://ws.apache.org/xmlrpc/ to transfer data between server and client

It is very simple to use.