Hey does anyone have any idea why the server does not receive the message
that is sent. Your help is much appreciated.

On Thu, Nov 12, 2009 at 1:49 AM, Mike88 <michael...@gmail.com> wrote:

> Hello, I have created a simple server and client both on android phone
> or emulators. The code is as follows
>
> Server:
>
> import java.io.BufferedReader;
> import java.io.InputStreamReader;
> import java.net.ServerSocket;
> import java.net.Socket;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.util.Log;
> import android.widget.TextView;
>
> public class AndroidServer extends Activity {
>    /** Called when the activity is first created. */
>        public static TextView tv;
>    @Override
>    public void onCreate(Bundle savedInstanceState) {
>        super.onCreate(savedInstanceState);
> //        setContentView(R.layout.main);
>
>        tv = new TextView(this);
>        tv.setText("Main - server");
>        setContentView(tv);
>
>        Thread sThread = new Thread(new TCPServerTest());
>
>        sThread.start();
>    }
> }
>
> class TCPServerTest implements Runnable{
>
>    public static final String SERVERIP = "10.0.2.15";
>    public static final int SERVERPORT = 4444;
>
>    public void run() {
>         try {
>              Log.d("TCP", "S: Connecting...");
>
>              ServerSocket serverSocket = new ServerSocket
> (SERVERPORT);
>              while (true) {
>                 Socket client = serverSocket.accept();
>                 Log.d("TCP", "S: Receiving...");
>                 try {
>                      BufferedReader in = new BufferedReader(new
> InputStreamReader(client.getInputStream()));
>                      String str = in.readLine();
>                      Log.d("TCP", "S: Received: '" + str + "'");
>                      AndroidServer.tv.setText(str + " ------ " +
> SERVERIP);
>                    } catch(Exception e) {
>                        Log.e("TCP", "S: Error", e);
>                    } finally {
>                         client.close();
>                         Log.d("TCP", "S: Done.");
>                    }
>
>              }
>
>         } catch (Exception e) {
>           Log.e("TCP", "S: Error", e);
>         }
>    }
> }
>
> Client:
>
> import java.io.BufferedWriter;
> import java.io.OutputStreamWriter;
> import java.io.PrintWriter;
> import java.net.InetAddress;
> import java.net.Socket;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.util.Log;
> import android.widget.TextView;
>
> public class AndroidClient extends Activity {
>    /** Called when the activity is first created. */
>        public TextView tv;
>    @Override
>    public void onCreate(Bundle savedInstanceState) {
>        super.onCreate(savedInstanceState);
> //        setContentView(R.layout.main);
>
>        tv = new TextView(this);
>        tv.setText("Main - client");
>        setContentView(tv);
>
>        Thread cThread = new Thread(new TCPClientTest());
>
>        cThread.start();
>    }
> }
>
> class TCPClientTest implements Runnable {
>
>
>    public void run() {
>         try {
>
>           InetAddress serverAddr = InetAddress.getByName("10.0.2.2");
>
>           Log.d("TCP", "C: Connecting...");
>           Socket socket = new Socket(serverAddr, 5555);
>           String message = "Hello from Client";
>               try {
>                Log.d("TCP", "C: Sending: '" + message + "'");
>                PrintWriter out = new PrintWriter( new BufferedWriter
> ( new OutputStreamWriter(socket.getOutputStream())),true);
>
>                out.println(message);
>                Log.d("TCP", "C: Sent.");
>                  Log.d("TCP", "C: Done.");
>
>             } catch(Exception e) {
>                 Log.e("TCP", "S: Error", e);
>                } finally {
>                  socket.close();
>                }
>         } catch (Exception e) {
>              Log.e("TCP", "C: Error", e);
>         }
>    }
> }
>
>
> The problem can be seen from the log files
>
> Server:
>
> D/TCP     (  700): S: Connecting...
>
> Clinet:
>
> D/TCP     (  703): C: Connecting...
> D/TCP     (  703): C: Sending: 'Hello from Client'
> I/global  (  703): Default buffer size used in BufferedWriter
> constructor. It would be better to be explicit if an 8k-char buffer is
> required.
> D/TCP     (  703): C: Sent.
> D/TCP     (  703): C: Done.
>
>
> 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.

-- 
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

Reply via email to