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


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

How to do perfect socket reconnection

Started byArtik <olsztyn.arti@gmail.com>
First post2012-12-09 07:11 -0800
Last post2012-12-09 14:58 -0800
Articles 3 — 2 participants

Back to article view | Back to comp.lang.java.help


Contents

  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

#2354 — How to do perfect socket reconnection

FromArtik <olsztyn.arti@gmail.com>
Date2012-12-09 07:11 -0800
SubjectHow to do perfect socket reconnection
Message-ID<b8d8763f-b003-4d95-b5d1-26c2aaf2184c@googlegroups.com>
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();

[toc] | [next] | [standalone]


#2355

FromLew <lewbloch@gmail.com>
Date2012-12-09 13:02 -0800
Message-ID<f607f76a-609b-4bc5-bc62-1ba85c6f0c2d@googlegroups.com>
In reply to#2354
On Sunday, December 9, 2012 7:11:20 AM UTC-8, Artik wrote:
> 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?"
> 

You posted on clj.programmer at 15:06Z, then here at 15:11 complaining you haven't yet seen an answer?

What you did is post the same question independently on multiple newsgroups, a practice known as
multi-posting and a violation of newsgroup etiquette.

Also, do not expect answers within five minutes, as a rule, or ever, on occasion. Learn to wait.

Plus, also, in addition, you got lots of answers on clj.programmer.

To which I'll add - don't use '(!Thread.interrupted())' to control a loop, and do study the 
Java tutorials and other material on multi-threaded programming.

--
Lew

[toc] | [prev] | [next] | [standalone]


#2356

FromArtik <olsztyn.arti@gmail.com>
Date2012-12-09 14:58 -0800
Message-ID<9528086d-ddcc-43e3-a595-328993b679e3@googlegroups.com>
In reply to#2355
Thank you.
You has right in all points. I'm sorry.
I try to resolve my problem in so long time and I've tried to find it on newsgroups today first day (before - two weeks ago I searched elsewhere) but the multiplication very similar questions is result of my newsgroup client - It told me somthing like this: "your post didn't send" and  and my fatigue.  Therefore i writo it again to different group.
I'm ashamed and I will try be more patient and careful in the future

~Artik

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web