Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2354

How to do perfect socket reconnection

Received by 10.224.105.205 with SMTP id u13mr9363998qao.6.1355065880908; Sun, 09 Dec 2012 07:11:20 -0800 (PST)
Received by 10.49.94.99 with SMTP id db3mr2439344qeb.2.1355065880892; Sun, 09 Dec 2012 07:11:20 -0800 (PST)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!c8no2197341qao.0!news-out.google.com!gf5ni48613681qab.0!nntp.google.com!c8no2197338qao.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.java.help
Date Sun, 9 Dec 2012 07:11:20 -0800 (PST)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=88.156.28.232; posting-account=FYIyEAoAAAANuhVTqhCgp1nBx7dpZ-nl
NNTP-Posting-Host 88.156.28.232
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <b8d8763f-b003-4d95-b5d1-26c2aaf2184c@googlegroups.com> (permalink)
Subject How to do perfect socket reconnection
From Artik <olsztyn.arti@gmail.com>
Injection-Date Sun, 09 Dec 2012 15:11:20 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.java.help:2354

Show key headers only | View raw


Hi,
I appologize - I asked about my problem in other group too but unfortunately I haven't got answer for my question:
"How to correctly support reconnecting client sockets?"
I have one thread (it's name thrd1) for controlling connection (and reconnection if is it needed) and thread (it's name thrd2) for cyclical sending data to server (it is written in Delhi).
Some strange happends when I stop server and start it again after some time.
Server receives information that my client (java-android) wants to connect several times (it depends on time - how long server doesn't respond) - but after these attempts connection back to almost normal state. 
Problem is in these too many attempts in connection and in this that when time not responding of server is enough long my application crushes.

In my opinion problem is in "not-cleaning" socket after my stop method? How to do it perfectly?

Could somebody help me to resolve my huge problem?
Regards
Artik

If I can I put my example code:
thrd2 = new Thread(new Runnable() {
public void run() {
    while (!Thread.interrupted()) {
        try {
            if (sock != null) {
                out.write("TEST DATA\n");
                out.flush();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } 
            //if sock is null wait 300ms
            else {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                     e.printStackTrace();
                }
            }
        } catch (IOException e) {
            try {
                sock=null;
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                 e.printStackTrace();
            }
        }
    }
}


and


thrd1 = new Thread(new Runnable() {
public void run() {
    while (!Thread.interrupted()) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e1) {

        }
        if (sock==null)
        try {
            sock = new Socket();
            sock.connect(new InetSocketAddress(
                    address, 5000), 300);
            r = new BufferedReader(new InputStreamReader(
                    sock.getInputStream()));
            out = new BufferedWriter(
                    new OutputStreamWriter(sock
                            .getOutputStream()));
            if ((thrd2!=null)&&(!thrd2.isAlive()))
            thrd2.start();
        } catch (UnknownHostException e) {
             e.printStackTrace();

        } catch (IOException e) {
             e.printStackTrace();
        }
    }
    ;
}
});
if ((thrd1!=null)&&(!thrd1.isAlive())) thrd1.start();

Back to comp.lang.java.help | Previous | NextNext in thread | Find similar | Unroll thread


Thread

How to do perfect socket reconnection Artik <olsztyn.arti@gmail.com> - 2012-12-09 07:11 -0800
  Re: How to do perfect socket reconnection Lew <lewbloch@gmail.com> - 2012-12-09 13:02 -0800
    Re: How to do perfect socket reconnection Artik <olsztyn.arti@gmail.com> - 2012-12-09 14:58 -0800

csiph-web