File Transfer and mp3 files

Hi,

I’ve written a small file transfer program. I have successfully transferred text files. But, when I try to transfer

an mp3 file, the file is transferred, but when I try to play it, my media player tells me that it cannot initialize the file.

This is the code I am using on the reciever’s side

InputStream stream;
int rc = 0;

                        stream = itf.recieveFile();
                        int len = (int)itf.getFileSize();
                        byte[] arr = new byte[len];
                        while(rc != -1)
                            rc = stream.read(arr);
                       
                        stream.close();
                        FileOutputStream  fout = new FileOutputStream("recvd.mp3");
                        for(int i = 0; i < len; i++)
                            fout.write(arr[i]);
                        fout.close();

Can anyone tell me how to correct this problem.

Cheers,

Earlence

Hi,

I solved the earlier problem. I am able to send an mp3 file using this kind of code

OutgoingFileTransfer otf;

otf.sendFile(new File(“sound_test.mp3”), “music file”);

and recv like this

final IncomingFileTransfer itf = req.accept();

itf.recieveFile(new File(“sound_test.mp3”));

But there are still problems. My applet is set up such that i have to click a button to send this file.

When i click the button for the first time, i get the msg that the stream is being negotiated and then nothing happens, however, when i click it the second time, the file transfer starts.

Can anyone tell me why this happens?

Also, I want to use the InputStream and OutputStream method to transfer the mp3 file incrementally.

Is this possible. I was thinking that If i transfer incrementally, it will be like streaming the audio from from one source to another.

This is my sender code

import java.awt.;
import java.awt.event.
;
import java.applet.;
import java.io.
;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.filetransfer.*;

/*


*/

public class XMPPTest1 extends Applet implements ActionListener
{
ConnectionConfiguration cc = null;
XMPPConnection conn = null;
Button connect, disconnect, send;
TextArea taRecv;
TextField taSend;

Button sf;
                       
public void init()
{
    connect = new Button("Connect");
    disconnect = new Button("Disconnect");
    send = new Button("Send");
    taSend = new TextField(20);
    taRecv = new TextArea("", 5, 30);
   
    sf = new Button("SendFile");
                   
    add(connect);
    add(disconnect);
    add(send);
    add(taSend);
    add(taRecv);
    add(sf);
                   
    connect.addActionListener(this);
    disconnect.addActionListener(this);
    send.addActionListener(this);
    sf.addActionListener(this);
}
   
public void actionPerformed(ActionEvent ae)
{
    String str = ae.getActionCommand();
    if(str.equals("Connect"))
        setupXMPPAndLogin();
    if(str.equals("Disconnect"))
        closeConn();
    if(str.equals("Send"))
    {
        sendMessage(taSend.getText());
        taRecv.append("me: " + taSend.getText() + "\n");
        taSend.setText("");
    }
    if(str.equals("SendFile"))
    {
        new Thread() {
            public void run() {
                sendFile();
            }
        }.start();
    }
       
}

void setupXMPPAndLogin()
{
    try {
       
        XMPPConnection.DEBUG_ENABLED = true;
               
        cc = new ConnectionConfiguration("jabber.org", 5222);
        conn = new XMPPConnection(cc);
                           
        showStatus("Attempting to connect");
        conn.connect();
        SASLAuthentication.supportSASLMechanism("PLAIN", 0);
        conn.login("user", "pass");
        showStatus("Logged In!");
       
        //setListener();
       
        showStatus("Listeners set");
       
    } catch(XMPPException xe) { System.out.println(xe.getMessage()); }
                   
}

void setListener()
{
    PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
    conn.addPacketListener(new PacketListener() {
        public void processPacket(Packet pkt)
        {
            Message m = (Message) pkt;
            if(m.getBody() != null)
            {
                taRecv.append("recv: " + m.getBody() + "\n");
            }
        }
       
    }, filter);
   
}

void sendMessage(String data)
{
    Message msg = new Message();
    msg.setType(Message.Type.chat);
    msg.setTo("recv.xmpp@jabber.org");
    msg.setBody(data);
    conn.sendPacket(msg);
    showStatus("Packet Dispatched");
}

void closeConn()
{
    showStatus("Logging Off!");
    conn.disconnect();
}

void sendFile()
{
    //sendMessage("ALOHA");
    FileTransferNegotiator.IBB_ONLY = true;
    FileTransferManager ftm = new FileTransferManager(conn);
    OutgoingFileTransfer otf = ftm.createOutgoingFileTransfer("recv.xmpp@jabber.org/Smack");
   
    try {
        /*InputStream in = new FileInputStream("sound_test.mp3");
        File f = new File("sound_test.mp3");
        int len = 0, rd = 0;
       
        len = (int) f.length();
                               
        len = in.available();
        byte[] arr = new byte[len];
        while(rd != -1)
            rd = in.read(arr);
       
        in.close();
                   
        OutputStream out;
       
        out = otf.sendFile("sound_test.mp3", (int) len, "music file");
        out.write(arr);
        out.flush();
        out.close();*/
                               
        otf.sendFile(new File("sound_test.mp3"), "music file");
                   
        showStatus("File Transfer started");
       
    }catch(Exception xe) { System.out.println(xe.getMessage()); }
}

}

This is the reciever code

import java.awt.;
import java.awt.event.
;
import java.applet.*;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.filetransfer.*;
import org.jivesoftware.smackx.filetransfer.FileTransfer.Status;

import java.io.*;

public class XMPPTest2 extends Applet implements ActionListener
{
ConnectionConfiguration cc = null;
XMPPConnection conn = null;
Button connect, disconnect, send;
TextArea taRecv;
TextField taSend;

public void init()
{
    connect = new Button("Connect");
    disconnect = new Button("Disconnect");
    send = new Button("Send");
    taSend = new TextField(20);
    taRecv = new TextArea("", 5, 30);
           
    add(connect);
    add(disconnect);
    add(send);
    add(taSend);
    add(taRecv);
           
    connect.addActionListener(this);
    disconnect.addActionListener(this);
    send.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
    String str = ae.getActionCommand();
    if(str.equals("Connect"))
        setupXMPPAndLogin();
    if(str.equals("Disconnect"))
        closeConn();
    if(str.equals("Send"))
    {
        sendMessage(taSend.getText());
        taRecv.append("me: " + taSend.getText() + "\n");
        taSend.setText("");
    }
       
}

void setupXMPPAndLogin()
{
    try {
       
        XMPPConnection.DEBUG_ENABLED = true;
       
        cc = new ConnectionConfiguration("jabber.org", 5222);
        conn = new XMPPConnection(cc);
                           
        showStatus("Attempting to connect");
        conn.connect();
        SASLAuthentication.supportSASLMechanism("PLAIN", 0);
        conn.login("user", "pass");
        showStatus("Logged In!");
       
        //setListener();
        setFileRecvListener();
       
        showStatus("Listeners set");
                   
    } catch(XMPPException xe) { System.out.println(xe.getMessage()); }
                   
}

void setListener()
{
    PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
    conn.addPacketListener(new PacketListener() {
        public void processPacket(Packet pkt)
        {
            Message m = (Message) pkt;
            if(m.getBody() != null)
            {
                taRecv.append("recv: " + m.getBody() + "\n");
            }
        }
       
    }, filter);
   
}

void sendMessage(String data)
{
    Message msg = new Message();
    msg.setType(Message.Type.chat);
    msg.setTo("earlenceferns@jabber.org");
    msg.setBody(data);
    conn.sendPacket(msg);
    showStatus("Packet Dispatched");
}

void closeConn()
{
    showStatus("Logging Off!");
    conn.disconnect();
}

void setFileRecvListener()
{
    final FileTransferManager ftm = new FileTransferManager(conn);
   
    ftm.addFileTransferListener(new FileTransferListener() {
       
        public void fileTransferRequest(FileTransferRequest req)
        {
            //sendMessage("R_ALOHA");
            showStatus("FileTransferRequest: " + req.getDescription());
            final IncomingFileTransfer itf = req.accept();
           
            new Thread() {
               
            public void run() {
           
                    try {
                       
                        itf.recieveFile(new File("sound_test.mp3"));

                    }catch(Exception xe) { System.out.println(xe.getMessage()); }
                   
               
            }
            }.start();
                       
               
               
                new Thread()
                {
                    public void run()
                    {
                        while(!itf.isDone())
                        {
                            if(itf.getStatus().equals(Status.error)) {
                                  System.out.println("ERROR!!! " + itf.getError());
                            } else {
                                  System.out.println(itf.getStatus());
                                  System.out.println(itf.getProgress());
                            }
                           
                            try {
                                sleep(1000);
                            }catch(InterruptedException ie) {}
                        }
                    }
                }.start();
                                   
           
        }
       
    });
   
}

}

Cheers,

Earlence