Redian新闻
>
一个socket相关的问题
avatar
一个socket相关的问题# Java - 爪哇娇娃
b*d
1
通过socket 送int数据,
int buffer[10].
但是,write( byte[], off, len ) 要求是byte形。 怎么处理? 在c++
里,可以用memcpy, or (void *). 请问在Java中如何处理?
avatar
e*c
2
Seems to me you have to use bitwise operation.
Also note java have no unsigned type. so use
byte[] makeBytes(int i){
byte[] bytes = new byte[4];
byte[0] = (i>>24)&FF;
...
heihei
}

【在 b*********d 的大作中提到】
: 通过socket 送int数据,
: int buffer[10].
: 但是,write( byte[], off, len ) 要求是byte形。 怎么处理? 在c++
: 里,可以用memcpy, or (void *). 请问在Java中如何处理?

avatar
m*t
3
this solution might not work if you are writing to another machine
that has a different ending scheme.
I would use something like:
ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
objOut.writeInt(anInt);

【在 e***c 的大作中提到】
: Seems to me you have to use bitwise operation.
: Also note java have no unsigned type. so use
: byte[] makeBytes(int i){
: byte[] bytes = new byte[4];
: byte[0] = (i>>24)&FF;
: ...
: heihei
: }

avatar
m*t
4
That was assuming you are using java on both ends. Otherwise it
can get real tricky on the receiving end, because you have to know
which ending scheme the sender machine uses.

【在 m******t 的大作中提到】
: this solution might not work if you are writing to another machine
: that has a different ending scheme.
: I would use something like:
: ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
: objOut.writeInt(anInt);

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。