Example #1
0
  public static void main(String[] args) {
    try {
      Socket s1 = new Socket("127.0.0.1", 57643);

      InputStream is = s1.getInputStream();
      DataInputStream dis = new DataInputStream(is);
      System.out.println(dis.readUTF());

      OutputStream os = s1.getOutputStream();
      DataOutputStream dos = new DataOutputStream(os);
      dos.writeUTF("Oh my gosh...");

      dis.close();
      is.close();
      dos.close();
      os.close();

      s1.close();
    } catch (ConnectException connExc) {
      connExc.printStackTrace();
      System.out.println("Server connection failed");
    } catch (IOException ioExc) {
      ioExc.printStackTrace();
    }
  }
Example #2
0
  /**
   * Initialize a server on the given port. This server will not listen until it is launched with
   * the start() method.
   *
   * @param port
   * @throws IOException
   */
  public Server(int port, int botPort, Retriever ret, Map<String, Double> traffic)
      throws IOException {
    if (port <= 1024 || botPort <= 1024) {
      throw new IllegalArgumentException("Ports under 1024 are reserved!");
    }

    _port = port;
    _clients = new ClientPool();
    _socket = new ServerSocket(port);
    try {
      _botSocket = new Socket("localhost", botPort);
      _botConnectionSuccess = true;
      _trafficInput = new BufferedReader(new InputStreamReader(_botSocket.getInputStream()));
      _trafficData = traffic;
    } catch (ConnectException e) {
      System.out.println("unable to connect");
      System.out.println(e.getMessage());
      _botConnectionSuccess = false;
    }
    r = ret;
  }