Android Open Source - android-chat-telnet Envoie Message






From Project

Back to project page android-chat-telnet.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android-chat-telnet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.anarchy.anarchytelnet;
/*from ww w.j ava 2s  .  c  o  m*/
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class EnvoieMessage {
  // reference sur l'activite principale
  Main main;
  // l'objet telnet
  AnarchyTelnet telnet;
  // notre IP
  String myIp;

  EnvoieMessage(Main m) {
    // on recupere l'activite
    main = m;
    // on recupere le telnet
    telnet = main.telnet;
    
    try {
      // affectation de l'ip grace a la methode ip() ecrite plus bas
      myIp = ip()+"";
    } catch (SocketException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
    /* methode appelee lorsque le clavier est sorti 
    c'est juste pour faire d'avoir que le bouton "entree" soit un bouton "envoyer" 
    mais c'est pas forcemment necessaire et ca empeche de faire des sauts de lignes */
    main.editText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            // si l'action c'est "envoyer"
            if (actionId == EditorInfo.IME_ACTION_SEND) {
              // on evoie et puis voil 
              envoie();
              handled = true;
            }
            return handled;
        }
    });
  }

  // methode d'envoi d'un message
  public void envoie() {
    /* on doit faire un nouveau thread pour ne pas bloquer la reception des messages
    et comme on modifie l'interface de l'activite principale, c'est mieux de faire un nouveau thread */
    main.mHandler.post(new Runnable() {
      public void run() {
        // on ajoute le texte dans le StringBuffer du telnet
        telnet.sb.append(myIp+ " : " + main.editText.getText()+"\n\r");
        // on change le texte dans l'interface
        main.text.setText(telnet.sb.toString());
        // on ecrit dans le flux sortant
        telnet.out.println(main.editText.getText().toString());
        // on vide le flux sortant
        telnet.out.flush();
        // on efface la zone de saisie
        main.editText.setText("");
        
        
      }
    });
  }
  
  // methode de recuperation de l'ip
  
  // c'est un peu galere mais faut eviter de prendre l'adresse de boucle locale, l'ipv6 etc.
  static InetAddress ip() throws SocketException {
    // on recupere toutes les interfaces
    Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        NetworkInterface ni;
        while (nis.hasMoreElements()) {
            ni = nis.nextElement();
      if (!ni.isLoopback()/*pas la boucle locale*/ && ni.isUp()/*et active*/) {
                  for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
                    //filtre ipv4/ipv6
                      if (ia.getAddress().getAddress().length == 4) {
                          //4 pour ipv4, 16 pour ipv6
                            return ia.getAddress();
                      }
                  }
            }
        }
      return null;
  }
  
}




Java Source Code List

com.anarchy.anarchytelnet.AnarchyTelnet.java
com.anarchy.anarchytelnet.EnvoieMessage.java
com.anarchy.anarchytelnet.Main.java
com.anarchy.anarchytelnet.MessageReceiver.java
org.apache.commons.net.DatagramSocketClient.java
org.apache.commons.net.DatagramSocketFactory.java
org.apache.commons.net.DefaultDatagramSocketFactory.java
org.apache.commons.net.DefaultSocketFactory.java
org.apache.commons.net.MalformedServerReplyException.java
org.apache.commons.net.PrintCommandListener.java
org.apache.commons.net.ProtocolCommandEvent.java
org.apache.commons.net.ProtocolCommandListener.java
org.apache.commons.net.ProtocolCommandSupport.java
org.apache.commons.net.SocketClient.java
org.apache.commons.net.io.CRLFLineReader.java
org.apache.commons.net.io.CopyStreamAdapter.java
org.apache.commons.net.io.CopyStreamEvent.java
org.apache.commons.net.io.CopyStreamException.java
org.apache.commons.net.io.CopyStreamListener.java
org.apache.commons.net.io.DotTerminatedMessageReader.java
org.apache.commons.net.io.DotTerminatedMessageWriter.java
org.apache.commons.net.io.FromNetASCIIInputStream.java
org.apache.commons.net.io.FromNetASCIIOutputStream.java
org.apache.commons.net.io.SocketInputStream.java
org.apache.commons.net.io.SocketOutputStream.java
org.apache.commons.net.io.ToNetASCIIInputStream.java
org.apache.commons.net.io.ToNetASCIIOutputStream.java
org.apache.commons.net.io.Util.java
org.apache.commons.net.io.package-info.java
org.apache.commons.net.telnet.EchoOptionHandler.java
org.apache.commons.net.telnet.InvalidTelnetOptionException.java
org.apache.commons.net.telnet.SimpleOptionHandler.java
org.apache.commons.net.telnet.SuppressGAOptionHandler.java
org.apache.commons.net.telnet.TelnetClient.java
org.apache.commons.net.telnet.TelnetCommand.java
org.apache.commons.net.telnet.TelnetInputListener.java
org.apache.commons.net.telnet.TelnetInputStream.java
org.apache.commons.net.telnet.TelnetNotificationHandler.java
org.apache.commons.net.telnet.TelnetOptionHandler.java
org.apache.commons.net.telnet.TelnetOption.java
org.apache.commons.net.telnet.TelnetOutputStream.java
org.apache.commons.net.telnet.Telnet.java
org.apache.commons.net.telnet.TerminalTypeOptionHandler.java
org.apache.commons.net.telnet.WindowSizeOptionHandler.java
org.apache.commons.net.telnet.package-info.java
org.apache.commons.net.util.Base64.java
org.apache.commons.net.util.Charsets.java
org.apache.commons.net.util.KeyManagerUtils.java
org.apache.commons.net.util.ListenerList.java
org.apache.commons.net.util.SSLContextUtils.java
org.apache.commons.net.util.SubnetUtils.java
org.apache.commons.net.util.TrustManagerUtils.java
org.apache.commons.net.util.package-info.java
org.apache.commons.net.package-info.java