0

I am using sockets and implementing a Java server and a c++ client. The client is sending server a class. Java server receives it as array of bytes but after that it is not converting it back into the class members right. I have looked a lot but I guess I don't really know what to look for. Here are the parts of the code, complete code is quite big

Client C++

IntVal temp; 
Set(temp,values);


int tempp;
tempp=send(s_id,&temp,sizeof(temp),0);
if(tempp==-1)
{
    printf("Sending Error \n");
}

"IntVal Class" has 7 to 8 float values and no member function.

Server Java

public static void main(String[] args) throws IOException {
        // TODO code application logic here
         Values values=new Values();
         gui display=new gui();
         display.setVisible(true);
        ServerSocket Sock=new ServerSocket(9090);
        try{
            while(true){
                System.out.println("Waiting");
                Socket socket=Sock.accept();

                    System.out.println("Connected");
                  InputStream ins=socket.getInputStream();
                    InputStreamReader insr= new InputStreamReader(ins);
                    BufferedReader br=new BufferedReader(insr);

                    byte[]Array=br.readLine().getBytes("UTF-8");
                   // values.SetValues(Array);
                    //display.SetValues(values);

                    values.tWidth=Array[0];
                    values.waterLevel=Array[4];
                    values.camHeight=Array[8];
                    values.camViewAngleY=Array[12];
                    values.camViewAngleX=Array[16];
                    values.distFromCamBank=Array[20];
                    values.distTwoPoints=Array[24];
                    values.AvgVelocity=Array[28];
                    values.crossSecArea=Array[32];
                    values.Flow=Array[36];
                    values.camTiltAngle=Array[40];
                    values.aboveWater=Array[44];
                    System.out.println(values.tWidth);
                    System.out.println(values.waterLevel);
                    socket.close();
                }

        }
        finally{
            Sock.close();
        }


    }
}

Here Class Values is equivalent to IntVal class in c++ client. As you can see I am only checking with first two values, They display Garbage values. Please point me in right direction that what is wrong here.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • 2
    It's not obvious where the C code is getting its data, or how this is supposed to work. But...first, check that the bytes being sent are really a valid Java class - copy them to a file on disk and write something which will try to load the class - i.e. take the socket code completely out of the picture and make sure your data is good. If it is, check that both ends of the wire are using the same endianness (low bit first or high bit first). – Tim Boudreau Sep 04 '13 at 21:04
  • Its just a simple c++ class...with only primitive data types..i.e.float. and the data is being calculated in the rest of the code(which is not posted here )... – HassanShafiq Sep 04 '13 at 21:15
  • 1
    Since you are sending byte[] and you want to turn it into a byte[], why are you reading it as text and not binary? The InputStream is binary already. Also you appear to be expecting 4 byte int values but you are only using the top byte. Or is it `float`, I am confused about what you are trying to do. – Peter Lawrey Sep 04 '13 at 21:52

1 Answers1

1

You are encoding and decoding the bytes yourself so it's you who is to blame for this, not Java. Use an InputStream, not a Reader, and process the bytes directly.

user207421
  • 305,947
  • 44
  • 307
  • 483