Author Topic: Problem receiving data from a Socket.  (Read 3545 times)

Offline Melssj5

  • double
  • *****
  • Posts: 715
    • View Profile
Problem receiving data from a Socket.
« on: May 15, 2007, 04:51:38 pm »
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11775
    • View Profile
    • http://www.jpct.net
Re: Problem receiving data from a Socket.
« Reply #1 on: May 15, 2007, 05:26:35 pm »
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.

Offline Melssj5

  • double
  • *****
  • Posts: 715
    • View Profile
Re: Problem receiving data from a Socket.
« Reply #2 on: May 15, 2007, 05:35:18 pm »
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

Offline raft

  • quad
  • ******
  • Posts: 1971
    • View Profile
    • http://www.aptalkarga.com
Re: Problem receiving data from a Socket.
« Reply #3 on: May 16, 2007, 10:15:04 am »
-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

Code: [Select]
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

« Last Edit: May 16, 2007, 05:55:03 pm by raft »

Offline Melssj5

  • double
  • *****
  • Posts: 715
    • View Profile
Re: Problem receiving data from a Socket.
« Reply #4 on: May 16, 2007, 05:00:58 pm »
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

Offline hytparadisee

  • int
  • **
  • Posts: 86
    • View Profile
    • http://peterhi.com
Re: Problem receiving data from a Socket.
« Reply #5 on: May 16, 2007, 05:11:10 pm »
Quote
1.- 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

Offline raft

  • quad
  • ******
  • Posts: 1971
    • View Profile
    • http://www.aptalkarga.com
Re: Problem receiving data from a Socket.
« Reply #6 on: May 16, 2007, 05:49:56 pm »
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

Offline raft

  • quad
  • ******
  • Posts: 1971
    • View Profile
    • http://www.aptalkarga.com
Re: Problem receiving data from a Socket.
« Reply #7 on: May 16, 2007, 06:00:27 pm »
i re-read the first, i missed the point. sorry, forget about my posts  :-[

Offline eye1

  • int
  • **
  • Posts: 68
    • View Profile
Re: Problem receiving data from a Socket.
« Reply #8 on: May 17, 2007, 01:04:18 pm »
I use appache XML-RPC http://ws.apache.org/xmlrpc/ to transfer data between server and client

It is very simple to use.