title: Chat Messages get lost when wifi is turned off asmack openfire
body: <p>I am trying to build a chat application where I have successfully implemented single chat and group chat. However whenever wifi is turned OFF the connection gets disconnected as expected but when the wifi is turned ON again all the messages sent to the user when it was turned off are lost. I have tried a lot of things but I havent got an answer to it.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>public class ChatConnection extends AsyncTask&lt;Void, Void, Void&gt; implements Constants
  {
  private String userId ;
  public XMPPConnection connection;
  
  private Context context;
  
  public ChatConnection(Context context)
  {
  //        this.userId = "2";
      this.context = context;
  }
  
  @Override
  protected void onPreExecute() {
      super.onPreExecute();
  }
  @Override
  protected Void doInBackground(Void... params)
  {
      // Create a connection
      ConnectionConfiguration connConfig = new ConnectionConfiguration(CHATHOST, PORT);
      connConfig.setSecurityMode(SecurityMode.disabled);
      connConfig.setDebuggerEnabled(true);
      connection = new XMPPTCPConnection(connConfig);
      try {
          connection.connect();
          Log.i("XMPPChatDemoActivity",  "[SettingsDialog] Connected to "+connection.getHost());
  
          if(connection.isConnected())
          {
              Map&lt;String, String&gt; attributes = new HashMap&lt;&gt;();
              attributes.put("username", USERID);
              attributes.put("password", PASSWORD);
              Registration reg = new Registration();
              reg.setType(IQ.Type.SET);
              reg.setTo(connection.getServiceName());
              reg.setAttributes(attributes);
  
              connection.sendPacket(reg);
  
              connection.login(USERID, PASSWORD);
  
              Log.i("XMPPChatDemoActivity",  "Logged in as" + connection.getUser());
              //Set the status to available
              Presence presence = new Presence(Presence.Type.available);
              connection.sendPacket(presence);
              Roster roster = connection.getRoster();
              Collection&lt;RosterEntry&gt; entries = roster.getEntries();
              for (RosterEntry entry : entries)
              {
                  Log.d("XMPPChatDemoActivity",  "--------------------------------------");
                  Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
                  Log.d("XMPPChatDemoActivity", "User: " + entry.getUser());
                  Log.d("XMPPChatDemoActivity", "Name: " + entry.getName());
                  Log.d("XMPPChatDemoActivity", "Status: " + entry.getStatus());
                  Log.d("XMPPChatDemoActivity", "Type: " + entry.getType());
                  Presence entryPresence = roster.getPresence(entry.getUser());
                  Log.d("XMPPChatDemoActivity", "Presence Status: "+ entryPresence.getStatus());
                  Log.d("XMPPChatDemoActivity", "Presence Type: " + entryPresence.getType());
  
                  Presence.Type type = entryPresence.getType();
                  if (type == Presence.Type.available)
                      Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
                  Log.d("XMPPChatDemoActivity", "Presence : " + entryPresence);
              }
          }
      }
      catch (XMPPException ex)
      {
          Log.e("XMPPChatDemoActivity",  "[SettingsDialog] Failed to connect to "+ connection.getHost());
          Log.e("XMPPChatDemoActivity", ex.toString());
          connection = null;
      }
      catch (SmackException e)
      {
          e.printStackTrace();
      }
      catch (IOException e)
      {
          Log.e("IO Exception",e.getMessage());
          e.printStackTrace();
      }
      return null;
  }
  
  @Override
  protected void onPostExecute(Void result) {
      super.onPostExecute(result);
  
      if(connection != null)
      {
          XMPPLogic.getInstance().setConnection(connection);
          MultiUserChat muc = new MultiUserChat(connection, 13+"@"+GROUP_CHAT);
          try {
              DiscussionHistory history = new DiscussionHistory();
              history.setMaxStanzas(0);
              muc.join(USERID +"@"+ CHATHOST,null,history, SmackConfiguration.getDefaultPacketReplyTimeout());
          } catch (NoResponseException e) {
              e.printStackTrace();
          } catch (XMPPErrorException e) {
              e.printStackTrace();
          } catch (NotConnectedException e) {
              e.printStackTrace();
          }
        //            createChatGroup();
          PacketFilter groupFilter = new MessageTypeFilter(Message.Type.groupchat);
  
          PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
  
          connection.addPacketListener(new PacketListener() {
              @Override
              public void processPacket(Packet packet) {
                  Message message = (Message) packet;
                  if (message.getBody() != null) {
                      String fromName = StringUtils.parseBareAddress(message.getFrom());
                      String toName = StringUtils.parseBareAddress(message.getTo());
                      String chatSubject = StringUtils.parseBareAddress(message.getSubject());
  
                      String messageBody = message.getBody();
                      Log.i("XMPPChatDemoActivity ", " Text Recieved " + message.getBody() + " from " + fromName);
                      String[] fromSplit = fromName.split("@");
                      String[] toSplit = toName.split("@");
                      String receiverId = toSplit[0];
                      String senderId = fromSplit[0];
  
                      Intent chatIntent = new Intent("new-chat");
                      chatIntent.putExtra("receiverId", receiverId);
                      chatIntent.putExtra("senderId", senderId);
                      chatIntent.putExtra("messageBody", messageBody);
                      LocalBroadcastManager.getInstance(context).sendBroadcast(chatIntent);
                  }
              }
          }, filter);
  
          connection.addPacketListener(new PacketListener() {
              @Override
              public void processPacket(Packet packet) throws NotConnectedException {
                  Log.e("Group Message", "Received");
  
                  Message message = (Message) packet;
                  if (message.getBody() != null) {
                      String groupName = StringUtils.parseBareAddress(message.getFrom());
                      String fromName = StringUtils.parseResource(message.getFrom());
                      String toName = StringUtils.parseBareAddress(message.getTo());
                      String chatSubject = StringUtils.parseBareAddress(message.getSubject());
  
                      String messageBody = message.getBody();
                      Log.i("XMPPChatDemoActivity ", " Text Recieved " + message.getBody() + " from " + fromName + " to " + toName);
                      String[] groupSplit = groupName.split("@");
                      String[] toSplit = toName.split("@");
                      String[] fromSplit = fromName.split("@");
                      String receiverId = toSplit[0];
                      String fromFirstSubString = fromSplit[0];
                      String senderId = groupSplit[0];
                      String originalGroup = groupSplit[0];
                      if (!fromFirstSubString.equals(receiverId)) {
                          Intent chatIntent = new Intent("new-chat");
                          chatIntent.putExtra("senderId", senderId);
                          chatIntent.putExtra("receiverId", receiverId);
                          chatIntent.putExtra("messageBody", messageBody);
                          LocalBroadcastManager.getInstance(context).sendBroadcast(chatIntent);
                      }
                  }
              }
          }, groupFilter);
  
      }
  }
  </code></pre>
  
  <p>And below is the exception I get when I turn the Wifi off:</p>
  
  <blockquote>
    <p>W/PacketWriter: Exception writing closing stream element
                                                                                  java.net.SocketException: sendto failed: EPIPE (Broken pipe)
                                                                                      at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506)
                                                                                      at libcore.io.IoBridge.sendto(IoBridge.java:475)
                                                                                      at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
                                                                                      at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
                                                                                      at
    java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
                                                                                      at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167)
                                                                                      at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158)
                                                                                      at java.io.BufferedWriter.flush(BufferedWriter.java:124)
                                                                                      at
    org.jivesoftware.smack.util.ObservableWriter.flush(ObservableWriter.java:44)
                                                                                      at
    org.jivesoftware.smack.tcp.PacketWriter.writePackets(PacketWriter.java:190)
                                                                                      at
    org.jivesoftware.smack.tcp.PacketWriter.access$000(PacketWriter.java:40)
                                                                                      at org.jivesoftware.smack.tcp.PacketWriter$1.run(PacketWriter.java:77)
                                                                                   Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken
    pipe)
                                                                                      at libcore.io.Posix.sendtoBytes(Native Method)
                                                                                      at libcore.io.Posix.sendto(Posix.java:151)
                                                                                      at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
                                                                                      at libcore.io.IoBridge.sendto(IoBridge.java:473)
                                                                                      at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507) 
                                                                                      at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46) 
                                                                                      at
    java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269) 
                                                                                      at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167) 
                                                                                      at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158) 
                                                                                      at java.io.BufferedWriter.flush(BufferedWriter.java:124) 
                                                                                      at
    org.jivesoftware.smack.util.ObservableWriter.flush(ObservableWriter.java:44) 
                                                                                      at
    org.jivesoftware.smack.tcp.PacketWriter.writePackets(PacketWriter.java:190) 
                                                                                      at
    org.jivesoftware.smack.tcp.PacketWriter.access$000(PacketWriter.java:40) 
                                                                                      at
    org.jivesoftware.smack.tcp.PacketWriter$1.run(PacketWriter.java:77)</p>
  </blockquote>
  
  <p>I think it could be some issue on the server side. Any leads would be appreciated.</p>