Bookmark Topic Watch Topic
  • New Topic

Problems with read and readLine  RSS feed

Gus Spain
Posts: 17
  • Mark post as helpful
  • send pies
  • 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];, 0, c);
String d = new String (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!
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi Gus,

you probably want to use one of Your
stream comming from the C program is not a DataInputStream
therefore java won't be able to read it as DataInputStream.
Hint: If you sent 4 bytes (an int), you should read 4 bytes
(an int) on the other side.

  • Java String is not C char*.
  • Data*Stream is not a stream of data being sent/read to/from the stream.
  • Not all computers out there are little endians.

  • I am not very sure about this:
    Shouldn't it be rather something like:

    That's about it. Try the InputStream, try some reads, just to
    see what data are out there, and then you will have a better
    notion. But you should remember that if you write your C program
    like that it won't be portable. It is not only about endianity
    but also that the string might have an encoding and Java does not
    know which.

    Best regards,
    Ernest Friedman-Hill
    author and iconoclast
    Posts: 24217
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Gus --

    Please don't post the same question to multiple forums: it just wastes people's time. I'm closing this copy and pointing further discussion to this one, which also contains a useful answer. Note that the mistakes I mention, and the mistakes Petr mentions, are different, so this code has all sorts of problems.
      Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!