multithreading - Android Service possible reuse? -


i doing project 2 people connect via wifi-hotspot (one hotspot, other 1 connects wifi) , can exchange messages (i make files later).

i created socket connection using 2 services (with 2 threads inside, 1 send info, 1 read info), 1 server , 1 client. communication should bidirectional , can started of them , here have problem.

basically in beggining, start service , start thread receive ( set action , check on method onstartcommand, know thread create). person has button, if press it, can send message. if person sends message, start service again (and set action send, can start thread send message).

the problem if start service again (to send), exception of socket being in use (already tried doing setreuseaddress(true) , not working). similar problem streams, not "related" (the outputstream 1 side not correspondent 1 inputstream other side - exchange header every time created).

how guys think solve this? put streams , socket's static, , check if created, doesn't seem consistent. if guys need, can post code, kind of long

simply check if socket using or not , close

private boolean isportinuse(string host, int port) {   // assume no connection possible.   boolean result = false;    try {     (new socket(host, port)).close();     result = true;   }   catch(socketexception e) {     // not connect.   }    return result; } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -