Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vector trouble 1

Status
Not open for further replies.

ankursaxena

Programmer
Sep 7, 2001
133
0
0
US


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.io_ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.io_ObjectOutputStream$BlockDataOutputStream.writeByte(Unknown Source)
at java.io_ObjectOutputStream.writeFatalException(Unknown Source)
at java.io_ObjectOutputStream.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.io_ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.io_ObjectOutputStream$BlockDataOutputStream.writeByte(Unknown Source)
at java.io_ObjectOutputStream.writeFatalException(Unknown Source)
at java.io_ObjectOutputStream.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
 
Whats the difference between remove and removeELemet??

they seem to be one and the same
 
I admittedly have not read your entire [long] post, but one thing struck me from the first line. Why, oh why, are you storing socket objects in a vector ? This is just asking for a nightmare. I have no doubt you have a broken pipe.

When working with sockets, IMO, the golden rule is :
open socket, send/recv data, close socket.

Simple as that. Do NOT keep sockets open unecesssarily. This is just bad programming technique.

--------------------------------------------------
Free Database Connection Pooling Software
 
See I have not seen much that you open a socket and close it after sending and recving information. Here I have to keep the connection alive because I have to non-stop send or recv information, it has to be open all the time, I dont know when I will recv data and I dont know when I will have to send data.

plus I dont know how many client will be connecting to me, so what do I do, I cant close the connection plus I dont know how many will connect, so where else will I store the connections?

But thanks for looking into it.

Ankur
 
If you are recieving data, you should use a ServerSocket object, and so listen constantly on a port.

If you are sending data, you should open a Socket object, send the data, read any response, and then close it.

--------------------------------------------------
Free Database Connection Pooling Software
 
I think this has nothign to do with a Vector problem but anyway:

- remove and removeElement methods have the same functionality. The remove one is just there to implement the List interface.

- Vector and ArrayList offer the same functionality, but ArrayList is not synchronized. So if you're not accesing concurrently, you should use ArrayList.

Anyway, instead of a Vector I think you need a pool here.

Cheers,

Dian
 
Giving timw a star becuase I had fallen by the wayside in teaching myself Java, by following his link it has re-ignited my ambition to do some more stuff.


[blue]Arguably the best cat skinner around ! [/blue]

Cheers
Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top