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

    Question 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:
    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);
    the problem is i am not able to decode the message. the structure of the message from the client is as follows:
    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
    hope someone can help me.
    regards,
    rnv

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    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.

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

    Default

    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:

    Java Code:
    typedef struct user
    {
        u_int 32 userid 
        u_int 32 cityid
        u_int 8  validuser /* 1-yes 0-no */
    } user _t, * user _p
    the values could be
    123 //user id
    987 //city id
    1 // successfull

    is it a problem of encoding between java (windows) and C (linux) system?

    regards,
    rnv

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    I read something like this:
    What does 000000048110079120521593741290122664 represent?
    Is it hex? 00 00 00 04 81 10 07 91 20 52 15 93 74 12 90 12 26 64 (18 bytes long)
    Sorry I do not have the C code
    What is this? I thought it looked like C code:
    Java Code:
    typedef struct user
    {
        u_int 32 userid 
        u_int 32 cityid
        u_int 8  validuser /* 1-yes 0-no */
    } user _t, * user _p
    is it a problem of encoding between java (windows) and C (linux) system?
    Depends. A byte in java should be the same byte in C. Anything else could be different.

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

    Default

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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    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.

  7. #7
    spygas is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    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

  1. Replies: 0
    Last Post: 04-05-2009, 04:59 PM
  2. Server socket - send image to client
    By Hinty in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 08:39 AM
  3. Replies: 1
    Last Post: 02-10-2009, 10:16 AM
  4. Client Server socket problem - help needed
    By kellaw in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-03-2008, 07:49 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 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
  •