예제 #1
0
    public String toString() {
      StringBuilder ret = new StringBuilder();
      InetAddress local = null, remote = null;
      String local_str, remote_str;

      Socket tmp_sock = sock;
      if (tmp_sock == null) ret.append("<null socket>");
      else {
        // since the sock variable gets set to null we want to make
        // make sure we make it through here without a nullpointer exception
        local = tmp_sock.getLocalAddress();
        remote = tmp_sock.getInetAddress();
        local_str = local != null ? Util.shortName(local) : "<null>";
        remote_str = remote != null ? Util.shortName(remote) : "<null>";
        ret.append(
            '<'
                + local_str
                + ':'
                + tmp_sock.getLocalPort()
                + " --> "
                + remote_str
                + ':'
                + tmp_sock.getPort()
                + "> ("
                + ((System.currentTimeMillis() - last_access) / 1000)
                + " secs old)");
      }
      tmp_sock = null;

      return ret.toString();
    }
  public ServerMessageGetterSender(Socket socket) {

    this.socket = socket;

    // Get input and output streams in reverse order of ClientMessageGetterSender class
    try {
      this.out = new ObjectOutputStream(socket.getOutputStream());
    } catch (IOException ioe) {
      System.out.println("Could not get ObjectOutputStream on socket: " + socket.getLocalPort());
    }

    try {
      this.in = new ObjectInputStream(socket.getInputStream());
    } catch (IOException ioe) {
      System.out.println("Could not get ObjectInputStream on socket: " + socket.getLocalPort());
    }
  }
예제 #3
0
 private String getSockAddress() {
   StringBuilder sb = new StringBuilder();
   if (sock != null) {
     sb.append(sock.getLocalAddress().getHostAddress()).append(':').append(sock.getLocalPort());
     sb.append(" - ")
         .append(sock.getInetAddress().getHostAddress())
         .append(':')
         .append(sock.getPort());
   }
   return sb.toString();
 }
  public boolean sendMessage(Message messageToSend) {

    try {
      out.writeObject(messageToSend);
    } catch (IOException ioe) {
      System.out.println("Could not write to socket: " + socket.getLocalPort());
      return false;
    }

    return true;
  }
예제 #5
0
 public String toString() {
   Socket tmp_sock = sock;
   if (tmp_sock == null) return "<null socket>";
   InetAddress local = tmp_sock.getLocalAddress(), remote = tmp_sock.getInetAddress();
   String local_str = local != null ? Util.shortName(local) : "<null>";
   String remote_str = remote != null ? Util.shortName(remote) : "<null>";
   return String.format(
       "%s:%s --> %s:%s (%d secs old) [%s] [recv_buf=%d]",
       local_str,
       tmp_sock.getLocalPort(),
       remote_str,
       tmp_sock.getPort(),
       TimeUnit.SECONDS.convert(getTimestamp() - last_access, TimeUnit.NANOSECONDS),
       status(),
       receiver != null ? receiver.bufferSize() : 0);
 }
  public Message getMessage() {

    // TODO error handling if cannot read message

    Message message;

    try {
      message = (Message) in.readObject();
      return message;
    } catch (IOException ioe) {
      System.out.println("Could not read from socket: " + socket.getLocalPort());

      // TODO create error Message type, instantiate one here and send it back
      return new CommandMessage("error", "error");
    } catch (ClassNotFoundException cnfe) {
      System.out.println("Error: Class not found");
      return new CommandMessage("error", "error");
    }
  }
예제 #7
0
 public InetSocketAddress getLocalAddress() {
   Socket s = connection.getChannel().socket();
   InetAddress ia = s.getLocalAddress();
   int port = s.getLocalPort();
   return new InetSocketAddress(ia, port);
 }
예제 #8
0
  public void listenSocket() {
    // Create socket connection
    ObjectInputStream ois = null;
    ObjectOutputStream oos = null;
    Date date = null;
    int[][] gridd = new int[9][9];
    boolean[][] fieldsDefault = new boolean[9][9];

    try {
      socket = new Socket("localhost", 12340);
      System.out.println(
          "Connected to"
              + socket.getInetAddress()
              + " on port "
              + socket.getPort()
              + "from port "
              + socket.getLocalPort()
              + " of "
              + socket.getLocalAddress());

      oos = new ObjectOutputStream(socket.getOutputStream());
      ois = new ObjectInputStream(socket.getInputStream());
      // read an object from the server
      // System.out.println("Before reading the object");
      // date = (Date) ois.readObject();

      System.out.println("Before reading the object");

      gridd = (int[][]) ois.readObject();
      fieldsDefault = (boolean[][]) ois.readObject();
      System.out.println("After reading the object");

      for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
          System.out.println(gridd[i][j] + " |");
        }
        System.out.println("\n");
      }
      for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
          System.out.println("**************************" + fieldsDefault[i][j] + " |");
        }
        System.out.println("\n");
      }

      gg.getGrid().resetGrid();
      gg.getGrid().setFieldss(gridd);
      // System.out.print("The date is: " + date);
      System.out.println("Blah Blah");

      //	oos.close();
      //	ois.close();
    } catch (Exception e) {
      System.out.println(e);
    }

    // ----------------------------------------------------------------------------------------------------------------
    try {
      out = new PrintWriter(socket.getOutputStream(), true);

      in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    } catch (UnknownHostException e) {
      System.out.println("Unknown host: kq6py.eng");
      System.exit(1);
    } catch (IOException e) {
      System.out.println("No I/O" + e);
      System.exit(1);
    }

    int i = -1;
    while (i == -1) {
      try {
        String line = in.readLine();
        /*System.out.println("Line is  :  "+ line);
        System.out
        .println("Connected to" + socket.getInetAddress()
        		+ " on port " + socket.getPort() + "from port "
        		+ socket.getLocalPort() + " of "
        		+ socket.getLocalAddress());
        		*/
        // if (line.length() > 0)
        if (line != null) {
          System.out.println("Someone else won!!!");
          doOtherPersonWon();
          i = 0;
        }

      } catch (IOException e) {
        System.out.println("Read failed");
        System.exit(1);
      }
    }
  }
예제 #9
0
 public int getLocalPort() {
   return mSocket.getLocalPort();
 }
예제 #10
0
 public int getLocalPort() {
   return sc.getLocalPort();
 }