TCP problem - hangs when reading server's response
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=Next&VALUE=0&");
5. BufferedReader input = 开发者_开发技巧new BufferedReader(new nputStreamReader(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 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.
String st = input.readLine(); Does this command make the program wait until something is being read or it just takes straight whatever is in the buffer. I guess you will need to check continuosly if something came from the server. I mean, you need one infinite loop, something like
While (True){
st = input.readLine();
This will check if anything came the whole time. If u dont use it and if String st = input.readLine(); command doesnt make the program wait, then your code will end without taking anything.
You of course need internet permission in the manifest.
If it were me I'd add instrumentation to the server so that it can tell you when there's been a connection, when that connection has received data, and when a reply is being sent. Or else run tcpdump on the server.
You could also temporarily try grabbing the input character by character rather than a whole line at a time.
Try to figure out how far the "conversation" is progressing so you can figure out where it is really getting stuck
精彩评论