is it possible to gracefully interrupt network transaction?
Hi,
I have this code
try{
ObjectInputStream fromServer;
ObjectOutputStream toServer;
Wrapper.clear();
toServer.writeUnshared(Wrapper);
toServer.flush();
try{
obj = fromServer.readObject();
if (obj !=null)
{
if (obj.getClass() == Hashtable.class)
{
tab = (Hashtable)obj;
}
}
}catch (EOFException eofx){
System.out.println("Clent EOF @ object input stream!");
}
return tab;
}catch(Exception e){
e.printStackTrace();
}
fromServer waits to read an object. Can I use socket object in way that the transaction would end without problems?
PS: this method is a part of a thread object. If I nullify or interrupt the thread what will happen to the transaction?
[1412 byte] By [
xpantaa] at [2007-11-27 10:35:24]

# 1
There are a few problems with your question.
1) You haven't shown us what you are doing with the Socket. For one thing you should be calling setSoTimeout on the Socket before you read with an appropriate value
2) This code appears to be hackery that you cut and pasted out of your code but makes no sense as is. For example you declare your input and output stream variables, never initialize them and then use them. So who knows what's really going on there...
3) You keep using the word "transaction" but I for one don't understand the context of your question.
4) "nullifying the thread" is a bad idea. It will not do you any good that is for sure.
# 2
> There are a few problems with your question.
>
> 1) You haven't shown us what you are doing with the
> Socket. For one thing you should be calling
> setSoTimeout on the Socket before you read with an
> appropriate value
following is part of the "connect" and "run" methods of my NetManager object
public void connect() throws Exception
{
if (socket == null){
try{
System.out.println("opening connection...to "+host+" at port "+port);
socket = new Socket(host, port);
toServer = new ObjectOutputStream(socket.getOutputStream());
fromServer = new ObjectInputStream(socket.getInputStream());
System.out.println("Connection established!");
fireClientEvent(new ClientEvent(this, ClientEvent.CONNECTION_ESTABLISHED, ""));
this.connected = true;
}catch(IOException e){
try{
if (socket != null)
socket.close();
System.out.println("Error while connecting... Server does not exist!");
this.connected = false;
fireClientEvent(new ClientEvent(this, ClientEvent.SERVER_DOES_NOT_EXIST, getHost()+":"+getPort()));
}catch(IOException x){
System.out.println("I/O ERROR @ CLIENT CONNECT!");
removeAllListeners();
x.printStackTrace();
}
}
}
else
{
fireClientEvent(new ClientEvent(this, ClientEvent.GENERIC_EVENT, "already connected at: "+getHost()+":"+getPort()));
}
}
public void run()
{
try{
System.out.println("starting NetManager Thread");
connect();
}catch(Exception we){
System.out.println(we.getMessage());
}
}
> 3) You keep using the word "transaction" but I for
> one don't understand the context of your question.
By the word "transaction" I am trying to imply the send and receive of data. toServer object sends data to server. fromServer object waits and receives data from the server. This whole process I am calling "transaction"
The reason I didn't attached all this code from the beginning is the fact that the problem is not while connecting to the server. The problem is while the Client waits for an answer. This is what I must end if it takes too long and not the connection.