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

Discussion in 'Java' started by mirton, Feb 26, 2005.

  1. mirton

    mirton Guest

    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.. :)
     
    mirton, Feb 26, 2005
    #1
    1. Advertisements

  2. mirton

    mirton Guest


    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.
     
    mirton, Mar 1, 2005
    #2
    1. Advertisements

  3. mirton

    Esmond Pitt Guest

    '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.
     
    Esmond Pitt, Mar 2, 2005
    #3
  4. mirton

    mirton Guest

    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.
     
    mirton, Mar 3, 2005
    #4
  5. No uncertainty on this end. It's definitely the case that you have an
    overdeveloped sense of entitlement. *plonk*
     
    John C. Bollinger, Mar 3, 2005
    #5
  6. mirton

    Esmond Pitt Guest

    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.
     
    Esmond Pitt, Mar 3, 2005
    #6
  7. mirton

    Mirton

    Joined:
    Dec 15, 2017
    Messages:
    1
    Likes Received:
    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 :)
     
    Mirton, Dec 15, 2017
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.