socketDataOut.flush();
底层发送都存在一个最小尺度问题,如512字节满了才自动发送,而你的代码刚好不够,所以不会引发自动发送

解决方案 »

  1.   

    我的问题出在socket的in上,是read读不到完整的包。这里的包长度是指我们在应用层上的定义,跟底层的无关。底层会帮我们实现差错控制。
      

  2.   

    是不是这里啊?dataSocket.setSendBufferSize(8192);或者是catch一下io的exception.........
      

  3.   

    DATAOUTPUTSTREM 之类的是发送UNICODE,所以。
      

  4.   

    这部分的原因应该是如borz所说,与流的限制有关。与JAVA的SOCKET机制无关。
    发送是0x1001个字节
    接收是0x1002个字节那么大家继续讨论,在这种情况下,难道要我们自己来做底层的工作吗?大家有什么好办法
      

  5.   

    错了!是在SOCKET的socketOptions里    /**
         * Set a hint the size of the underlying buffers used by the
         * platform for outgoing network I/O. When used in set, this is a
         * suggestion to the kernel from the application about the size of
         * buffers to use for the data to be sent over the socket. When
         * used in get, this must return the size of the buffer actually
         * used by the platform when sending out data on this socket.
         *
         * Valid for all sockets: SocketImpl, DatagramSocketImpl
         *
         * @see Socket#setSendBufferSize
         * @see Socket#getSendBufferSize
         * @see DatagramSocket#setSendBufferSize
         * @see DatagramSocket#getSendBufferSize
         */
        public final static int SO_SNDBUF = 0x1001;    /**
         * Set a hint the size of the underlying buffers used by the
         * platform for incoming network I/O. When used in set, this is a
         * suggestion to the kernel from the application about the size of
         * buffers to use for the data to be received over the
         * socket. When used in get, this must return the size of the
         * buffer actually used by the platform when receiving in data on
         * this socket.
         *
         * Valid for all sockets: SocketImpl, DatagramSocketImpl
         *
         * @see Socket#setReceiveBufferSize
         * @see Socket#getReceiveBufferSize
         * @see DatagramSocket#setReceiveBufferSize
         * @see DatagramSocket#getReceiveBufferSize
         */
        public final static int SO_RCVBUF = 0x1002;
      

  6.   

    to airwing
    我还是觉得与底层有关,因为setReceiveBuffer只是告诉JAVA系统设定一种缓冲机制
    但数据总是要发送到网络上的。在物理层,存在一个最小发送长度,如通过ATM传送时好象是512个字节,你可以看看有关书籍,自然明白底层的运作
      

  7.   

    to kz
      和物理层无关,任何应用层数据都会在IP层被分割层多个IP包,由TCP层负责传输和顺序、差错控制。难道各位做程序都只发512字节吗?
      

  8.   

    to airwing
    是这样,例如TCP层发送数据513个字节,但你的网卡与下一个节点如中继器的通信有一个最小发送单位--比方512个字节。当TCP层flush()时物理层即网卡发送了两个数据包:一个512个字节;另一个也是512个字节(但只包含一个有用数据。当从另一端接收时,第一次read将512个字节放入了你的缓冲区(注意:接收必须是以数据包为单位),所以你必须再read一次