[android-developers] Re: TCP problem - hangs when reading server's response
Hi, I have only worked with doing sockets more manually, using bytebuffer(http://onjava.com/pub/a/onjava/2002/09/04/nio.html and ignore non-blocking for now) and then parsing what I need (I guess I still like the C-way of using sockets), so I am not an expert in this field. Anyway, have you checked that the server actually receives your message (from line#4) and generates the correct response? Can you see the response arriving on your client device/machine? Based on my understanding of readLine, it will block until \n, \r og \r\n is read and then return the complete string. Can you modify the server application to include for example \n at the end of the packet (just for testing)? -Kristian -- 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
[android-developers] Re: TCP problem - hangs when reading server's response
The following should work (taken from Jt.InputStream component) It uses read and a buffer. It also checks for maxLenght (which you probably don't need). Keep in mind that read may return less bytes than requested. At least something like this should give you an idea of what is happening (exceptions, log messages, etc) try { handleTrace (readStream:available: + istream.available ()); while ((len = istream.read (buf, 0, bufferSize)) 0) { handleTrace (readStream: + len); buffer1 = new byte [len]; i = 0; while (i len) { buffer1[i] = buf[i]; i++; } buff.setBuffer (buffer1); if (readStream) { count += len; if (count maxLength) { handleError (maxLenth exceeded: + maxLength); return (null); } if (sbuffer == null) sbuffer = new StringBuffer (); sbuffer.append(new String (buffer1)); } } } catch (Exception e) { handleException (e); return (null); } return (sbuffer!=null?sbuffer.toString():null); On Nov 13, 9:11 pm, bobetko bobe...@gmail.com wrote: I am trying to write client for Android which is supposed to communicate with PC server application on local network. Server app is written by my friend in C#. Currently there is an iPhone app that is using this server application with no problems. I have very simple code for TCP client: 1. Socket s = new Socket(server, port); 2. OutputStream out = s.getOutputStream(); 3. PrintWriter output = new PrintWriter(out); 4. output.println(ACTION=NextVALUE=0); 5. BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream())); 6. String st = input.readLine(); I went through many TCP implementation examples, and they are all similar. Pretty much like my code above. My app freezes on line 6 when I try to read response from the server. It doesn't cause any errors (no exceptions), nothing shows in debugger, just timeout error after awhile. Server is supposed to return string OK after executing my action in line 4. I don't understand why this code hangs. Input is not NULL (I've checked it). I would expect some exception to be thrown or simply empty string to be returned. So? What am I missing? Could it be problem with some special characters that server app is sending and android can't handle that? Do I need any special permission in my manifest? I am positive that I have correct IP address and correct port number. I can see that on server application running on my PC. Thanks. -- 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
Re: [android-developers] Re: TCP problem - hangs when reading server's response
I suppose I'm a bit rusty on socket networking, since I've been using mostly HTTP, but let's give it a try. It would be very helpful to determine if the server is actually sending a response. But some possible problems: After the output.println, perhaps it's necessary to flush the output buffer. -- 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
Re: [android-developers] Re: TCP problem - hangs when reading server's response
Another way to debug this is: Run your code in the emulator, and run a tcp/ip protocol sniffer on the host computer at the same time (WireShark, etc.) See if any traffic is getting to the server at all, and what it sends to the client. -- Kostya Vasilyev -- http://kmansoft.wordpress.com 14.11.2010 21:13 пользователь Frank Weiss fewe...@gmail.com написал: I suppose I'm a bit rusty on socket networking, since I've been using mostly HTTP, but let's give it a try. It would be very helpful to determine if the server is actually sending a response. But some possible problems: After the output.println, perhaps it's necessary to flush the output buffer. -- You received this message because you are subscribed to the Google Groups Android Develop... -- 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
[android-developers] Re: TCP problem - hangs when reading server's response
The server app was written for iPhone witch maybe handle some things differently. As a message terminator server app author used OK sequence (I will ask him why), so I definitely couldn't use readLine(). It turned out there was more then one problem. In line 4, I had to replace all characters with amp; but that alone was not the solution. It seems that PrintWriter adds something to OutputStream, probably \n character or something similar. Server didn't like that and my actions were ignored. I tried PrintStream instead of PrintWriter and that worked perfectly. So, now I am good. Thanks all. -- 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
[android-developers] Re: TCP problem - hangs when reading server's response
And one more thing, Encoding.UTF8 is used. Not sure if that might be important. -- 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
Re: [android-developers] Re: TCP problem - hangs when reading server's response
Well, you're using ReadLine() are you sure the server is actually sending lines, as in ending with '\n'? You might want to try using the InputStream directly with read(). On Sat, Nov 13, 2010 at 6:18 PM, bobetko bobe...@gmail.com wrote: And one more thing, Encoding.UTF8 is used. Not sure if that might be important. -- 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 -- ~ Jeremiah:9:23-24 Android 2D MMORPG: http://developingthedream.blogspot.com/, http://www.youtube.com/user/revoltingx -- 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
[android-developers] Re: TCP problem - hangs when reading server's response
I tried: input.read(); I also tried to read Reader the same way: Reader in = new InputStreamReader(s.getInputStream()); in.read(); Out of desperation I tried to recompile to 2.2 (now is 2.1). -- 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