You usually need different threads to read and write, prevent blocking the UI thread.
On Jul 14, 12:49 am, Victor Lin <borns...@gmail.com> wrote: > Well, but will the read of InputStream blocked when there is no > incoming data? > I mean... for example: > > I run the sock in specific thread, because those methods' call will > blocked. The code might like this: > > /// This is the socket thread for checking data > while not stop: > data = read(); // if no incoming data, we will blocked here > > The client program needs to check the data from sever, so it run a > loop to read data. And how about outgoing data? Can I send out going > data from another thread? like this. > > // this is called from UI thread > void onClick() { > outputStream.write("hello baby!"); > > } > > On 7月14日, 下午2時21分, James <030440...@163.com> wrote: > > > > > Hi Victor Lin: > > I don't know why could your problem happen,As One socket have both > > inputstream and outputstreams,They are responsible for read and write; > > And as far as i call tell,Socket is not android-specified,Hope this > > helps! > > > Bgs! > > James > > > On Jul 14, 12:57 pm, Victor Lin <borns...@gmail.com> wrote: > > > > Hi, > > > > I am developing a program that needs async socket. I am new to Android > > > and Java. As I can see in the document, to write network program, I > > > got Socket and SocketChannel, but it seems they are all sync socket, > > > it means, I need an extra thread to call blocking read, write... and > > > etc. Here comes the problem, my program is a chat room, the > > > communicate is not linear, I have no idea when the server will send a > > > message to me, if I read in that thread to wait message from server, > > > but the user want to send message to server, the reading function > > > blocked forever, if there is no incoming data, then the pending > > > outgoing message will not be sent. That's why I need async socket. So > > > my question is: How to write async socket under Android environment? > > > > Thanks. > > > Victor Lin. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---