java网络通讯(二)多种类型消息发送
作者:互联网
int类型
思路就是,
public void sendInt(int value){
OutputStream os= null; //这里是用socket的输出流,而不是用serversocket的
try {
os = socket.getOutputStream();
int b24=(value>>24)&0xFF;
int b16=(value>>16)&0xFF;
int b8=(value>>8)&0xFF;
int b0=(value>>0)&0xFF;
os.write(b24);
os.write(b16);
os.write(b8);
os.write(b0);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void receiveInt(){
int vel=0;
try {
InputStream is = client.getInputStream();
int[] msg=new int[4];
System.out.println("开始读消息:");
for (int i=0;i<4;i++){
msg[i]=is.read();
}
for (int i=0;i<4;i++){
vel=vel+msg[i]<<(3-i)*8;
}
System.out.println(vel);
client.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
标签:java,int,value,write,网络通讯,0xFF,多种类型,vel,os 来源: https://blog.csdn.net/bbhallo/article/details/122530487