1. Welcome to the newer, better version of Android Forums! Be sure to see the announcement and enjoy checking it out!

    Some of you have been having login issues. - Please try now. Sorry for the trouble!
  2. All attachments uploaded on the first day of this new look need to be re-uploaded, or will appear broken. All prior to that, and all going forward, should work fine. We apologize for the inconvenience!

Simple TCP Socket Client Application


  1. Jasp3r

    Jasp3r New Member

    I have currently created a simple tcp socket client application that sends and receives messages from a dotnet server application I have created. The application itself actually works however there are a few things I would like to know if they are possible or not. Also this is my first ever droid application and my first ever java application so I am sure there is a better way to do what I have written so please go easy on me.

    The problem that I am having is I need a way to monitor the status of the socket so that if the connection gets dropped whether it is from the server application ending or a network drop that the android client would disconnect and put a notification on the screen letting me know. Currently if I end the server application nothign happens on the client and it acts like the connection is still open. Granted this is probably due to the way I have it written so I am looking for samples or suggestions on how to change this so I can monitor this. Any help would be great. Eventually this will be put into a much larger project for my home but I am starting off small so I can do it correctly the first time rather than having someing messy later on.

    My code is listed below.

    Thanks
    KJ

    Source:

    package com.kkearney.tcp1;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    public class TCPClient1Activity extends Activity {
    public static final int BUFFER_SIZE = 2048;
    private Socket sck = null;
    private PrintWriter out = null;
    private BufferedReader in = null;

    Button btnSend;
    Button btnConnect;
    Button btnDisconnect;
    EditText textOut;
    TextView textIn;
    String response = "";
    final Handler handler = new Handler();
    final Runnable updateUI = new Runnable() {
    public void run() {
    textIn.setText(response);
    }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    textOut = (EditText)findViewById(R.id.txtSend);
    textIn = (TextView)findViewById(R.id.txtMSG);
    btnSend = (Button)findViewById(R.id.btnSend);
    btnConnect = (Button)findViewById(R.id.btnConnect);
    btnDisconnect = (Button)findViewById(R.id.btnDisconnect);
    }

    public void ConnectUp(View view){
    OpenConnection();
    }


    public void OpenConnection(){
    new Thread(new Runnable(){
    public void run(){
    try{
    if (sck == null){
    sck = new Socket("10.0.0.135",8080);
    sck.setKeepAlive(true);
    screenConfig();

    in = new BufferedReader(new InputStreamReader(sck.getInputStream()));
    out = new PrintWriter(sck.getOutputStream());
    sendDataWithString("testing 1 2 3");
    int charsRead = 0;
    char[] buffer = new char[BUFFER_SIZE];

    while ((charsRead = in.read(buffer)) != -1) {
    response += new String(buffer).substring(0, charsRead);
    handler.post( updateUI );
    }

    screenConfig();
    }
    }
    catch (IOException e) {
    System.out.print(e+"");
    }
    catch (Exception e) {
    System.out.print(e+"");
    }
    }
    }).start();


    }
    public void screenConfig(){
    if (sck.isClosed() == false){
    textOut.setClickable(true);
    btnSend.setClickable(true);
    btnDisconnect.setClickable(true);
    btnConnect.setClickable(false);
    }else{
    textOut.setClickable(false);
    btnSend.setClickable(false);
    btnDisconnect.setClickable(false);
    btnConnect.setClickable(true);
    }
    }

    public void SendMessage(View view){
    sendDataWithString(textOut.getText().toString());
    textOut.setText(null);
    }
    public void DisConnect(View view){
    CloseConnection();
    }

    public void CloseConnection(){
    if (sck != null) {
    try {
    sck.close();
    screenConfig();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    sck = null;
    }
    }
    }

    public void sendDataWithString(String message) {
    if (message != null && sck != null) {
    out.write(message);
    out.flush();
    }
    }

    }


    main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:eek:rientation="vertical" >
    <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <Button
    android:id="@+id/btnConnect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Connect" android:eek:nClick="ConnectUp" android:clickable="true"/>
    <Button
    android:id="@+id/btnDisconnect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Disconnect" android:eek:nClick="DisConnect" android:clickable="false"/>
    </TableRow>
    <EditText
    android:id="@+id/txtSend"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:clickable="false">
    <requestFocus />
    </EditText>
    <Button
    android:id="@+id/btnSend"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Send" android:eek:nClick="SendMessage" android:clickable="false"/>
    <TextView
    android:id="@+id/txtMSG"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />
    </LinearLayout>

    Advertisement

Share This Page