powered by Jive Software

How to catch packetreader throw exceptions

hi,

my client sometimes raise SocketException and my client jabber thread dead.

i have connectionClosed() and connectionClosedOnError. My code look like

private void addListener() {

PacketFilter filter = new PacketTypeFilter(Message.class);

PacketCollector myCollector = conn.createPacketCollector(filter);

PacketListener listener = new PacketListener() {

public void processPacket(Packet p) {

newIncomingMessage((Message) p);

}

};

conn.addPacketListener(listener, filter);

conn.addConnectionListener(new ConnectionListener() {

public void connectionClosed() {

autoLoginServer();

cat.debug(“connectionClosed : restart connection”);

}

public void connectionClosedOnError(Exception e) {

autoLoginServer();

cat.debug(e.getMessage()+ " connectionClosedOnError : restart connection");

}

});

}

but, sometimes raise SocketException and my code not catched this error. Smack writen console :

java.net.SocketException: Connection reset

at java.net.SocketInputStream.read(Unknown Source)

at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)

at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)

at sun.nio.cs.StreamDecoder.read(Unknown Source)

at java.io.InputStreamReader.read(Unknown Source)

at java.io.BufferedReader.fill(Unknown Source)

at java.io.BufferedReader.read1(Unknown Source)

at java.io.BufferedReader.read(Unknown Source)

at org.xmlpull.mxp1.MXParser.fillBuf(MXParser.java:2971)

at org.xmlpull.mxp1.MXParser.more(MXParser.java:3025)

at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1144)

at org.xmlpull.mxp1.MXParser.next(MXParser.java:1093)

at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:377)

at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43)

at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:63)

how to catch this exception and recover my connection.

Thnx advence.

Message was edited by: y0rk

The code

PacketFilter filter = new PacketTypeFilter(Message.class);

/code

and

conn.addPacketListener(listener, filter);

/code

Wont work if you want to listen out for Connection Events.

You are adding a filter to your Listener to say listen out only for Packets of type Message. A Connection closed event afaik isnt a packet of Type Message.

XMPPConnection has the method

#addConnectionListener(ConnectionListener);

So there is no need to create a filter type. Simply call

connection.addConnectionListener(new ConnectionListener());

/code

hth

Jon

Message was edited by: JonWright

this sound goods like but howto process incoming packets?

where is the processPacket in your code.

PacketListener listener = new PacketListener() {

public void processPacket(Packet p) {

newIncomingMessage((Message) p);

}

};

thnx advance

Something like this

class ConnectionListener {

void connectionClosed() {

// do something bout the connection being closed

}

void connectionClosedOnError(Exception e) {

// do something bout the connection being closed due to an error

}

}

/code

Now you add this Listener to your XMPPConnection

XMPPConnection conn

conn.addConnectionListener(new ConnectionListener());

/code

Your connection should now react to the close events that may happen to it allowing you to perform whatever logic you want to implement

hth

Jon