How to initialize a connection for filetransfer

Hi everyone,

I’m doing an IM system with smack and openfire which need a function to implement filetransfer.

I have initialize a conneciton for send and receive message, and the port is 5222.

ConnectionConfiguration config=new ConnectionConfiguration(ConstantUtils.IPAddress,5222);
                    config.setSASLAuthenticationEnabled(false);
                    config.setSecurityMode(SecurityMode.disabled);
                    ConstantUtils.conn2=new XMPPConnection(config);
try{
                                        ConstantUtils.conn2.connect();
                                        ConstantUtils.conn2.login(ConstantUtils.loginname, loginpasswd);
                                        System.out.println("connect succes");
                              }catch(XMPPException e){
                                        e.printStackTrace();
                              }

The code works fine.

But when I want to initialize the connection for filetransfer. It doesn’t work.

The code is almost the same except the port, I have change it to 7777.

ConnectionConfiguration config= new ConnectionConfiguration(ConstantUtils.IPAddress,7777);
                    config.setSASLAuthenticationEnabled(false);
                    config.setSecurityMode(SecurityMode.disabled);
                    ConstantUtils.fileconn=new XMPPConnection(config);
try {
                                                  ConstantUtils.fileconn.connect();
                                                  System.out.println("connect done");
                                                  ConstantUtils.fileconn.login(ConstantUtils.loginname, loginpasswd);
                                                  System.out.println("login done");
                                        } catch (Exception e) {
                                                  // TODO Auto-generated catch block
                                                  e.printStackTrace();
                                        }

It doesn’t work or catch any excepiton.

Did I do it a wrong way?

Thank you.

Strong Su

File transfer is using the same connection which you used to connect to the server with port 5222/5223, fileTransferManager will use port 7777 to send and recieve data.

FileTransferManager fileTransferManager = new FileTransferManager(connection);

Thank you for your reply, Wael.

Well, it seems I need to go back to slove the old problem. If file transfer uses the same connection and it will automatically use port 7777. But the transfer status is always error and the error which I catch is null.

Thank you very much.

Here is my code of send file:

**public** **void** onClick(View v){ FileTransferManager manager=**new** FileTransferManager(ConstantUtils.conn2); OutgoingFileTransfer transfer=manager.createOutgoingFileTransfer(ConstantUtils.chatname+"@srong/Smack"); File file=**new** File("/mnt/sdcard/test1.jpg"); System.out.println("begin to send file"); **try** { transfer.sendFile(file,"hahaha"); } **catch** (XMPPException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("done(send file)"); **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("Canceled!!!"+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"); }

Try to add the followings lines:

1.FileTransferNegotiator.setServiceEnabled(ConstantUtils.conn2, true);

2.OutgoingFileTransfer.setResponseTimeout(30000);

I add the code you mentioned. The problem stays the same. I debug my code and here is the result

06-25 05:07:29.317: D/SMACK(1008): 05:07:29 AM RCV (-1261183816):

 <si xmlns="[http://jabber.org/protocol/si](http://jabber.org/protocol/si)" id="jsi_3828818928404921117" mime-type="image/jpeg" profile="[http://jabber.org/protocol/si/profile/file-transfer](http://jabber.org/protocol/si/profile/file-transfer)">

      <file xmlns="[http://jabber.org/protocol/si/profile/file-transfer](http://jabber.org/protocol/si/profile/file-transfer)" name="test1.jpg" size="11201">

test1

      <feature xmlns="[http://jabber.org/protocol/feature-neg](http://jabber.org/protocol/feature-neg)">

<field var="stream-method"type=“listsingle”>

                          <value>[http://jabber.org/protocol/bytestreams](http://jabber.org/protocol/bytestreams)</value>
                          <value>[http://jabber.org/protocol/ibb](http://jabber.org/protocol/ibb)</value>

06-25 05:07:29.407: D/SMACK(1008): 05:07:29 AM SENT (-1261183816):

 <si xmlns="[http://jabber.org/protocol/si](http://jabber.org/protocol/si)">

      <feature xmlns="[http://jabber.org/protocol/feature-neg](http://jabber.org/protocol/feature-neg)">
                     <value>[http://jabber.org/protocol/bytestreams](http://jabber.org/protocol/bytestreams)</value>

                     <value>[http://jabber.org/protocol/ibb](http://jabber.org/protocol/ibb)</value>

06-25 05:07:29.487: D/SMACK(1008): 05:07:29 AM RCV (-1261183816):

      <query xmlns="[http://jabber.org/protocol/disco#info](http://jabber.org/protocol/disco#info)"/>

06-25 05:07:34.557: D/SMACK(1008): 05:07:34 AM RCV (-1261183816):

      <open xmlns="[http://jabber.org/protocol/ibb](http://jabber.org/protocol/ibb)" block-size="4096" sid="jsi_3828818928404921117" stanza="iq"/>

06-25 05:07:34.577: D/SMACK(1008): 05:07:34 AM SENT (-1261183816):

06-25 05:07:34.657: D/SMACK(1008): 05:07:34 AM RCV (-1261183816):

 <data xmlns="[http://jabber.org/protocol/ibb](http://jabber.org/protocol/ibb)" seq="0" sid="jsi_3828818928404921117">

/9j/4AAQSkZJRgABAQEAYABgA

06-25 05:07:39.747: D/SMACK(1008): 05:07:39 AM RCV (-1261183816):

 <data xmlns="[http://jabber.org/protocol/ibb](http://jabber.org/protocol/ibb)" seq="0" sid="jsi_3828818928404921117">

/9j/4AAQSkZJRgABAQEAYABgA

06-25 05:07:44.697: D/SMACK(1008): 05:07:44 AM RCV (-1261183816):

 <close xmlns="[http://jabber.org/protocol/ibb](http://jabber.org/protocol/ibb)" sid="jsi_3828818928404921117"/>

Thank you.

I compare my debug with XEP-0047. I think my code suddenly stopped when sending files considering xml with ID V3rGq-7 and V3rGq-8.

Maybe there is a problem with your file receiver

IncomingFileTransfer transfer = fileTransferRequest.accept();

String root = Environment.getExternalStorageDirectory().toString();

String path = “/YourFolder”;

File myDir = new File(root + path);

myDir.mkdirs();

try {

transfer.recieveFile(new File(root + path, transfer.getFileName()));

} catch (Exception e) {

e.printStackTrace();

}

Sometime this line:

transfer.recieveFile(new File(root + path, transfer.getFileName()));

throw a silent error if there is somthing wrong with the new file.

Thank you very much. I looked over the code of receiving file and found the thread stopped when receiving file, which is really a stupid mistake.

I set FileTransferNegotiator. IBB_only true. It works fine.

But there is another problem with socks5. I Google it and found it’s a common problem that the host is 127.0.0.1. But I’m not sure how to modify it. I’m using windows and compiled library. My domain is strong.

Is " IPAddress localhost" the right form of code that I should add to C://windows/system32/drivers/etc/hosts ?

Thank you,

Strong

You are welcome.

Localhost is so painful while testing so I’ve never test it. try to use another computer on your network as a server:

  1. Use another computer as a server.

  2. Set a fixed IP for the server. e.g (192.168.10.50)

  3. Disable firewall on the server.

  4. Turn on network discovery on the server.

  5. Connect to the server with the new IP 192.168.10.50:5222

There is an open issue SMACK-561

about it.

I’m very sorry, I forgot that I experience this problem before, even with a public IP.

Thank you. You have helped me a lot. I will try socks5 later.