JavaTCP/IP笔记之发送和接受数据

     在程序中使用套接字是因为需要向其他程序提供信息,或使用其他程序提供的信息。任何要交换信息的程序之间在信息的编码方式上必须达成共
识(如将信息表示为位序列),以及哪个程序发送信息,什么时候和怎样接收信息都将影响程序的行为。

     TCP/IP 协议以字节的方式传输用户数据,并没有对其进行检查和修改,使得应用程序可以非常灵活地对其传输的信息进行编码。

     应用程序协议中明确定义了信息的发送者应该怎样排列和解释这些位序列,同时还要定义接收者应该怎样解析,这样才使信息的接收者能够抽取出每个字段的意义。

     TCP/IP 协议的唯一约束是,信息必须在块(chunks)中发送和接收,而块的长度必须是 8 位的倍数。可以认为在 TCP/IP 协议中传输的信息是字节序列。

信息编码 

     简单数据类型,如 int,long,char,String 等,是如何通过套接字发送和接收的。传输信息时可以通过套接字将字节信息写入一个 OutputStream实例中(该实例已经与一个 Socket 相关联),或将其封装进一个DatagramPacket 实例中(该实例将由 DatagramSocket 发送),然而这些操作所能处理的唯一数据类型是字节和字节数组。幸运的是Java提供了内置工具能够帮助我们完成这些转换。



    对于需要超过一个字节来表示的数据类型,必须知道这些字节的发送顺序。从整数的右边开始,由低位到高位地发送,即 little-endian 顺序;或从左边开始,由高位到低位发送,即 big-endian 顺序。

    对于任何多字节的整数,发送者和接收者必须在使用 big-endian 顺序还是使用little-endian顺序上达成共识。如果发送者使用了little-endian顺序来发送上述整数,而接收者以 big-endian 顺序对其进行接收,那么接收者将取到错误的值。

    发送者和接收者需要达成共识的最后一个细节是:所传输的数值是有符号的(signed)还是无符号的(unsigned)。Java 中的四种基本整型都是有符号的,它们的值以二进制补码的方式存储,这是有符号数值的常用表示方式.

    

package network;

public class BruteForceCoding {
	private static byte byteVal = 101;
	private static short shortVal = 10001;
	private static int intVal = 100000001;
	private static long longVal = 1000000000001L;
	private final static int BSIZE = Byte.SIZE;
	private final static int SSIZE = Short.SIZE;
	private final static int ISIZE = Integer.SIZE;
	private final static int LSIZE = Long.SIZE;
	private final static int BYTEMASK = 0xFF;

	public static String byteArrayToDecimalString(byte[] bArray) {		
		StringBuilder rtn = new StringBuilder();
		for (byte b : bArray) {
			rtn.append(b & BYTEMASK).append(" ");			
		}
		return rtn.toString();
	}

	public static int encodeIntBigEndian(byte[] dst, long val, int offset,
			int size) {
		for (int i = 0; i < size; i++) {			
			dst[offset++] = (byte) (val >> ((size - i - 1) * Byte.SIZE));
		}
		return offset;
	}

	public static long decodeIntBigEndian(byte[] val, int offset, int size) {
		long rtn = 0;
		for (int i = 0; i < size; i++) {
			rtn = (rtn << Byte.SIZE) | ((long) val[offset + i] & BYTEMASK);
		}
		return rtn;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		byte[] message = new byte[BSIZE + SSIZE + ISIZE + LSIZE];
		
		System.out.println(BSIZE + SSIZE + ISIZE + LSIZE);
		System.out.println("Encoded message: "+ byteArrayToDecimalString(message));
		
		int offset = encodeIntBigEndian(message, byteVal, 0, BSIZE);
		
		offset = encodeIntBigEndian(message, shortVal, offset, SSIZE);
		
		offset = encodeIntBigEndian(message, intVal, offset, ISIZE);
		
		encodeIntBigEndian(message, longVal, offset, LSIZE);
		
		
				
		long value = decodeIntBigEndian(message, BSIZE, SSIZE);
		System.out.println("Decoded short = " + value);
		value = decodeIntBigEndian(message, BSIZE + SSIZE + ISIZE, LSIZE);
		System.out.println("Decoded long = " + value);

		offset = 4;
		value = decodeIntBigEndian(message, offset, BSIZE);
		System.out.println("Decoded value (offset " + offset + ", size "
				+ BSIZE + ") = " + value);
		byte bVal = (byte) decodeIntBigEndian(message, offset, BSIZE);
		System.out.println("Same value as byte = " + bVal);

	}

}

    DataOutputStream  类允许你将基本数据类型,写入一个流中:它提供了 writeByte(),writeShort(),writeInt(),以及 writeLong()方法。这些方法按照 big-endian 顺序,将整数以适当大小的二进制补码的形式写到流中。ByteArrayOutputStream类获取写到流中的字节序列,并将其转换成一个字节数组。

    

ByteArrayOutputStream buf = new ByteArrayOutputStream(); 
DataOutputStream out = new DataOutputStream(buf); 
out.writeByte(byteVal); 
out.writeShort(shortVal); 
out.writeInt(intVal); 
out.writeLong(longVal); 
out.flush(); 
byte[] msg = buf.toByteArray(); 
    DataInputStream类和ByteArrayInputStream类接受恢复数据。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值