使用套接字将asn1数据从客户端发送到服务器

时间:2014-05-11 21:57:48

标签: java c sockets asn.1

我在C客户端编写了这段代码来获取一个int号,编码并通过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 );
   }

一切都很好,我得到输出

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

但在java服务器中我收到此错误

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

1 个答案:

答案 0 :(得分:1)

问题是您发送了结构(message1)而不是编码缓冲区。 将C程序的最后一部分更改为:

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

期望类似以下字节序列(hexa)30 03 02 01 02