1. Download our Official Android App: Forums for Android!

Apps android client and server side programming

Discussion in 'Android Development' started by mIXpRo, Aug 6, 2013.

  1. mIXpRo

    mIXpRo Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    17
    Joined:
    Sep 13, 2010

    Sep 13, 2010
    17
    0
    15
    hi,
    I'm trying to run server side and client side examples I found on the net .
    Here's the server :
    [HIGH]

    public class ServerActivity extends Activity {

    private TextView serverStatus;
    private TextView serverStatus2;

    // DEFAULT IP
    public static String SERVERIP = "10.100.102.15";

    // DESIGNATE A PORT
    public static final int SERVERPORT = 8080;

    private Handler handler = new Handler();

    private ServerSocket serverSocket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_server);
    serverStatus = (TextView) findViewById(R.id.server_status);
    serverStatus2 = (TextView) findViewById(R.id.server_status2);

    SERVERIP = getLocalIpAddress();

    Thread fst = new Thread(new ServerThread());
    fst.start();
    }

    public class ServerThread implements Runnable {

    public void run() {
    try {
    if (SERVERIP != null) {
    handler.post(new Runnable() {
    @Override
    public void run() {
    serverStatus
    .setText("Listening on IP: " + SERVERIP);
    }
    });
    serverSocket = new ServerSocket(SERVERPORT);
    while (true) {
    // LISTEN FOR INCOMING CLIENTS
    Socket client = serverSocket.accept();
    handler.post(new Runnable() {
    @Override
    public void run() {
    serverStatus2.setText("Connected.");
    }
    });

    try {
    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    client.getInputStream()));
    String line = null;
    while ((line = in.readLine()) != null) {
    Log.d("ServerActivity", line);
    handler.post(new Runnable() {
    @Override
    public void run() {
    // DO WHATEVER YOU WANT TO THE FRONT END
    // THIS IS WHERE YOU CAN BE CREATIVE
    }
    });
    }
    break;
    } catch (Exception e) {
    handler.post(new Runnable() {
    @Override
    public void run() {
    serverStatus
    .setText("Oops. Connection interrupted. Please reconnect your phones.");
    }
    });
    e.printStackTrace();
    }
    }
    } else {
    handler.post(new Runnable() {
    @Override
    public void run() {
    serverStatus
    .setText("Couldn't detect internet connection.");
    }
    });
    }
    } catch (final Exception e) {
    handler.post(new Runnable() {
    @Override
    public void run() {
    serverStatus.setText("Error" + e.getMessage());
    }
    });
    e.printStackTrace();
    }
    }
    }

    // GETS THE IP ADDRESS OF YOUR PHONE'S NETWORK
    private String getLocalIpAddress() {
    try {
    for (Enumeration<NetworkInterface> en = NetworkInterface
    .getNetworkInterfaces(); en.hasMoreElements();) {
    NetworkInterface intf = en.nextElement();
    for (Enumeration<InetAddress> enumIpAddr = intf
    .getInetAddresses(); enumIpAddr.hasMoreElements();) {
    InetAddress inetAddress = enumIpAddr.nextElement();
    if (!inetAddress.isLoopbackAddress()) {
    return inetAddress.getHostAddress().toString();
    }
    }
    }
    } catch (SocketException ex) {
    Log.e("ServerActivity", ex.toString());
    }
    return null;
    }

    @Override
    protected void onStop() {
    super.onStop();
    try {
    // MAKE SURE YOU CLOSE THE SOCKET UPON EXITING
    serverSocket.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }

    [/HIGH]
    And here's the client's code:
    [HIGH]

    public class ClientActivity extends Activity {

    private EditText serverIp;

    private Button connectPhones;

    private String serverIpAddress = "";

    private boolean connected = false;

    //private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client);

    serverIp = (EditText) findViewById(R.id.server_ip);
    connectPhones = (Button) findViewById(R.id.connect_phones);
    connectPhones.setOnClickListener(connectListener);
    }

    private OnClickListener connectListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
    if (!connected) {
    serverIpAddress = serverIp.getText().toString();
    if (!serverIpAddress.equals("")) {
    Thread cThread = new Thread(new ClientThread());
    cThread.start();
    }
    }
    }
    };

    public class ClientThread implements Runnable {

    public void run() {
    try {
    InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
    Log.d("ClientActivity", "C: Connecting...");
    Socket socket = new Socket(serverAddr,
    8080);
    connected = true;
    while (connected) {
    try {
    Log.d("ClientActivity", "C: Sending command.");
    PrintWriter out = new PrintWriter(
    new BufferedWriter(new OutputStreamWriter(
    socket.getOutputStream())), true);
    // WHERE YOU ISSUE THE COMMANDS
    out.println("Hey Server!");
    Log.d("ClientActivity", "C: Sent.");
    } catch (Exception e) {
    Log.e("ClientActivity", "S: Error", e);
    }
    }
    socket.close();
    Log.d("ClientActivity", "C: Closed.");
    } catch (Exception e) {
    Log.e("ClientActivity", "C: Error", e);
    connected = false;
    }
    }
    }
    }
    [/HIGH]
    when running the above , the server throws and exception "Socket Closed" ,
    without even reaching the while loop "while (true)".
    Does anybody have an idea?
     

    Advertisement

  2. SiempreTuna

    SiempreTuna Android Expert
    Rank:
    None
    Points:
    423
    Posts:
    3,889
    Joined:
    Jan 11, 2013

    Jan 11, 2013
    3,889
    1,051
    423
    Dumb question, but you're sure you have the connection details correct? Do you have something on port 8080 at 10.100.102.15?
     
  3. mIXpRo

    mIXpRo Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    17
    Joined:
    Sep 13, 2010

    Sep 13, 2010
    17
    0
    15
    it doesn't matter , because in onCreate() function i do :
    which initializes SERVERIP to the emulator ip which is : 10.0.2.15
    at least that what :
    prints.
     

Share This Page

Loading...