You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Michi Mutsuzaki <mi...@yahoo-inc.com> on 2011/04/25 22:52:44 UTC

java: using binary and TFramedTransport

Hello,

I'm having trouble using binary type with TFramedTransport in Java. I'm
using Thrift 0.6. I have a method that takes a binary as an argument:

    i32 myMethod(1:binary blob),

I have a Java client that calls myMethod():

    TSocket socket = new TSocket("localhost", 9090);
    TTransport transport = new TFramedTransport(socket);
    transport.open();
    TProtocol protocol = new TBinaryProtocol(transport);
    MyService.Client client = new MyService.Client(protocol);
    client.myMethod(ByteBuffer.wrap("hello".getBytes()));

The server is started like this:

    TServerTransport trans = new TServerSocket(port);
    TThreadPoolServer.Args args = new TThreadPoolServer.Args(trans);
    args.processor(new MyService.Processor(new MyProcessor()));
    args.transportFactory(new TFramedTransport.Factory());
    args.protocolFactory(new TBinaryProtocol.Factory(true, true));
    TServer server = new TThreadPoolServer(args);
    server.serve();

On the server side, myMethod() receives more than "hello". It looks
something like this:

    ?^A^@^A^@^@^@^KmyMethod^@^@^@^A^K^@^A^@^@^@^Chello^@

It works correctly if I don't use TFramedTransport. Am I misusing
TFramedTransport?

Thanks!
--Michi


Re: java: using binary and TFramedTransport

Posted by Michi Mutsuzaki <mi...@yahoo-inc.com>.
Ah I see. Let me try that.

Thanks!
--Michi

On 4/25/11 2:54 PM, "Bryan Duxbury" <br...@rapleaf.com> wrote:

> No, you are misusing the ByteBuffer. You need to pay attention to
> .position() and .limit() - only the bytes between those two values are part
> of that field.
> 
> On Mon, Apr 25, 2011 at 1:52 PM, Michi Mutsuzaki <mi...@yahoo-inc.com>wrote:
> 
>> Hello,
>> 
>> I'm having trouble using binary type with TFramedTransport in Java. I'm
>> using Thrift 0.6. I have a method that takes a binary as an argument:
>> 
>>    i32 myMethod(1:binary blob),
>> 
>> I have a Java client that calls myMethod():
>> 
>>    TSocket socket = new TSocket("localhost", 9090);
>>    TTransport transport = new TFramedTransport(socket);
>>    transport.open();
>>    TProtocol protocol = new TBinaryProtocol(transport);
>>    MyService.Client client = new MyService.Client(protocol);
>>    client.myMethod(ByteBuffer.wrap("hello".getBytes()));
>> 
>> The server is started like this:
>> 
>>    TServerTransport trans = new TServerSocket(port);
>>    TThreadPoolServer.Args args = new TThreadPoolServer.Args(trans);
>>    args.processor(new MyService.Processor(new MyProcessor()));
>>    args.transportFactory(new TFramedTransport.Factory());
>>    args.protocolFactory(new TBinaryProtocol.Factory(true, true));
>>    TServer server = new TThreadPoolServer(args);
>>    server.serve();
>> 
>> On the server side, myMethod() receives more than "hello". It looks
>> something like this:
>> 
>>    ?^A^@^A^@^@^@^KmyMethod^@^@^@^A^K^@^A^@^@^@^Chello^@
>> 
>> It works correctly if I don't use TFramedTransport. Am I misusing
>> TFramedTransport?
>> 
>> Thanks!
>> --Michi
>> 
>> 
> 


Re: java: using binary and TFramedTransport

Posted by Bryan Duxbury <br...@rapleaf.com>.
No, you are misusing the ByteBuffer. You need to pay attention to
.position() and .limit() - only the bytes between those two values are part
of that field.

On Mon, Apr 25, 2011 at 1:52 PM, Michi Mutsuzaki <mi...@yahoo-inc.com>wrote:

> Hello,
>
> I'm having trouble using binary type with TFramedTransport in Java. I'm
> using Thrift 0.6. I have a method that takes a binary as an argument:
>
>    i32 myMethod(1:binary blob),
>
> I have a Java client that calls myMethod():
>
>    TSocket socket = new TSocket("localhost", 9090);
>    TTransport transport = new TFramedTransport(socket);
>    transport.open();
>    TProtocol protocol = new TBinaryProtocol(transport);
>    MyService.Client client = new MyService.Client(protocol);
>    client.myMethod(ByteBuffer.wrap("hello".getBytes()));
>
> The server is started like this:
>
>    TServerTransport trans = new TServerSocket(port);
>    TThreadPoolServer.Args args = new TThreadPoolServer.Args(trans);
>    args.processor(new MyService.Processor(new MyProcessor()));
>    args.transportFactory(new TFramedTransport.Factory());
>    args.protocolFactory(new TBinaryProtocol.Factory(true, true));
>    TServer server = new TThreadPoolServer(args);
>    server.serve();
>
> On the server side, myMethod() receives more than "hello". It looks
> something like this:
>
>    ?^A^@^A^@^@^@^KmyMethod^@^@^@^A^K^@^A^@^@^@^Chello^@
>
> It works correctly if I don't use TFramedTransport. Am I misusing
> TFramedTransport?
>
> Thanks!
> --Michi
>
>