• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with read and readLine  RSS feed

Gus Spain
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i�ve been trying for several days to comunicate a C server and a Java client, and i got stuck while receiving the data in Java. First i�ve got to say that i�m working under windows using visual studio.
The server seems to work fine, it prints well all the messages and sends the information like this

char *result;
result = malloc(sizeof(char)*1024);
result = Getdata(Buffer,Buffer2, &db);
int bx = strlen(result);
retval = send(msgsock,(char *)bx,sizeof(int),0);
retval = send(msgsock,result,sizeof(result),0);

and this is what i do in the Java part to receive the data

DataInputStream in = new DataInputStream(socket.getInputStream());
int c = in.readInt() - 1; //it has a -1 because of the \0
System.out.println ("C IS " + c);
byte [] au = null;
au = new byte[c];
in.read(au, 0, c);
String d = new String (au);
in.read(au,0,1); // now we read the \0

but then i always get this message

C IS 1885696626

I also tried using this

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str;
if ((str = in.readLine())!=null)
System.out.println("Recibido resultado str:"+str);

the problem is that i have to receive two messages, and while the first one arrives fine, the second one is just junk, the typical msdos funny symbols.

Where am I spoiling it? What should i do in order to read well? Or what is the best way to get the data from the socket?
I�m quite desperate , this is my last program for the university in order to get my degree!
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gus,

Consider what you're doing here

when c is a number like 1885696626. How big is that array in memory? At least 1885696626 bytes! According to my calculations that's 1.88 gigabytes, so that's where the OutOfMemory exception is coming from. You need to think about reading in a reasonable number of bytes e.g. 4096 at a time in a loop I think.
Ernest Friedman-Hill
author and iconoclast
Posts: 24217
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"result" is a char*; sizeof(result) is likely to be 4. It's not the length of the string, in any case, but the size of the pointer. Don't you want "bx" instead of sizeof(result) here?

If you use "bx", then remove the code that reads the \0 on the Java end, because you won't be sending it (and there's no reason to, of course.)

Note, also, that read(array, 0, count) isn't guaranteed to read "count" bytes; it will return the number actually read, which may be less. You need to call read in a loop, adjusting the parameters, until you get all the data you expect.
[ September 20, 2004: Message edited by: Ernest Friedman-Hill ]
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The C part to send the data:

The JAVA part to receive the data:

And you must add this reverse() method in the JAVA side:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!