思路就是,
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(); } }