powered by Jive Software

How to send multiple messages from reading a file

i wrote a program which reads messages from file line by line and sends messages.

suppose there r 5 lines.iam reading first line and sending.but response is coming 5 times.

for second message also response is coming 5 times please check this ones.i want to store the message which i send and response for that messages one by one in file

import java.util.*;

import java.io.*;

import org.jivesoftware.smack.*;

import org.jivesoftware.smack.filter.*;

import org.jivesoftware.smack.packet.*;

import org.jivesoftware.smackx.*;

import org.jivesoftware.smackx.muc.*;

import org.jivesoftware.smack.Chat;

import org.jivesoftware.smack.PacketListener;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.filter.PacketFilter;

import org.jivesoftware.smack.filter.PacketTypeFilter;

import org.jivesoftware.smack.packet.Message;

import org.jivesoftware.smack.packet.Packet;

import org.jivesoftware.smack.packet.Presence;

import javax.swing.JDialog;

//import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class Demo extends JDialog

{ //class block open

JScrollPane scrollPane = null;

JTextArea textAreaConverstation = new JTextArea();

GoogleTalkConnection conn = null;

Chat chat = null;

PacketFilter filter = null;

static String server = “jabber.org”;

static String user = “abc”;

static String password = “abc”;

 String jid = "xyz@gmail.com";

Date d= new Date();

String timestamp=(d.getYear()1900)""*(d.getMonth()1)"d.getDate()”_"*d.getHours();

String logspath=“C:
timestamp”.txt”;

String filepath=“C:
st.txt”;

FileOutputStream fos;

String st1,str1;

BufferedReader br;

int n;

Object element;

List list = new LinkedList();

public Demo()

{ // constructor block open

scrollPane = new JScrollPane(textAreaConverstation);

getContentPane().add(scrollPane);

setTitle(“Demo”);

setSize(600, 400);

setVisible(true);

try

{ //try1 open

conn = new GoogleTalkConnection();

conn.login(user, password);

textAreaConverstation.append(“Connected”);

textAreaConverstation.append("\n");

Roster roster = conn.getRoster();

if (roster.getEntryCount() > 0)

{ //if

for (Iterator i=roster.getEntries(); i.hasNext(); )

{ //for

RosterEntry re = (RosterEntry)i.next();

System.out.println(re.getName() + " ::::: " +re.getUser());

Presence presence = roster.getPresence(re.getUser());

System.out.println(presence);

} //end for

} //end if

chat.setFilteredOnThreadID(false);

chat=conn.createChat(jid);

Message message = chat.createMessage();

try

{ //try2 open

br=new BufferedReader(new InputStreamReader(new FileInputStream(filepath)));

str1=br.readLine();

//String st1;

//Message message = chat.createMessage();

//message.setBody(str1);

//chat.sendMessage(message);

while (str1!=null)

{ //while open

StringTokenizer st = new StringTokenizer(str1,"\n");

while (st.hasMoreTokens()) {

st1=(String)st.nextToken();

list.add(st1);

}

str1=br.readLine();

}

n=list.size();

for(int i=0;i<n;i++)

{

element=list.get(i);

String val=element.toString();

message = chat.createMessage();

System.out.println(val);

message.setBody(val);

chat.sendMessage(message);

chat.nextMessage();

textAreaConverstation.append("<" + user + “>”);

textAreaConverstation.append(message.getBody());

textAreaConverstation.append("\n");

try

{ //try3 open

fos=new FileOutputStream(logspath,true);

String des="<“user”>“val”\n";

byte []b=des.getBytes();

fos.write(b);

} //try3 close

catch(Exception e)

{ //catch3 open

System.out.println(e);

} //catch3 close

Thread.sleep(3000);

}

PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class), new FromContainsFilter(jid));

PacketCollector myCollector = conn.createPacketCollector(filter);

PacketListener myListener = new PacketListener()

{

public void processPacket(Packet packet)

{

Message message = (Message)packet;

String from = message.getFrom();

String body = message.getBody();

//textAreaConverstation.append("<" + message.getFrom() + "> ");

textAreaConverstation.append("<" from " > ");

textAreaConverstation.append(body);

textAreaConverstation.append("\n");

try

{ //try4 open

fos=new FileOutputStream(logspath,true);

String str2="<“from”>“body”\n";

byte []b1=str2.getBytes();

fos.write(b1);

} //try4 close

catch (Exception e1)

{ //catch4 open

e1.printStackTrace();

} //catch4 close*/

}

};

conn.addPacketListener(myListener, filter);

// Thread.sleep(2000);

} //try2 close

catch (Exception e1)

{ //catch2 open

e1.printStackTrace();

} //catch2 close

} //try1 close

catch (XMPPException e)

{ //catch1 open

e.printStackTrace();

} //catch1 close

} // Demo constructor block close

public static void main(String[] args)

{ //main block open

new Demo();

} //main block close

} //class close