Question

I wrote this code in the C client to get an int number , encode it and send it through socket

int clientNumber;
printf("Enter a number :\n  "); 
scanf("%d",&clientNumber);
Message01_t *message1;
message1 = calloc(1, sizeof(Message01_t));
message1->number= clientNumber;
char    buffer[BUFFERSIZE];
size_t  bufferLen = sizeof(buffer); 
der_encode_to_buffer(&asn_DEF_Message01, message1, buffer, bufferLen);
xer_fprint(stdout, &asn_DEF_Message01, message1);

if( send( to_server_socket, &message1, sizeof( message1 ), 0 ) != sizeof( message1) )     {
printf( "socket write failed");
 exit( -1 );
   }

All look fine , I got for output

Enter a number :
  2
<Message01>
    <number>2</number>
</Message01>

But in the java server I get this error

received data: B0 41 40 A1 B4 
com.chaosinmotion.asn1.AsnFatalException: 
 >> During decoding, the encoded tag value in the byte array does not match with this object's tag number ! 
    at com.turkcelltech.jac.Sequence.decode(Sequence.java:100)
    at ServiceRequest.run(ServiceRequest.java:30)

this is the server code 
BerInputStream bin = receive(5);
            Message01 msg01 = new Message01();
            msg01.decode(bin);
            printMessage(msg01);
Was it helpful?

Solution

The problem is that you sent the structure (message1) instead of the encoded buffer. Change the last part of the C program to:

if( send( to_server_socket, buffer, bufferLen, 0 ) != bufferLen ) {
    printf( "socket write failed");
    exit( -1 );
}

Expect something like a sequence of the following bytes (hexa) 30 03 02 01 02

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top