AnsweredAssumed Answered

XMPP File Transfer Error: refused cancelled error The peer did not find any of the provided stream mechanisms acceptable.

Question asked by tushar on Sep 9, 2016

First i run the 2nd user which executes fine but while running the 1st user i get the "refused cancelled error The peer did not find any of the provided stream mechanisms acceptable." error. How do i resolve it? 

 

XmppTest : this is my 1st user for conversation.

 

package com.javacodegeeks.xmpp;

import java.util.Scanner;

 

public class XmppTest {

 

  public static void main(String[] args) throws Exception {

 

  String username = "tupa616684";

  String password = "12345678";

 

  XmppManager xmppManager = new XmppManager("127.0.0.1", 5222);

 

  xmppManager.init();

  xmppManager.performLogin(username, password);

  xmppManager.setStatus(true, "Hello everyone");

 

  String buddyJID = "dhvi427017@yob-dtp-12-stu5tpa";

  String buddyName = "dhvi427017";

  xmppManager.createEntry(buddyJID, buddyName);

 

  xmppManager.sendMessage(null, "dhvi427017@yob-dtp-12-stu5tpa");

  //System.out.println("aaaaaaaaaaaaaaaaa"+xmppManager.getUsers().size());

  xmppManager.send();

  boolean isRunning = true;

  int i=0;

  while (isRunning) {

  //Thread.sleep(10000);

  Scanner reader = new Scanner(System.in);  // Reading from System.in

  System.out.println("Enter a message: ");

  String n = reader.nextLine();

  xmppManager.sendMessage(n, "dhvi427017@yob-dtp-12-stu5tpa");

  }

 

  xmppManager.destroy();

 

  }

}

 

XmppTestUser : This is my 2nd user of conversation.

 

package com.javacodegeeks.xmpp;

import java.util.Scanner;

 

public class XmppTestUser {

 

  public static void main(String[] args) throws Exception {

 

  String username = "dhvi427017";

  String password = "12345678";

 

  XmppManager xmppManager = new XmppManager("127.0.0.1",5222);

 

  xmppManager.init();

  xmppManager.performLogin(username, password);

  xmppManager.setStatus(true, "Hello everyone");

 

  /*String buddyJID = "prashant@yob-ltp-20-dvi";

  String buddyName = "prashant";

  xmppManager.createEntry(buddyJID, buddyName);*/

 

  xmppManager.sendMessage("Hello ADMIN", "tupa616684@yob-dtp-12-stu5tpa");

  xmppManager.receive();

  xmppManager.printRoster();

 

  boolean isRunning = true;

  int i=0;

  while (isRunning) {

  Scanner reader = new Scanner(System.in);  // Reading from System.in

  System.out.println("Enter a message: ");

  String n = reader.nextLine();

 

  //Thread.sleep(12000);

  xmppManager.sendMessage(n, "tupa616684@yob-dtp-12-stu5tpa");

  }

 

  xmppManager.destroy();

  }

}

 

XmppManager : This is a manager file which contains required methods.

 

package com.javacodegeeks.xmpp;

 

import java.io.File;

import java.util.ArrayList;

import java.util.Collection;

import java.util.HashMap;

 

 

import org.jivesoftware.smack.Chat;

import org.jivesoftware.smack.ChatManager;

import org.jivesoftware.smack.ConnectionConfiguration;

import org.jivesoftware.smack.MessageListener;

import org.jivesoftware.smack.Roster;

import org.jivesoftware.smack.RosterEntry;

import org.jivesoftware.smack.SmackConfiguration;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;

import org.jivesoftware.smack.packet.Message;

import org.jivesoftware.smack.packet.Presence;

import org.jivesoftware.smack.packet.Presence.Type;

import org.jivesoftware.smackx.filetransfer.FileTransferListener;

import org.jivesoftware.smackx.filetransfer.FileTransferManager;

import org.jivesoftware.smackx.filetransfer.FileTransferRequest;

import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;

import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;

import org.jivesoftware.smackx.filetransfer.FileTransfer.Status;

 

 

public class XmppManager {

 

  private static final int packetReplyTimeout = 500; // millis

 

  private String server;

  private int port;

 

  private ConnectionConfiguration config;

  private XMPPConnection connection;

 

  private ChatManager chatManager;

  private MessageListener messageListener;

 

  public XmppManager(String server, int port) {

  this.server = server;

  this.port = port;

  }

 

  public void init() throws XMPPException {

 

  System.out.println(String.format("Initializing connection to server %1$s port %2$d", server, port));

 

 

  SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);

 

  config = new ConnectionConfiguration(server, port);

  config.setSASLAuthenticationEnabled(false);

  config.setSecurityMode(SecurityMode.disabled);

 

  connection = new XMPPConnection(config);

  connection.connect();

 

  System.out.println("Connected: " + connection.isConnected());

 

  chatManager = connection.getChatManager();

  messageListener = new MyMessageListener();

 

  }

 

  public void performLogin(String username, String password) throws XMPPException {

  if (connection!=null && connection.isConnected()) {

  connection.login(username, password);

  }

  }

 

  public void setStatus(boolean available, String status) {

 

  Presence.Type type = available? Type.available: Type.unavailable;

  Presence presence = new Presence(type);

 

  presence.setStatus(status);

  connection.sendPacket(presence);

 

  }

 

  public void destroy() {

  if (connection!=null && connection.isConnected()) {

  connection.disconnect();

  }

  }

 

  public void printRoster() throws Exception {

  Roster roster = connection.getRoster();

  Collection<RosterEntry> entries = roster.getEntries();

  for (RosterEntry entry : entries) {

     System.out.println(String.format("Buddy:%1$s - Status:%2$s",

      entry.getName(), entry.getStatus()));

  }

  }

 

  public void sendMessage(String message, String buddyJID) throws XMPPException {

  System.out.println(String.format("Sending mesage '%1$s' to user %2$s", message, buddyJID));

  Chat chat = chatManager.createChat(buddyJID, messageListener);

  chat.sendMessage(message);

  }

 

  public void createEntry(String user, String name) throws Exception {

  System.out.println(String.format("Creating entry for buddy '%1$s' with name %2$s", user, name));

  Roster roster = connection.getRoster();

  roster.createEntry(user, name, null);

  }

 

  class MyMessageListener implements MessageListener {

 

 

  @Override

  public void processMessage(Chat chat, Message message) {

  String from = message.getFrom();

  String body = message.getBody();

  if(body !=null){

  System.out.println(String.format("'%1$s' from %2$s", body, from));

  }

 

  }

 

  }

 

  public ArrayList<HashMap<String, String>> getUsers(){

     ArrayList<HashMap<String, String>> usersList=new ArrayList<HashMap<String, String>>();

     Presence presence = new Presence(Presence.Type.available);

     connection.sendPacket(presence);

     final Roster roster = connection.getRoster();

     Collection<RosterEntry> entries = roster.getEntries();

 

     for (RosterEntry entry : entries) {

 

             HashMap<String, String> map = new HashMap<String, String>();

             Presence entryPresence = roster.getPresence(entry.getUser());

 

             Presence.Type type = entryPresence.getType();      

 

             map.put("USER", entry.getName().toString());

             map.put("STATUS", type.toString());

             usersList.add(map);

 

     }

  return usersList;

  }

  public void send() throws XMPPException{

  XmppManager xmppManager = new XmppManager("127.0.0.1", 7777);

  //xmppManager.init();

  xmppManager.performLogin("tupa616684", "12345678");

 

  SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);

 

  config = new ConnectionConfiguration(server, port);

  config.setSASLAuthenticationEnabled(false);

  config.setSecurityMode(SecurityMode.disabled);

 

  connection = new XMPPConnection(config);

  connection.connect();

  FileTransferManager manager = new FileTransferManager(connection);

  OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("dhvi427017@yob-dtp-12-stu5tpa");

  File file = new File("C:\\Users\\Tushar.Patil\\Desktop\\scopeList.txt");

  try {

    transfer.sendFile(file, "scopeList.txt");

  } catch (XMPPException e) {

    e.printStackTrace();

  }

  while(!transfer.isDone()) {

    if(transfer.getStatus().equals(Status.error)) {

       System.out.println("ERROR!!! " + transfer.getError());

    } else if (transfer.getStatus().equals(Status.cancelled)

                     || transfer.getStatus().equals(Status.refused)) {

       System.out.println("Cancelled!!! " + transfer.getError());

    }

    try {

       Thread.sleep(1000L);

    } catch (InterruptedException e) {

       e.printStackTrace();

    }

  }

  if(transfer.getStatus().equals(Status.refused) || transfer.getStatus().equals(Status.error)

  || transfer.getStatus().equals(Status.cancelled)){

    System.out.println("refused cancelled error " + transfer.getError());

  } else {

    System.out.println("Success");

  }

  }

 

  public void receive(){

 

  FileTransferManager manager = new FileTransferManager(connection);

 

  manager.addFileTransferListener(new FileTransferListener() {

    public void fileTransferRequest(final FileTransferRequest request) {

       new Thread(){

          @Override

          public void run() {

             IncomingFileTransfer transfer = request.accept();

             //File mf = Environment.getExternalStorageDirectory();

             File file = new File("C:\\Users\\Tushar.Patil\\Desktop\\" + transfer.getFileName());

             try{

                 transfer.recieveFile(file);

                 while(!transfer.isDone()) {

                    try{

                       Thread.sleep(1000L);

                    }catch (Exception e) {

                    }

                    if(transfer.getStatus().equals(Status.error)) {

                    }

                    if(transfer.getException() != null) {

                       transfer.getException().printStackTrace();

                    }

                 }

              }catch (Exception e) {

             }

          };

        }.start();

     }

  });

  }

 

}

Outcomes