You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by 汤国军 <to...@gmail.com> on 2007/09/04 02:42:56 UTC

problem of using mina communicate with c++ socket

Hi, i am a newer of mina, and i'v got a problem. It like this:

Before ,both of Our Client and server are using C++ socket. But now   we
want to  migrate the server by using mina.
We hava done some simple tests.
When I use c++ socket sending  a string , the simple mina server can receive
it.

But when sending a c++ struct,
struct Employee {
 char name[10];
 int id;
 int salory;
};
the server can only read out "name",can not get the information of "id" and
"salory".

I implemented the method MessageReceive(session, msg). When I try to
type-cast msg to ByteBuffer, there throw out an exception.

So i urgently want to know how can i do this?

thank you and sorry for my poor english.

-- 
Hope is a good thing, maybe the best of the things!

Re: problem of using mina communicate with c++ socket

Posted by mat <fo...@gmail.com>.
Futhermore, if method message.order(ByteOrder.LITTLE_ENDIAN) is called, int
makeInt() is done by the message.order(). Am I right?

On 9/5/07, mat <fo...@gmail.com> wrote:
>
> Sorry, I meant Java.nio API did the order convert for us.
>
> On 9/5/07, Lan Boon Ping <bo...@gmail.com> wrote:
> >
> > Hi,
> >
> > It has nothing to do with JVM, it is just a matter of reading byte in
> > different sequence. For instance, you have a byte stream,
> > byte[] aa =new byte[]{0,0,0,4};
> >
> > //to read big Endian int, i would do
> > int bigEndianInt = makeInt( bytes[ 0 ], bytes[ 1 ], bytes[ 2 ], bytes[ 3
> > ]
> > );
> >
> > //to read little endian int, i would do
> > int littleEndianInt = makeInt( bytes[ 3 ], bytes[ 2 ], bytes[ 1 ],
> > bytes[ 0
> > ] );
> >
> > private static int makeInt( byte byte3, byte byte2, byte byte1, byte
> > byte0 )
> > {
> >        return (int) ( ( ( ( byte3 & 0xff ) << 24 ) |
> >                         ( ( byte2 & 0xff ) << 16 ) |
> >                         ( ( byte1 & 0xff ) << 8 ) |
> >                         ( ( byte0 & 0xff ) << 0 ) ) );
> > }
> >
> > Hope this help.
> >
> > On 9/5/07, 汤国军 < towerness@gmail.com> wrote:
> > >
> > > I don't know whether JVM do it for us.
> > > But  seems that it do not do ByteOrder
> > >
> > >
> > > 2007/9/4, mat < forum.maillist@gmail.com>:
> > > >
> > > > Does JVM do the byte reserve for us in the method message.order(
> > > > ByteOrder.LITTLE_ENDIAN) ?
> > > >
> > > > On 9/4/07, Lan Boon Ping < boonping81@gmail.com> wrote:
> > > > >
> > > > > Hi,
> > > > >
> > > > > Be aware of little Endian(C++) and big Endian(Java), you should be
> > > able
> > > > to
> > > > > use the ByteBuffer to decode the Struct Employee corrrectly. For
> > > > instance,
> > > > >
> > > > > ByteBuffer message;
> > > > >
> > > > > message.order (ByteOrder.LITTLE_ENDIAN) ;
> > > > >
> > > > > byte[] name  = new byte[10];
> > > > > message.get( name);
> > > > > int id = message.getInt();
> > > > > int salary = message.getInt ();
> > > > >
> > > > > Hope this help.
> > > > >
> > > > > Regards
> > > > > BP.
> > > > >
> > > > >
> > > > > On 9/4/07, 汤国军 < towerness@gmail.com> wrote:
> > > > > >
> > > > > > Hi, i am a newer of mina, and i'v got a problem. It like this:
> > > > > >
> > > > > > Before ,both of Our Client and server are using C++ socket. But
> > now
> > > > we
> > > > > > want to  migrate the server by using mina.
> > > > > > We hava done some simple tests.
> > > > > > When I use c++ socket sending  a string , the simple mina server
> > can
> > > > > > receive
> > > > > > it.
> > > > > >
> > > > > > But when sending a c++ struct,
> > > > > > struct Employee {
> > > > > > char name[10];
> > > > > > int id;
> > > > > > int salory;
> > > > > > };
> > > > > > the server can only read out "name",can not get the information
> > of
> > > > "id"
> > > > > > and
> > > > > > "salory".
> > > > > >
> > > > > > I implemented the method MessageReceive(session, msg). When I
> > try to
> > > > > > type-cast msg to ByteBuffer, there throw out an exception.
> > > > > >
> > > > > > So i urgently want to know how can i do this?
> > > > > >
> > > > > > thank you and sorry for my poor english.
> > > > > >
> > > > > > --
> > > > > > Hope is a good thing, maybe the best of the things!
> > > > > >
> > > > >
> > > >
> > >
> > >
> > >
> > > --
> > > Hope is a good thing, maybe the best of the things!
> > >
> >
>
>

Re: problem of using mina communicate with c++ socket

Posted by mat <fo...@gmail.com>.
Sorry, I meant Java.nio API did the order convert for us.

On 9/5/07, Lan Boon Ping <bo...@gmail.com> wrote:
>
> Hi,
>
> It has nothing to do with JVM, it is just a matter of reading byte in
> different sequence. For instance, you have a byte stream,
> byte[] aa =new byte[]{0,0,0,4};
>
> //to read big Endian int, i would do
> int bigEndianInt = makeInt( bytes[ 0 ], bytes[ 1 ], bytes[ 2 ], bytes[ 3 ]
> );
>
> //to read little endian int, i would do
> int littleEndianInt = makeInt( bytes[ 3 ], bytes[ 2 ], bytes[ 1 ], bytes[
> 0
> ] );
>
> private static int makeInt( byte byte3, byte byte2, byte byte1, byte byte0
> )
> {
>        return (int) ( ( ( ( byte3 & 0xff ) << 24 ) |
>                         ( ( byte2 & 0xff ) << 16 ) |
>                         ( ( byte1 & 0xff ) << 8 ) |
>                         ( ( byte0 & 0xff ) << 0 ) ) );
> }
>
> Hope this help.
>
> On 9/5/07, 汤国军 <to...@gmail.com> wrote:
> >
> > I don't know whether JVM do it for us.
> > But  seems that it do not do ByteOrder
> >
> >
> > 2007/9/4, mat <fo...@gmail.com>:
> > >
> > > Does JVM do the byte reserve for us in the method message.order(
> > > ByteOrder.LITTLE_ENDIAN) ?
> > >
> > > On 9/4/07, Lan Boon Ping < boonping81@gmail.com> wrote:
> > > >
> > > > Hi,
> > > >
> > > > Be aware of little Endian(C++) and big Endian(Java), you should be
> > able
> > > to
> > > > use the ByteBuffer to decode the Struct Employee corrrectly. For
> > > instance,
> > > >
> > > > ByteBuffer message;
> > > >
> > > > message.order(ByteOrder.LITTLE_ENDIAN) ;
> > > >
> > > > byte[] name  = new byte[10];
> > > > message.get( name);
> > > > int id = message.getInt();
> > > > int salary = message.getInt();
> > > >
> > > > Hope this help.
> > > >
> > > > Regards
> > > > BP.
> > > >
> > > >
> > > > On 9/4/07, 汤国军 <to...@gmail.com> wrote:
> > > > >
> > > > > Hi, i am a newer of mina, and i'v got a problem. It like this:
> > > > >
> > > > > Before ,both of Our Client and server are using C++ socket. But
> now
> > > we
> > > > > want to  migrate the server by using mina.
> > > > > We hava done some simple tests.
> > > > > When I use c++ socket sending  a string , the simple mina server
> can
> > > > > receive
> > > > > it.
> > > > >
> > > > > But when sending a c++ struct,
> > > > > struct Employee {
> > > > > char name[10];
> > > > > int id;
> > > > > int salory;
> > > > > };
> > > > > the server can only read out "name",can not get the information of
> > > "id"
> > > > > and
> > > > > "salory".
> > > > >
> > > > > I implemented the method MessageReceive(session, msg). When I try
> to
> > > > > type-cast msg to ByteBuffer, there throw out an exception.
> > > > >
> > > > > So i urgently want to know how can i do this?
> > > > >
> > > > > thank you and sorry for my poor english.
> > > > >
> > > > > --
> > > > > Hope is a good thing, maybe the best of the things!
> > > > >
> > > >
> > >
> >
> >
> >
> > --
> > Hope is a good thing, maybe the best of the things!
> >
>

Re: problem of using mina communicate with c++ socket

Posted by Lan Boon Ping <bo...@gmail.com>.
Hi,

It has nothing to do with JVM, it is just a matter of reading byte in
different sequence. For instance, you have a byte stream,
byte[] aa =new byte[]{0,0,0,4};

//to read big Endian int, i would do
int bigEndianInt = makeInt( bytes[ 0 ], bytes[ 1 ], bytes[ 2 ], bytes[ 3 ]
);

//to read little endian int, i would do
int littleEndianInt = makeInt( bytes[ 3 ], bytes[ 2 ], bytes[ 1 ], bytes[ 0
] );

private static int makeInt( byte byte3, byte byte2, byte byte1, byte byte0 )
{
        return (int) ( ( ( ( byte3 & 0xff ) << 24 ) |
                         ( ( byte2 & 0xff ) << 16 ) |
                         ( ( byte1 & 0xff ) << 8 ) |
                         ( ( byte0 & 0xff ) << 0 ) ) );
}

Hope this help.

On 9/5/07, 汤国军 <to...@gmail.com> wrote:
>
> I don't know whether JVM do it for us.
> But  seems that it do not do ByteOrder
>
>
> 2007/9/4, mat <fo...@gmail.com>:
> >
> > Does JVM do the byte reserve for us in the method message.order(
> > ByteOrder.LITTLE_ENDIAN) ?
> >
> > On 9/4/07, Lan Boon Ping < boonping81@gmail.com> wrote:
> > >
> > > Hi,
> > >
> > > Be aware of little Endian(C++) and big Endian(Java), you should be
> able
> > to
> > > use the ByteBuffer to decode the Struct Employee corrrectly. For
> > instance,
> > >
> > > ByteBuffer message;
> > >
> > > message.order(ByteOrder.LITTLE_ENDIAN) ;
> > >
> > > byte[] name  = new byte[10];
> > > message.get( name);
> > > int id = message.getInt();
> > > int salary = message.getInt();
> > >
> > > Hope this help.
> > >
> > > Regards
> > > BP.
> > >
> > >
> > > On 9/4/07, 汤国军 <to...@gmail.com> wrote:
> > > >
> > > > Hi, i am a newer of mina, and i'v got a problem. It like this:
> > > >
> > > > Before ,both of Our Client and server are using C++ socket. But now
> > we
> > > > want to  migrate the server by using mina.
> > > > We hava done some simple tests.
> > > > When I use c++ socket sending  a string , the simple mina server can
> > > > receive
> > > > it.
> > > >
> > > > But when sending a c++ struct,
> > > > struct Employee {
> > > > char name[10];
> > > > int id;
> > > > int salory;
> > > > };
> > > > the server can only read out "name",can not get the information of
> > "id"
> > > > and
> > > > "salory".
> > > >
> > > > I implemented the method MessageReceive(session, msg). When I try to
> > > > type-cast msg to ByteBuffer, there throw out an exception.
> > > >
> > > > So i urgently want to know how can i do this?
> > > >
> > > > thank you and sorry for my poor english.
> > > >
> > > > --
> > > > Hope is a good thing, maybe the best of the things!
> > > >
> > >
> >
>
>
>
> --
> Hope is a good thing, maybe the best of the things!
>

Re: problem of using mina communicate with c++ socket

Posted by 汤国军 <to...@gmail.com>.
I don't know whether JVM do it for us.
But  seems that it do not do ByteOrder


2007/9/4, mat <fo...@gmail.com>:
>
> Does JVM do the byte reserve for us in the method message.order(
> ByteOrder.LITTLE_ENDIAN) ?
>
> On 9/4/07, Lan Boon Ping < boonping81@gmail.com> wrote:
> >
> > Hi,
> >
> > Be aware of little Endian(C++) and big Endian(Java), you should be able
> to
> > use the ByteBuffer to decode the Struct Employee corrrectly. For
> instance,
> >
> > ByteBuffer message;
> >
> > message.order(ByteOrder.LITTLE_ENDIAN) ;
> >
> > byte[] name  = new byte[10];
> > message.get( name);
> > int id = message.getInt();
> > int salary = message.getInt();
> >
> > Hope this help.
> >
> > Regards
> > BP.
> >
> >
> > On 9/4/07, 汤国军 <to...@gmail.com> wrote:
> > >
> > > Hi, i am a newer of mina, and i'v got a problem. It like this:
> > >
> > > Before ,both of Our Client and server are using C++ socket. But now
> we
> > > want to  migrate the server by using mina.
> > > We hava done some simple tests.
> > > When I use c++ socket sending  a string , the simple mina server can
> > > receive
> > > it.
> > >
> > > But when sending a c++ struct,
> > > struct Employee {
> > > char name[10];
> > > int id;
> > > int salory;
> > > };
> > > the server can only read out "name",can not get the information of
> "id"
> > > and
> > > "salory".
> > >
> > > I implemented the method MessageReceive(session, msg). When I try to
> > > type-cast msg to ByteBuffer, there throw out an exception.
> > >
> > > So i urgently want to know how can i do this?
> > >
> > > thank you and sorry for my poor english.
> > >
> > > --
> > > Hope is a good thing, maybe the best of the things!
> > >
> >
>



-- 
Hope is a good thing, maybe the best of the things!

Re: problem of using mina communicate with c++ socket

Posted by mat <fo...@gmail.com>.
Does JVM do the byte reserve for us in the method message.order(
ByteOrder.LITTLE_ENDIAN) ?

On 9/4/07, Lan Boon Ping <bo...@gmail.com> wrote:
>
> Hi,
>
> Be aware of little Endian(C++) and big Endian(Java), you should be able to
> use the ByteBuffer to decode the Struct Employee corrrectly. For instance,
>
> ByteBuffer message;
>
> message.order(ByteOrder.LITTLE_ENDIAN) ;
>
> byte[] name  = new byte[10];
> message.get( name);
> int id = message.getInt();
> int salary = message.getInt();
>
> Hope this help.
>
> Regards
> BP.
>
>
> On 9/4/07, 汤国军 <to...@gmail.com> wrote:
> >
> > Hi, i am a newer of mina, and i'v got a problem. It like this:
> >
> > Before ,both of Our Client and server are using C++ socket. But now   we
> > want to  migrate the server by using mina.
> > We hava done some simple tests.
> > When I use c++ socket sending  a string , the simple mina server can
> > receive
> > it.
> >
> > But when sending a c++ struct,
> > struct Employee {
> > char name[10];
> > int id;
> > int salory;
> > };
> > the server can only read out "name",can not get the information of "id"
> > and
> > "salory".
> >
> > I implemented the method MessageReceive(session, msg). When I try to
> > type-cast msg to ByteBuffer, there throw out an exception.
> >
> > So i urgently want to know how can i do this?
> >
> > thank you and sorry for my poor english.
> >
> > --
> > Hope is a good thing, maybe the best of the things!
> >
>

Re: problem of using mina communicate with c++ socket

Posted by 汤国军 <to...@gmail.com>.
Sorry for late response, i'll try your suggestion.

2007/9/4, Lan Boon Ping <bo...@gmail.com>:
>
> Hi,
>
> Be aware of little Endian(C++) and big Endian(Java), you should be able to
> use the ByteBuffer to decode the Struct Employee corrrectly. For instance,
>
> ByteBuffer message;
>
> message.order(ByteOrder.LITTLE_ENDIAN) ;
>
> byte[] name  = new byte[10];
> message.get( name);
> int id = message.getInt();
> int salary = message.getInt();
>
> Hope this help.
>
> Regards
> BP.
>
>
> On 9/4/07, 汤国军 <to...@gmail.com> wrote:
> >
> > Hi, i am a newer of mina, and i'v got a problem. It like this:
> >
> > Before ,both of Our Client and server are using C++ socket. But now   we
> > want to  migrate the server by using mina.
> > We hava done some simple tests.
> > When I use c++ socket sending  a string , the simple mina server can
> > receive
> > it.
> >
> > But when sending a c++ struct,
> > struct Employee {
> > char name[10];
> > int id;
> > int salory;
> > };
> > the server can only read out "name",can not get the information of "id"
> > and
> > "salory".
> >
> > I implemented the method MessageReceive(session, msg). When I try to
> > type-cast msg to ByteBuffer, there throw out an exception.
> >
> > So i urgently want to know how can i do this?
> >
> > thank you and sorry for my poor english.
> >
> > --
> > Hope is a good thing, maybe the best of the things!
> >
>



-- 
Hope is a good thing, maybe the best of the things!

Re: problem of using mina communicate with c++ socket

Posted by Lan Boon Ping <bo...@gmail.com>.
Hi,

Be aware of little Endian(C++) and big Endian(Java), you should be able to
use the ByteBuffer to decode the Struct Employee corrrectly. For instance,

ByteBuffer message;

message.order(ByteOrder.LITTLE_ENDIAN) ;

byte[] name  = new byte[10];
message.get( name);
int id = message.getInt();
int salary = message.getInt();

Hope this help.

Regards
BP.


On 9/4/07, 汤国军 <to...@gmail.com> wrote:
>
> Hi, i am a newer of mina, and i'v got a problem. It like this:
>
> Before ,both of Our Client and server are using C++ socket. But now   we
> want to  migrate the server by using mina.
> We hava done some simple tests.
> When I use c++ socket sending  a string , the simple mina server can
> receive
> it.
>
> But when sending a c++ struct,
> struct Employee {
> char name[10];
> int id;
> int salory;
> };
> the server can only read out "name",can not get the information of "id"
> and
> "salory".
>
> I implemented the method MessageReceive(session, msg). When I try to
> type-cast msg to ByteBuffer, there throw out an exception.
>
> So i urgently want to know how can i do this?
>
> thank you and sorry for my poor english.
>
> --
> Hope is a good thing, maybe the best of the things!
>