Server does not receive message from client

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Server does not receive message from client

Postby mike88 » Thu Nov 12, 2009 9:00 am

Hello, I have created a simple server and client both on android phone
or emulators. The code is as follows

Server:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. import java.io.BufferedReader;
  5.  
  6. import java.io.InputStreamReader;
  7.  
  8. import java.net.ServerSocket;
  9.  
  10. import java.net.Socket;
  11.  
  12.  
  13.  
  14. import android.app.Activity;
  15.  
  16. import android.os.Bundle;
  17.  
  18. import android.util.Log;
  19.  
  20. import android.widget.TextView;
  21.  
  22.  
  23.  
  24. public class AndroidServer extends Activity {
  25.  
  26.    /** Called when the activity is first created. */
  27.  
  28.        public static TextView tv;
  29.  
  30.    @Override
  31.  
  32.    public void onCreate(Bundle savedInstanceState) {
  33.  
  34.        super.onCreate(savedInstanceState);
  35.  
  36. //        setContentView(R.layout.main);
  37.  
  38.  
  39.  
  40.        tv = new TextView(this);
  41.  
  42.        tv.setText("Main - server");
  43.  
  44.        setContentView(tv);
  45.  
  46.  
  47.  
  48.        Thread sThread = new Thread(new TCPServerTest());
  49.  
  50.  
  51.  
  52.        sThread.start();
  53.  
  54.    }
  55.  
  56. }
  57.  
  58.  
  59.  
  60. class TCPServerTest implements Runnable{
  61.  
  62.  
  63.  
  64.    public static final String SERVERIP = "10.0.2.15";
  65.  
  66.    public static final int SERVERPORT = 4444;
  67.  
  68.  
  69.  
  70.    public void run() {
  71.  
  72.         try {
  73.  
  74.              Log.d("TCP", "S: Connecting...");
  75.  
  76.  
  77.  
  78.              ServerSocket serverSocket = new ServerSocket
  79.  
  80. (SERVERPORT);
  81.  
  82.              while (true) {
  83.  
  84.                 Socket client = serverSocket.accept();
  85.  
  86.                 Log.d("TCP", "S: Receiving...");
  87.  
  88.                 try {
  89.  
  90.                      BufferedReader in = new BufferedReader(new
  91.  
  92. InputStreamReader(client.getInputStream()));
  93.  
  94.                      String str = in.readLine();
  95.  
  96.                      Log.d("TCP", "S: Received: '" + str + "'");
  97.  
  98.                      AndroidServer.tv.setText(str + " ------ " +
  99.  
  100. SERVERIP);
  101.  
  102.                    } catch(Exception e) {
  103.  
  104.                        Log.e("TCP", "S: Error", e);
  105.  
  106.                    } finally {
  107.  
  108.                         client.close();
  109.  
  110.                         Log.d("TCP", "S: Done.");
  111.  
  112.                    }
  113.  
  114.  
  115.  
  116.              }
  117.  
  118.  
  119.  
  120.         } catch (Exception e) {
  121.  
  122.           Log.e("TCP", "S: Error", e);
  123.  
  124.         }
  125.  
  126.    }
  127.  
  128. }
  129.  
  130.  
  131.  
  132.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Client:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. import java.io.BufferedWriter;
  5.  
  6. import java.io.OutputStreamWriter;
  7.  
  8. import java.io.PrintWriter;
  9.  
  10. import java.net.InetAddress;
  11.  
  12. import java.net.Socket;
  13.  
  14.  
  15.  
  16. import android.app.Activity;
  17.  
  18. import android.os.Bundle;
  19.  
  20. import android.util.Log;
  21.  
  22. import android.widget.TextView;
  23.  
  24.  
  25.  
  26. public class AndroidClient extends Activity {
  27.  
  28.    /** Called when the activity is first created. */
  29.  
  30.        public TextView tv;
  31.  
  32.    @Override
  33.  
  34.    public void onCreate(Bundle savedInstanceState) {
  35.  
  36.        super.onCreate(savedInstanceState);
  37.  
  38. //        setContentView(R.layout.main);
  39.  
  40.  
  41.  
  42.        tv = new TextView(this);
  43.  
  44.        tv.setText("Main - client");
  45.  
  46.        setContentView(tv);
  47.  
  48.  
  49.  
  50.        Thread cThread = new Thread(new TCPClientTest());
  51.  
  52.  
  53.  
  54.        cThread.start();
  55.  
  56.    }
  57.  
  58. }
  59.  
  60.  
  61.  
  62. class TCPClientTest implements Runnable {
  63.  
  64.  
  65.  
  66.  
  67.  
  68.    public void run() {
  69.  
  70.         try {
  71.  
  72.  
  73.  
  74.           InetAddress serverAddr = InetAddress.getByName("10.0.2.2");
  75.  
  76.  
  77.  
  78.           Log.d("TCP", "C: Connecting...");
  79.  
  80.           Socket socket = new Socket(serverAddr, 5555);
  81.  
  82.           String message = "Hello from Client";
  83.  
  84.               try {
  85.  
  86.                Log.d("TCP", "C: Sending: '" + message + "'");
  87.  
  88.                PrintWriter out = new PrintWriter( new BufferedWriter
  89.  
  90. ( new OutputStreamWriter(socket.getOutputStream())),true);
  91.  
  92.  
  93.  
  94.                out.println(message);
  95.  
  96.                Log.d("TCP", "C: Sent.");
  97.  
  98.                  Log.d("TCP", "C: Done.");
  99.  
  100.  
  101.  
  102.             } catch(Exception e) {
  103.  
  104.                 Log.e("TCP", "S: Error", e);
  105.  
  106.                } finally {
  107.  
  108.                  socket.close();
  109.  
  110.                }
  111.  
  112.         } catch (Exception e) {
  113.  
  114.              Log.e("TCP", "C: Error", e);
  115.  
  116.         }
  117.  
  118.    }
  119.  
  120. }
  121.  
  122.  
  123.  
  124.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


The problem can be seen from the log files

Server:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. D/TCP     (  700): S: Connecting...
  5.  
  6.  
  7.  
  8.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Clinet:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. D/TCP     (  703): C: Connecting...
  5.  
  6. D/TCP     (  703): C: Sending: 'Hello from Client'
  7.  
  8. I/global  (  703): Default buffer size used in BufferedWriter
  9.  
  10. constructor. It would be better to be explicit if an 8k-char buffer is
  11.  
  12. required.
  13.  
  14. D/TCP     (  703): C: Sent.
  15.  
  16. D/TCP     (  703): C: Done.
  17.  
  18.  
  19.  
  20.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Note when i run the program the server has id 5554 and the client id
5556 from the emulators.

I have used ./adb -s 5554 forward tcp:4444 tcp:5555

However the client message fails to send to the server. Does anyone
have any idea why?

I have tried doing ./adb -s 5556 forward tcp:5555 tcp:4444 in case
that was the problem but i had no luck.

Help would be very much appreciated.
mike88
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Nov 12, 2009 8:52 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 10 guests