Exemplo n.º 1
0
 @Override
 public boolean send(String message) {
   if (!socket.isConnected()) return false;
   try {
     output.writeUTF(message);
     Logger.i(this, "has send string " + message.length());
   } catch (IOException e) {
     Logger.printException(this, e);
     return false;
   }
   return true;
 }
Exemplo n.º 2
0
 @Override
 public boolean disconnect() {
   if (!socket.isConnected()) return false;
   try {
     input.close();
     output.close();
     socket.close();
   } catch (IOException e) {
     Logger.printException(this, e);
   }
   return !socket.isConnected();
 }
Exemplo n.º 3
0
 public void initSocket() {
   socket = new Socket();
   try {
     socket.setSendBufferSize(Config.SOCKET_MAX_SEND_BUFFER_SIZE);
     socket.setReceiveBufferSize(Config.SOCKET_MAX_RECEIVE_BUFFER_SIZE);
     socket.setKeepAlive(Config.SOCKET_KEEP_ALIVE_ENABLED);
     socket.setTcpNoDelay(Config.SOCKET_TCP_NO_DELAY);
     socket.setReuseAddress(true);
   } catch (SocketException e) {
     Logger.printException(this, e);
   }
 }
Exemplo n.º 4
0
 @Override
 public boolean connect(String host, int port) {
   initSocket();
   if (socket.isConnected()) return false;
   try {
     socket.connect(new InetSocketAddress(host, port), Config.SOCKET_TIMEOUT);
     input = new DataInputStream(socket.getInputStream());
     output = new DataOutputStream(socket.getOutputStream());
   } catch (ConnectException e) {
     Logger.i(this, "Connection refused...");
   } catch (java.net.SocketTimeoutException e) {
     Logger.e(this, "Socket Timout!");
   } catch (IOException e) {
     Logger.printException(this, e);
   }
   return socket.isConnected();
 }
Exemplo n.º 5
0
  @Override
  public boolean send(byte[] message, int length) throws SocketException {
    if (!socket.isConnected()) return false;

    try {

      BinaryConverter.writeIntToBytes(length, lengthbytes);
      output.write(lengthbytes);
      output.write(message, 0, length);
      // Logger.i(this, "has send bytes " + (temp.length));
    } catch (SocketException e) {
      Logger.toast(this, "Connection lost, returning to Menu.");
      throw e;
    } catch (IOException e) {
      Logger.printException(this, e);
      return false;
    }
    return true;
  }