Results 1 to 12 of 12
- 07-01-2010, 11:25 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
sending byte from server to C client
hi,
must be easy but I do not know how:
I need to send 4 + 1 bytes of integer from the java socket server to the C client. how do i create it:
The structure of the data to be send is as follows
I have the following codeJava Code:typedef struct resp { u_int32 res; u_int8 successfull; }
I do not know how to go further. I triedJava Code:BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream()); DataOutputStream out =new DataOutputStream (bos);
out.writeInt(..)
but the client does not get it in correct format.
regards,
rnv
- 07-01-2010, 01:19 PM #2
Can you define what the C program needs to receive byte by byte vs showing C code?
- 07-01-2010, 01:52 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
sending byte from server to C client
The problem is I do not have the C code. the only thing I know is that it expects the data in following structure in byte stream.
Java Code:typedef struct resp { u_int32 res; u_int8 successfull; }
- 07-01-2010, 02:46 PM #4
What you've posted looks like C code. No use here.
Can you break it down to byte by byte?
For example:
bytes 0-15 is a short integer (LowEndian)
bytes 16-23 is a byte
Given the byte layout of the input record it will be possible to load the bytes into java variables.
- 07-01-2010, 03:40 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Yes the code is in C. I included it to show what I should send.
I should send 4 byte and 1 byte integer. sorry I am new to this byte level coding and do not know what it exactly is (Low endian or whatever).
will really appreciate for an example.
- 07-01-2010, 04:31 PM #6
Put the bytes to be sent in a byte array and use a write() method
- 07-01-2010, 09:37 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Yes.. I understood that but I would like to know how.
I should send 4byte integer and 1byte integer in a byte array.
is the following correct?
In above example i craete the 4 byte integer. first of all i do not know if this is the correct way. the second is how do i now add 1 byte array?Java Code:public void sendmst() { BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); DataOutputStream out =new DataOutputStream (bos); // seq num byte[] seqnum = new byte[4]; seqnum = intToByteArray(1); out.write(seqnum); out.flush(); } public static final byte[] intToByteArray(int value) { return new byte[] { (byte)(value >>> 24), (byte)(value >>> 16), (byte)(value >>> 8), (byte)value}; }
will really appreciate for help.
regards,
rnv
- 07-01-2010, 10:50 PM #8
Yes, I think you've got it.
Here's a reworked version of your code showing how I test it. I use a ByteArray and then print it to see what the code generates:
Java Code:import java.util.*; import java.io.*; public class DataToByte { public static void main(String[] args) { try{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Use this for testing DataOutputStream out =new DataOutputStream (baos); // seq num int intIn = 0x12345678; byte[] seqnum = intTo4ByteArray(intIn); out.write(seqnum); out.write(new byte[]{(byte)0x98}); // add another one out.flush(); byte[] output = baos.toByteArray(); // get what was written // Show what we have - first the array System.out.print("output="); for(int i=0; i < output.length; i++) { System.out.print(Integer.toHexString((output[i] & 0xFF)) + " "); // need AND 0xFF to trim int sign spread } System.out.println(""); // flush // Then the input System.out.println("inInt=" + Integer.toHexString(intIn)); //output=12 34 56 78 98 //inInt=12345678 }catch(Exception x) { x.printStackTrace(); } } public static byte[] intTo4ByteArray(int value) { return new byte[] { (byte)(value >>> 24), (byte)(value >>> 16), (byte)(value >>> 8), (byte)value}; } }
- 07-02-2010, 11:02 AM #9
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Thank you very much Norm.
is this the hex value of the int 12345678? Is it important?Java Code:int intIn = 0x12345678;
I guess using this way the socket client in C can read the what is sent.. correct?
regards,
rnv
- 07-02-2010, 12:59 PM #10
No. I used a hex value on the input so I could easily verify that when i printed what came out of the process was the same as what went into it.
- 07-02-2010, 02:38 PM #11
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
so that means I can simply use
without the hex value and the C client should be able to read it.Java Code:int intIn = 12345678;
please let me know if that is not the case.
also will really appreciate if you can point me to URL or any other tutorial where I can learn more about such things.
thank you very much.
- 07-02-2010, 03:20 PM #12
The 0x12345678 was test only test data for ease of visual verification. If you remove the 0x I have no idea what the hex value of 12345678 is.
My example passed a know value to some methods and observed that that value made it thru the methods ok.
You now need to change the java code to write the output to the C code
Similar Threads
-
sending file from client to server - socket closed error
By forex in forum NetworkingReplies: 3Last Post: 04-05-2010, 02:19 AM -
Sending Text File --- Server-To-Client
By nigamsir in forum NetworkingReplies: 1Last Post: 03-08-2010, 03:45 PM -
Sending a File from Server to Client and saving the file to Clients computer
By al_Marshy_1981 in forum NetworkingReplies: 8Last Post: 02-18-2010, 12:54 PM -
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 -
sending jar files from client to server?
By gobinathm in forum New To JavaReplies: 2Last Post: 11-13-2007, 05:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks