ankursaxena
Programmer
Hi, I have a Vector of sockets, every so often I send packets to all the sockets in the vector using the following code. Before sending I check if the socket is connected, if it is cont sending, else close the socket, input/output streams and remove it from the vector, but it doesnt seem to work. Here you go...
Code:
AppConstants.traceln("Sockets to send to: " + status_manager.rt_comm.outStreams.size());
ListIterator iter = status_manager.rt_comm.outStreams.listIterator();
ListIterator iter_sock = status_manager.rt_comm.clientSockets.listIterator();
ListIterator iter_in = status_manager.rt_comm.inStreams.listIterator();
int o_count = 0;
while (iter.hasNext())
{
try
{
Socket sock = (Socket)iter_sock.next();
ObjectOutputStream oos = (ObjectOutputStream)iter.next();
ObjectInputStream ois = (ObjectInputStream)iter_in.next();
if(sock.isConnected())
{
oos.writeObject((id == 0? "PTxAS_1" : "PTxAS_2"));
oos.writeObject(ids);
oos.writeObject(vals);
oos.flush();
AppConstants.traceln("Writting to " + o_count + " socket");
}
else
{
//remove sock and steam
oos.close();
ois.close();
sock.close();
status_manager.rt_comm.inStreams.remove(ois);
status_manager.rt_comm.outStreams.remove(oos);
status_manager.rt_comm.clientSockets.remove(sock);
AppConstants.traceln("Removed socket/IO-Streams from vectors because conn was closed by client");
}
o_count++;
}
catch(Exception exc)
{
AppConstants.traceln("Exception occured while sending to socket " + o_count);
exc.printStackTrace(AppConstants.log_file);
}
Is it that when I get the object from the vector it really is a copy and when I close that, it doesnt close the actual connection?
I keep getting this error..
3213: Sockets to send to: 3
3214: Exception occured while sending to socket 0
java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.ibjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.ibjectOutputStream$BlockDataOutputStream.writeByte(Unknown Source)
at java.ibjectOutputStream.writeFatalException(Unknown Source)
at java.ibjectOutputStream.writeObject(Unknown Source)
at PTXConnThread.run(PTXStatus.java:292)
at java.lang.Thread.run(Unknown Source)
3215: Exception occured while sending to socket 0
java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.ibjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.ibjectOutputStream$BlockDataOutputStream.writeByte(Unknown Source)
at java.ibjectOutputStream.writeFatalException(Unknown Source)
at java.ibjectOutputStream.writeObject(Unknown Source)
at PTXConnThread.run(PTXStatus.java:292)
at java.lang.Thread.run(Unknown Source)
3216: Exception occured while sending to socket 0
I had the code in place, where in the excpetion I would close everything, but that didnt affect, so I removed it and really the time when the exception is thrown the connection.isconnected() should return a false, next time and automatically remove it.
Dont know whats happening here, thanks
Thanks for any help.
Ankur