src/main/java/pityoulish/sockets/server/SimplisticSocketHandler.java
public final void sendResponse(Socket sock, ByteBuffer response)
    throws Exception
  {
    // To make it a bit harder for the clients, don't send everything in a
    // single packet. But at least provide the first tag and length (4 bytes)
    // in the first package. Just to be nice.

    int split = 4 + (int)(Math.random() * (double)(response.remaining()-3));

    try {
      OutputStream os = sock.getOutputStream();
      os.write(response.array(),
               response.position()+response.arrayOffset(),
               split);
      os.flush();
      if (split < response.remaining())
       {
         os.write(response.array(),
                  response.position()+response.arrayOffset()+split,
                  response.remaining()-split);
         os.flush();
       }
    } catch (Exception x) {
      System.out.println(x.toString());
      try {
        sock.close();
      } catch (Exception ignore) {
        x.addSuppressed(ignore);
      }
      throw x;
    }

  }
/**
   * Sends a response over a socket.
   * This flushes the output, but does not close the socket on success.
   *
   * @param sock        the socket over which to send
   * @param response    a byte buffer holding the response, backed by an array.
   *                    The response begins at the current position and
   *                    extends to the limit of the buffer.
   *
   * @throws Exception  in case of a problem
   */
Sends a response over a socket.
This flushes the output, but does not close the socket on success.

@param sock        the socket over which to send
@param response    a byte buffer holding the response, backed by an array.
The response begins at the current position and
extends to the limit of the buffer.

@throws Exception  in case of a problem
Sends a response over a socket . This flushes the output but does not close the socket on success . @param sock the socket over which to send @param response a byte buffer holding the response backed by an array . The response begins at the current position and extends to the limit of the buffer . @throws Exception in case of a problem
public final void sendResponse(Socket sock, ByteBuffer response)
    throws Exception
  {
    int split = 4 + (int)(Math.random() * (double)(response.remaining()-3));
    try {
      OutputStream os = sock.getOutputStream();
      os.write(response.array(),
               response.position()+response.arrayOffset(),
               split);
      os.flush();
      if (split < response.remaining())
       {
         os.write(response.array(),
                  response.position()+response.arrayOffset()+split,
                  response.remaining()-split);
         os.flush();
       }
    } catch (Exception x) {
      System.out.println(x.toString());
      try {
        sock.close();
      } catch (Exception ignore) {
        x.addSuppressed(ignore);
      }
      throw x;
    }
  }
public final void sendResponse ( Socket sock , ByteBuffer response ) throws Exception { int split = 4 + ( int ) ( Math . random ( ) * ( double ) ( response . remaining ( ) - 3 ) ) ; try { OutputStream os = sock . getOutputStream ( ) ; os . write ( response . array ( ) , response . position ( ) + response . arrayOffset ( ) , split ) ; os . flush ( ) ; if ( split < response . remaining ( ) ) { os . write ( response . array ( ) , response . position ( ) + response . arrayOffset ( ) + split , response . remaining ( ) - split ) ; os . flush ( ) ; } } catch ( Exception x ) { System . out . println ( x . toString ( ) ) ; try { sock . close ( ) ; } catch ( Exception ignore ) { x . addSuppressed ( ignore ) ; } throw x ; } }
Sends a response over a socket.
Sends a response over a socket .
// To make it a bit harder for the clients, don't send everything in a // single packet. But at least provide the first tag and length (4 bytes) // in the first package. Just to be nice.