Ejemplo n.º 1
0
  /**
   * method to start the udp server
   *
   * @param portAddress
   */
  @SuppressWarnings("resource")
  public void TCPServer(int portAddress) {

    Socket socket = null;
    try {
      ServerSocket serverSocket = new ServerSocket(portAddress);
      while (true) {
        socket = serverSocket.accept();
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        OutputStream os = socket.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        String sendMessage = "";
        String number = br.readLine();
        String temp[];
        temp = number.replace("$$$spliter$$$", "|").split("\\|");
        // if user wants to get the time from the server
        if (temp[0].charAt(0) == '1' || temp[0].charAt(0) == '0') {
          String sendMessage1 = tsapp.name + "";
          // if the user wants to get time in calender format
          if (temp[0].charAt(0) == '0') {
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(tsapp.name);
            sendMessage1 = c.toInstant() + "\n";
          }
          dos.writeBytes(sendMessage1 + "\n");
          dos.flush();
        }
        // if the user wants to set the time
        else if (temp[0].charAt(0) == '2') {
          // if the user did'nt send username or password
          if (temp[1].equals("")) sendMessage = ("Invalid User:\n");
          // if the user sends username and password
          else if (temp[1].equals(tsapp.actualUserName + tsapp.actualPassWord)) {
            sendMessage = ("Valid User: Data set on the server is" + temp[2] + "\n");
            tsapp.name = Long.parseLong(temp[2]);
          }
          // if the username and password does'nt match
          else sendMessage = ("Invalid User:\n");
          dos.writeBytes(sendMessage + "\n");
          dos.flush();
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        socket.close();
      } catch (Exception e) {
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * method to start udp server
   *
   * @param portAddress
   * @throws IOException
   */
  @SuppressWarnings("resource")
  public void UDPServer(int portAddress) throws IOException {
    DatagramSocket receiveSocket = new DatagramSocket(portAddress);
    while (true) {
      byte[] receivebuffer = new byte[1024];
      byte sendBuffer[] = new byte[1024];
      DatagramPacket receivePacket = new DatagramPacket(receivebuffer, receivebuffer.length);
      receiveSocket.receive(receivePacket);
      String number = new String((receivePacket.getData())).trim();
      String temp[];
      temp = number.replace("$$$spliter$$$", "|").split("\\|");
      int port = receivePacket.getPort();
      InetAddress inet = receivePacket.getAddress();
      // if the user wants to get time
      if (temp[0].charAt(0) == '1' || temp[0].charAt(0) == '0') {
        String returnMessage = tsapp.name + "\n";
        // to send time user in calender format
        if (temp[0].charAt(0) == '0') {
          Calendar c = Calendar.getInstance();
          c.setTimeInMillis(tsapp.name);
          returnMessage = c.toInstant() + "\n";
        }
        sendBuffer = returnMessage.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, inet, port);
        receiveSocket.send(sendPacket);
      }
      // if the user wants to set the time
      else if (temp[0].charAt(0) == '2') {
        String sendMessage;
        // if the user didnt send the username or password
        if (temp[1].equals("")) sendMessage = ("Invalid User:\n");
        // if username and password is sent from user
        else if (temp[1].equals(tsapp.actualUserName + tsapp.actualPassWord)) {
          sendMessage = ("Valid User: Data set on the server is" + temp[2] + "\n");
          tsapp.name = Long.parseLong(temp[2]);
        }
        // username and password does not match
        else sendMessage = ("Invalid User:\n");
        // send the message to the client
        sendBuffer = sendMessage.getBytes();

        DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, inet, port);
        receiveSocket.send(sendPacket);
      }
    }
  }