Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #20192
| Received | by 10.224.173.202 with SMTP id q10mr9465384qaz.3.1355070413102; Sun, 09 Dec 2012 08:26:53 -0800 (PST) |
|---|---|
| Received | by 10.49.84.167 with SMTP id a7mr2522586qez.11.1355070413086; Sun, 09 Dec 2012 08:26:53 -0800 (PST) |
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!c8no2163395qao.0!news-out.google.com!gf5ni48613681qab.0!nntp.google.com!c8no2206081qao.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.java.programmer |
| Date | Sun, 9 Dec 2012 08:26:53 -0800 (PST) |
| In-Reply-To | <ka2c97$ag8$1@dont-email.me> |
| 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 |
| References | <9a8716eb-5842-45d8-b62e-193122cd863e@googlegroups.com> <ka2c97$ag8$1@dont-email.me> |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <27f309f0-5118-4fa8-8b26-858d19af27de@googlegroups.com> (permalink) |
| Subject | Re: Java (android) socket reconnection |
| From | artik <olsztyn.arti@gmail.com> |
| Injection-Date | Sun, 09 Dec 2012 16:26:53 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-Received-Bytes | 3935 |
| Xref | csiph.com comp.lang.java.programmer:20192 |
Show key headers only | View raw
My whole code looks like. I have to use to threads due to manage obtaining data from server too. Could You look at it in free time and tell me what's wrong?
Regards
Artik
public class MainActivity extends Activity {
Button button;
TextView textview1;
public Socket sock = null;
private BufferedWriter out;
private Thread thrd2, thrd1;
private static String address = "xyz";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
textview1 = (TextView) findViewById(R.id.editText1);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
thrd2 = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
try {
if (out != 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 {
stopSocket();
Thread.sleep(1000);
} catch (InterruptedException e1) {
e.printStackTrace();
}
}
}
}
});
thrd1 = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
if (sock == null)
try {
sock = new Socket();
sock.connect(new InetSocketAddress(address,
5000), 1000);
sock.setSoLinger(true, 1);
sock.setTcpNoDelay(true);
out = new BufferedWriter(
new OutputStreamWriter(sock
.getOutputStream()));
if ((thrd2 != null) && (!thrd2.isAlive()))
thrd2.start();
} catch (UnknownHostException e) {
stopSocket();
} catch (IOException e) {
stopSocket();
}
}
;
}
});
if ((thrd1 != null) && (!thrd1.isAlive()))
thrd1.start();
};
});
}
public void stopSocket() {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
Log.d("-----", e.getMessage() + " " + e.getCause());
}
try {
if (sock != null) {
sock.close();
}
sock = null;
} catch (IOException e) {
Log.d("-----", e.getMessage() + " " + e.getCause());
sock = null;
out = null;
}
}
}
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-09 07:06 -0800
Re: Java (android) socket reconnection Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-12-09 16:55 +0100
Re: Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-09 08:26 -0800
Re: Java (android) socket reconnection Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-12-09 19:07 +0100
Re: Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-09 12:36 -0800
Re: Java (android) socket reconnection Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-12-09 23:07 +0100
Re: Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-09 14:45 -0800
Re: Java (android) socket reconnection Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-12-10 00:47 +0100
Re: Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-10 13:09 -0800
Re: Java (android) socket reconnection Lew <lewbloch@gmail.com> - 2012-12-10 13:52 -0800
Re: Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-09 08:34 -0800
Re: Java (android) socket reconnection Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-09 11:09 -0500
Re: Java (android) socket reconnection artik <olsztyn.arti@gmail.com> - 2012-12-09 08:33 -0800
csiph-web