示例#1
0
  public void listen() {

    while (true) {
      Socket clientSoc = null;

      try {
        clientSoc = serverSocket.accept();
      } catch (IOException e) {
        System.out.println("Accept Socket Error");
        System.exit(1);
      }

      /* Send Welcome Message */
      try {
        out = new PrintWriter(clientSoc.getOutputStream(), true);
        out.println("Thank you for connecting");
        System.out.println("Connection successful with client " + clientSoc.getLocalAddress());
        System.out.println("Waiting for input from " + clientSoc.getLocalAddress());
      } catch (IOException e) {
        e.printStackTrace();
      }

      new Thread(new ClientConnected(clientSoc, out)).start();
    }
  }
示例#2
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();
    }
示例#3
0
  public void run() {
    try {
      try {
        scanner = new Scanner(socket.getInputStream());
        out = new PrintWriter(socket.getOutputStream());

        while (true) {
          checkConn();

          if (!scanner.hasNext()) {
            return;
          }

          msg = scanner.nextLine();

          System.out.println("Client said: " + msg);

          for (int i = 1; i <= Server.ConnectionArray.size(); i++) {
            Socket tSocket = (Socket) Server.ConnectionArray.get(i - 1);
            PrintWriter tOut = new PrintWriter(tSocket.getOutputStream());
            tOut.println(msg);
            tOut.flush();
            System.out.println("Sent to: " + tSocket.getLocalAddress().getHostName());
          }
        }
      } finally {
        socket.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
示例#4
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();
 }
示例#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);
 }
示例#6
0
  public void checkConn() {
    try {

      if (!socket.isConnected()) {
        for (int i = 1; i <= Server.ConnectionArray.size(); i++) {
          if (Server.ConnectionArray.get(i) == socket) {
            Server.ConnectionArray.remove(i);
          }
        }
        for (int i = 1; i <= Server.ConnectionArray.size(); i++) {
          Socket tSocket = (Socket) Server.ConnectionArray.get(i - 1);
          PrintWriter tOut = new PrintWriter(tSocket.getOutputStream());
          tOut.println(tSocket.getLocalAddress().getHostName() + " disconnected!");
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public void alert() {

    System.out.println("\n !!!!!!! tcpclient.!!!!!!!!!! \n");

    String saddr;
    int ch;
    String login_id = " ";

    try {
      Socket clientSocket = new Socket("192.168.0.5", 6789);

      // 172.0.0.1

      /*
       * FileReader file_uid_read=null;
       *
       *
       *
       *
       * file_uid_read=new FileReader("store_uid.txt"); Character c;
       *
       * while((ch=file_uid_read.read())!=-1) { c=new Character((char)ch);
       * login_id=login_id+c.toString(); System.out.println(
       * "login id isssss"+login_id);
       *
       * }//while
       *
       *
       */
      String sentence;
      System.out.println("chkpt 1");
      String modifiedSentence;
      // BufferedReader inFromUser = new BufferedReader( new
      // InputStreamReader(System.in));

      System.out.println("chkpt 1");
      DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
      // BufferedReader inFromServer = new BufferedReader(new
      // InputStreamReader(clientSocket.getInputStream()));
      // sentence = inFromUser.readLine();
      System.out.println("chkpt 1");

      // calc ip addr
      // InetAddress addr=InetAddress.getLocalHost();
      saddr =
          passusername
              + " INVALID LOGIN ATTEMPT AT: ip address "
              + (clientSocket.getLocalAddress()).toString();

      System.out.println("adddrrrr" + saddr);
      saddr = saddr + " by user name " + passusername;

      outToServer.writeBytes(saddr);
      // modifiedSentence = inFromServer.readLine();
      // System.out.println("FROM SERVER: " + modifiedSentence);
      clientSocket.close();

    } // try
    catch (Exception e) {
      System.out.println("2EXCEPTION");
    }
  }
示例#8
0
 public InetSocketAddress getLocalAddress() {
   Socket s = connection.getChannel().socket();
   InetAddress ia = s.getLocalAddress();
   int port = s.getLocalPort();
   return new InetSocketAddress(ia, port);
 }
示例#9
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);
      }
    }
  }
示例#10
0
 public InetAddress getLocalAddress() {
   return mSocket.getLocalAddress();
 }
示例#11
0
  /*
   *    Send a message to the server
   *
   */
  void sendToServer() {

    String replyCode;
    while (state != SMTP_State.DONE)
      try {
        replyCode = in.readLine();
        if (replyCode != null) {
          System.out.println(replyCode);
          switch (state) {
            case HELO: // Verify the initial TCP connection, send reply
              System.out.println("STATE = HELO");
              if (replyCode.substring(0, 3).equals("220")) {
                state = SMTP_State.MAIL;
                out.println("HELO" + socket.getLocalAddress());
                System.out.println("HELO" + socket.getLocalAddress());
              } else {
                System.out.println("Error, connection not established.\n" + replyCode);
                state = SMTP_State.DONE;
              }
              break;

            case MAIL: // Wait for HELO response
              System.out.println("STATE = MAIL");
              if (replyCode.substring(0, 3).equals("250")) {
                state = SMTP_State.RCPT;
                System.out.println("MAIL FROM: " + sender_email_address);
                out.println("MAIL FROM: " + sender_email_address);
              } else {
                System.out.println("Error in HELO response.\n" + replyCode);
                state = SMTP_State.DONE;
              }
              break;

            case RCPT:
              System.out.println("STATE = RCPT");
              if (replyCode.substring(0, 3).equals("250")) {
                state = SMTP_State.DATA;
                System.out.println("RCPT TO: " + receiver_email_address);
                out.println("RCPT TO: " + receiver_email_address);
              } else {
                System.out.println("Error in MAIL response.\n" + replyCode);
                state = SMTP_State.DONE;
              }
              break;

            case DATA: // Wait for recipient ok response before sending data
              System.out.println("STATE = DATA");
              if (replyCode.substring(0, 3).equals("250")) {
                state = SMTP_State.MESSAGE;
                out.println("DATA");
              } else {
                System.out.println("Error in RCPT response: \n" + replyCode);
                state = SMTP_State.DONE;
              }
              break;

            case MESSAGE: // Send formatted message
              System.out.println("STATE = MESSAGE");
              if (replyCode.substring(0, 3).equals("354")) {
                state = SMTP_State.QUIT;
                out.println(MIME);
                out.println(".");
                System.out.println(".");
              } else {
                System.out.println("Error in DATA response: \n" + replyCode);
                state = SMTP_State.DONE;
              }
              break;

            case QUIT: // Send formatted message
              System.out.println("STATE = QUIT");
              if (replyCode.substring(0, 3).equals("250")) {
                state = SMTP_State.FINISH;
                out.println("QUIT");
              } else {
                System.out.println("Error in MESSAGE response.");
                state = SMTP_State.DONE;
              }
              break;

            case FINISH: // Final message
              System.out.println("STATE = FINISH");
              if (replyCode.substring(0, 3).equals("221")) {
                state = SMTP_State.DONE;
                out.println("QUIT");
              } else {
                System.out.println("Error in QUIT response.");
                state = SMTP_State.DONE;
              }
              break;
            case DONE: // When to close ports?
              break;
          }
        }
      } catch (IOException e) {
        System.out.println(e);
      }
  }
示例#12
0
 public String getLocalHost() {
   return sc.getLocalAddress().getHostAddress();
 }