Results 1 to 12 of 12
  1. #1
    rnvrnv is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Unhappy 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
    Java Code:
    typedef struct resp
    {
        u_int32 res;
        u_int8 successfull;
    }
    I have the following code

    Java Code:
    BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());
    DataOutputStream out =new DataOutputStream (bos);
    I do not know how to go further. I tried

    out.writeInt(..)

    but the client does not get it in correct format.

    regards,
    rnv

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    Can you define what the C program needs to receive byte by byte vs showing C code?

  3. #3
    rnvrnv is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default 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;
    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    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.

  5. #5
    rnvrnv is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    Put the bytes to be sent in a byte array and use a write() method

  7. #7
    rnvrnv is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    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?
    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};
    }
    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?

    will really appreciate for help.
    regards,
    rnv

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    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};
       }
    }

  9. #9
    rnvrnv is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Thank you very much Norm.

    Java Code:
    int intIn = 0x12345678;
    is this the hex value of the int 12345678? Is it important?

    I guess using this way the socket client in C can read the what is sent.. correct?

    regards,
    rnv

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    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.

  11. #11
    rnvrnv is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    so that means I can simply use

    Java Code:
    int intIn = 12345678;
    without the hex value and the C client should be able to read it.

    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.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    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

  1. Replies: 3
    Last Post: 04-05-2010, 02:19 AM
  2. Sending Text File --- Server-To-Client
    By nigamsir in forum Networking
    Replies: 1
    Last Post: 03-08-2010, 03:45 PM
  3. Replies: 8
    Last Post: 02-18-2010, 12:54 PM
  4. Replies: 1
    Last Post: 02-10-2009, 09:16 AM
  5. sending jar files from client to server?
    By gobinathm in forum New To Java
    Replies: 2
    Last Post: 11-13-2007, 05:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •