Results 1 to 7 of 7
- 07-01-2010, 06:29 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Java Socket server with C client problem
Hi,
I am new to java socket specially if the client is in C.
The java server gets the connection and gets something from the C client but I am it is not reading the message correctly. the C client sends the data in byte stream which i try to get as follows:
the problem is i am not able to decode the message. the structure of the message from the client is as follows:Java Code:BufferedInputStream bis = new BufferedInputStream(clientSocket .getInputStream()); DataInputStream in =new DataInputStream (bis); byte[] b = new byte[200]; in.read(b); for (int i = 0; i < b.length; i++) { clientmsg+=(int)b[i]; } System.out.println("clientmsg: "+clientmsg);
hope someone can help me.Java Code:typedef struct user { u_int 32 username u_int 32 city u_int 8 validuser /* 1-yes 0-no */ } user _t, * user _p
regards,
rnv
- 07-01-2010, 01:17 PM #2
Can you define the bytes being sent to the Java program byte by byte vs showing C code?
Have you tried debugging the code by printing out the bytes as they are read so you can see what is being received?
What is the type of clientmsg?
Have you tried using the String() constructor to build the String vs the concatenate technique you are using.
- 07-01-2010, 02:01 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Sorry I do not have the C code.
I read something like this:
000000048110079120521593741290122664
But I guess it is wrong. my original post contains how I get this thing but I think I am doing something wrong.
actually I should be getting the following structure:
the values could beJava Code:typedef struct user { u_int 32 userid u_int 32 cityid u_int 8 validuser /* 1-yes 0-no */ } user _t, * user _p
123 //user id
987 //city id
1 // successfull
is it a problem of encoding between java (windows) and C (linux) system?
regards,
rnv
- 07-01-2010, 02:42 PM #4
What does 000000048110079120521593741290122664 represent?I read something like this:
Is it hex? 00 00 00 04 81 10 07 91 20 52 15 93 74 12 90 12 26 64 (18 bytes long)
What is this? I thought it looked like C code:Sorry I do not have the C codeJava Code:typedef struct user { u_int 32 userid u_int 32 cityid u_int 8 validuser /* 1-yes 0-no */ } user _t, * user _pDepends. A byte in java should be the same byte in C. Anything else could be different.is it a problem of encoding between java (windows) and C (linux) system?
- 07-02-2010, 03:00 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Here is how I would like to read message from the C socket client in my Java Socket server. the example shows how to read 4 byte followed by 1 byte int.
to get int from 4byte array I used getIntFromByteArray method. to get 1byte value i simply get the byte.
Is it the correct way?
Java Code:BufferedInputStream bis = new BufferedInputStream(clientSocket.getInputStream()); DataInputStream in = new DataInputStream(bis); // read 4 byte user id byte[] useridByte = new byte[4]; in.read(useridByte); // Bytes are read into //read 1 byte age byte[] ageByte= new byte[1]; in.read(versionbyteByte); // get userid int userid = getIntFromByteArray(useridByte); // get age byte age = ageByte[0];
Java Code:public static int getIntFromByteArray(byte[] b) { int number = 0; for (int i = 0; i < 4; ++i) { number |= (b[3 - i] & 0xff) << (i << 3); } return number; }
- 07-02-2010, 03:16 PM #6
Use the same technique I showed in the other thread. Put the bytes into a ByteArrayInputStream and have your Data stream wrap that. Then run your code and print out the results and compare it the what you put into the BAIS.
- 11-09-2010, 12:47 AM #7
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
Does your C client convert struct to network-byte-order before send()? It's requiered if you are sending structs containing fields larger than char (like int, long, etc). Check this out: htons(), htonl(), ntohs(), ntohl()
Btw, I would recommend you to convert byte array to int in more simple way:
Java Code:public static int getIntFromByteArray(byte[] b) { int number = 0; for (int x = 0; x < b.length; x++) number = (number << 8) | b[x]; return number; }
Similar Threads
-
java.io.EOFException: send vector from client to server through socket
By kathychow in forum New To JavaReplies: 0Last Post: 04-05-2009, 03:59 PM -
Server socket - send image to client
By Hinty in forum NetworkingReplies: 2Last Post: 03-14-2009, 07:39 AM -
Sending a .Doc file from client to a server through socket
By amita_k29 in forum NetworkingReplies: 1Last Post: 02-10-2009, 09:16 AM -
Client Server socket problem - help needed
By kellaw in forum Threads and SynchronizationReplies: 6Last Post: 10-03-2008, 06:49 PM -
Identify Client in Socket Client Server Application
By masadjie in forum NetworkingReplies: 1Last Post: 12-20-2007, 09:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks