An existing connection was forcibly closed -- Unnable to find reason

M

mirton

Hi,

I have a server with selectors and a Midlet application.
I connect to the server from the midlet without any problem, but when I
turn of the Midlet device then an Exception is thrown
(java.io.IOException: An existing connection was forcibly closed by the
remote host).

--(at the bottom are the links to the files with the entire code)--

This is the stack trace of the exception:
java.io.IOException: An existing connection was forcibly closed by the
remote ho
st
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(Unknown Source)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
at sun.nio.ch.IOUtil.read(Unknown Source)
at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
at ServerThread.doRequest(ServerThread.java:76)
at ServerThread.requests(ServerThread.java:57)
at ServerThread.<init>(ServerThread.java:34)
at DBServer.main(DBServer.java:252)


This is the code part from the Midlet (the class extends Thread):
And this is the only place where the connection is created and no
matter what, it is closed after each operation.
public void run(){
try{
conn =
Connector.open("socket://"+host+":"+port,Connector.READ_WRITE );
out=new DataOutputStream(((OutputConnection)
conn).openOutputStream());
in=new DataInputStream(((InputConnection)
conn).openInputStream());
switch(action){
case CONNECT:
form.login();
conn.close();
System.out.println("CONNECT");
return;
case LOGIN:
login(form.loginTxt.getString(),form.passwordTxt.getString());
conn.close();
return;
case PAY:
pay();
conn.close();
return;
}

}
catch(Exception e) {
System.out.println("RUN THREAD: "+e);

if(e.toString().equals("javax.microedition.io.ConnectionNotFoundException:
TCP open")) form.noConnect();

}

}

And this is the code part where the exception occurs in the server:
try {
bb.clear();
reading:
System.out.println("IS CONNECTED: "+socket.isConnected());
while (true) {
---- int i = socket.read(bb);-------- (Exception)
if (i > 0) {
bb.flip();
CharBuffer cb = charset.decode(bb);
while (cb.hasRemaining()) {
char c = cb.get();
if (c == '\r' || c == '\n') break reading;
sb.append(c);
}
}
}

}
catch (Exception exx) {
System.out.println("Exception - reading from channel"+exx);
exx.printStackTrace();
}

Maybe this exception is thrown earlier but not shown, maybe because
most network operations in the server are non-blocking.

Links as text:
http://mirton.homeftp.net/ServerThread.txt
http://mirton.homeftp.net/Client.txt
http://mirton.homeftp.net/DBServer.txt -- (this class creates the
ServerThread object)


or as Java files:
http://mirton.homeftp.net/ServerThread.java
http://mirton.homeftp.net/Client.java
http://mirton.homeftp.net/DBServer.java

If the above links dont work then try the ones below...

Links as text:
http://www.pjwstk.edu.pl/~s3288/Client.txt
http://www.pjwstk.edu.pl/~s3288/ServerThread.txt
http://www.pjwstk.edu.pl/~s3288/DBServer.txt

or as Java files:
http://www.pjwstk.edu.pl/~s3288/ServerThread.java
http://www.pjwstk.edu.pl/~s3288/Client.java
http://www.pjwstk.edu.pl/~s3288/DBServer.java

Any help would be helpful.. :)
 
M

mirton

Hi,

I have a server with selectors and a Midlet application.
I connect to the server from the midlet without any problem, but when I
turn of the Midlet device then an Exception is thrown
(java.io.IOException: An existing connection was forcibly closed by the
remote host).

--(at the bottom are the links to the files with the entire code)--

This is the stack trace of the exception:
java.io.IOException: An existing connection was forcibly closed by the
remote ho
st
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(Unknown Source)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
at sun.nio.ch.IOUtil.read(Unknown Source)
at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
at ServerThread.doRequest(ServerThread.java:76)
at ServerThread.requests(ServerThread.java:57)
at ServerThread.<init>(ServerThread.java:34)
at DBServer.main(DBServer.java:252)


This is the code part from the Midlet (the class extends Thread):
And this is the only place where the connection is created and no
matter what, it is closed after each operation.
public void run(){
try{
conn =
Connector.open("socket://"+host+":"+port,Connector.READ_WRITE );
out=new DataOutputStream(((OutputConnection)
conn).openOutputStream());
in=new DataInputStream(((InputConnection)
conn).openInputStream());
switch(action){
case CONNECT:
form.login();
conn.close();
System.out.println("CONNECT");
return;
case LOGIN:
login(form.loginTxt.getString(),form.passwordTxt.getString());
conn.close();
return;
case PAY:
pay();
conn.close();
return;
}

}
catch(Exception e) {
System.out.println("RUN THREAD: "+e);

if(e.toString().equals("javax.microedition.io.ConnectionNotFoundException:
TCP open")) form.noConnect();

}

}

And this is the code part where the exception occurs in the server:
try {
bb.clear();
reading:
System.out.println("IS CONNECTED: "+socket.isConnected());
while (true) {
---- int i = socket.read(bb);-------- (Exception)
if (i > 0) {
bb.flip();
CharBuffer cb = charset.decode(bb);
while (cb.hasRemaining()) {
char c = cb.get();
if (c == '\r' || c == '\n') break reading;
sb.append(c);
}
}
}

}
catch (Exception exx) {
System.out.println("Exception - reading from channel"+exx);
exx.printStackTrace();
}

Maybe this exception is thrown earlier but not shown, maybe because
most network operations in the server are non-blocking.

Links as text:
http://mirton.homeftp.net/ServerThread.txt
http://mirton.homeftp.net/Client.txt
http://mirton.homeftp.net/DBServer.txt -- (this class creates the
ServerThread object)


or as Java files:
http://mirton.homeftp.net/ServerThread.java
http://mirton.homeftp.net/Client.java
http://mirton.homeftp.net/DBServer.java

If the above links dont work then try the ones below...

Links as text:
http://www.pjwstk.edu.pl/~s3288/Client.txt
http://www.pjwstk.edu.pl/~s3288/ServerThread.txt
http://www.pjwstk.edu.pl/~s3288/DBServer.txt

or as Java files:
http://www.pjwstk.edu.pl/~s3288/ServerThread.java
http://www.pjwstk.edu.pl/~s3288/Client.java
http://www.pjwstk.edu.pl/~s3288/DBServer.java

Any help would be helpful.. :)


OK..........
It's either none of you know the answer to this problem or you just
don't want to write the answer, Thanx a lot..

Assholes.
 
E

Esmond Pitt

'Assholes' yourself, but among other things you seem to have a
misconception about Socket.isConnected(). This only tells you whether
you have connected your end of the socket in the current JVM via a
constructor or Socket.connect(), not whether the other end is still there.
 
M

mirton

Esmond said:
'Assholes' yourself, but among other things you seem to have a
misconception about Socket.isConnected(). This only tells you whether
you have connected your end of the socket in the current JVM via a
constructor or Socket.connect(), not whether the other end is still
there.

Whatever...
I called the Socket.isConnected() method while still testing it, trying
to find the reason, and I didn't know what exactly it was for,
so Thank you.
 
J

John C. Bollinger

OK..........
It's either none of you know the answer to this problem or you just
don't want to write the answer, Thanx a lot..

Assholes.

No uncertainty on this end. It's definitely the case that you have an
overdeveloped sense of entitlement. *plonk*
 
E

Esmond Pitt

I have a server with selectors and a Midlet application.
I connect to the server from the midlet without any problem, but when I
turn of the Midlet device then an Exception is thrown
(java.io.IOException: An existing connection was forcibly closed by the
remote host).

If this means that you have turned off the Midlet device and the server
then gets an exception reading from the connection, then TCP/IP is
working as designed, or indeed a little better. Otherwise you need to
state your problem more clearly.
 
Joined
Dec 15, 2017
Messages
1
Reaction score
0
OP here.
I just found this post and I feel embarrassed at what an asshole I've been back in 2005 :oops:
I would like to apologise (although belated by 12 years) to the forum members and posters in this thread and also a thank you to Esmond Pitt for trying to help, even through I didn't back then, I appreciate it now :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top