problem while reading from a socket in java
Actually i'm trying to send some data from "C" socket to java socket at the frontend . at the front end i've used buffered byte stream reader to read from the socket ,when i tried to read the message from java socket ,the message is corrupted , i mean if i try to send "AABBCC" from a socket created in "C" language to java , java socket intrepret the bytes as " CC AA CC BB CE DF" .However it should receive the same as i sent from the socket at "C" . I'm still not able to find the exact reason why java socket is reading wrong bytes . is there any specific reader i should use to read from java socket or wht will be the proposed solution . or i simply give a JNI call to C Code only to read the data at "C" socket .. ?
The Java socket is fine.It doesn't matter what language the sending program was written in. All that matters is that the sending program and receiving program agree on the format and meaning of the bytes.
jverda at 2007-7-12 22:07:45 >

i do not get what are you saying like in C i just send the length of bytes to be read and the buffer "AABBCC" but socket at java receives it wrong and if i send any value less then "A" it works fin e i mean if you send
0x1234 it will be received correctly but thye moment u send greater values data gets corrupted itself.
> i do not get what are you saying like in C i just
> send the length of bytes to be read and the buffer
> "AABBCC" but socket at java receives it wrong
No, it doesn't receive it wrong. You're processing it incorrectly. Or you're not sending what you think you are.
Since I can't see what data is being sent, or how, or how you're processing it, I can't give you any more deatails about what you're doing wrong.
jverda at 2007-7-12 22:07:45 >

> if you send > 0x1234 it will be received correctly but thye moment> u send greater values data gets corrupted itself.Highly unlikely.
jverda at 2007-7-12 22:07:45 >

what i want to say is if you write "AABBCC" on C socket java does not read the data correctly , this happens to me earlier also when i tried to receive using inputstreamreader then i shifted to bytestream to read in bytes . But if u send values like 01 , 02 ,03 the data is correctly received at Java socket . How can the processing be wrong i mean i'm just converting the received bytes into hex at the front end and displaying them.any value if written in terms of AA BB i mean greater then 79 is processed wrong . is there any alternative way to read from java socket and it is sure that data sent from C socket is 100 % correct
> what i want to say is if you write "AABBCC" on C
> socket java does not read the data correctly
It does if you write your code correctly.
And it DOES NOT MATTER what language the sender is written in. The bytes that are sent will be the bytes that are received.
The problem is not the socket. It is the format of the data.
> How can the processing be wrong
I don't know because I haven't seen the sending code, the bytes sent, or the receiving code.
> is there any alternative way to read from java socket
> and it is sure that data sent from C socket is 100 %
> correct
Okay, I give up. You win. Java sockets know what language the sender was written in, and Java doesn't like C, so it refuses to read the bytes properly. It is impossible to send data from a C program to a Java program. Give up now.
jverda at 2007-7-12 22:07:45 >

Here, do this on the receiving end. It will print out the hex values of each byte received, and those ARE the same bytes that were sent.
InputStream ins = sock.getInputStream();
int byteRead;
while((byteRead = ins.read()) != -1) {
System.out.println(Integer.toHexString(byteRead & 0xFF));
}
jverda at 2007-7-12 22:07:45 >

i'll try this and let you know if problem still occurs
I'm getting bytes that spell out:s o u r c e = C p r o g r a m m e
i didn't get you sorry...............
> i'll try this and let you know if problem still occursThat code is not to fix the problem. It's to show you what bytes the socket is receiving. Then you can figure out whether it's that you're processing them incorrectly, or that the sender isn't sending what you think it
jverda at 2007-7-12 22:07:45 >
